// 
// Decompiled by Procyon v0.6.0
// 

package org.bson;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

class Bits
{
    static void readFully(final InputStream inputStream, final byte[] buffer) throws IOException {
        readFully(inputStream, buffer, 0, buffer.length);
    }
    
    static void readFully(final InputStream inputStream, final byte[] buffer, final int offset, final int length) throws IOException {
        if (buffer.length < length + offset) {
            throw new IllegalArgumentException("Buffer is too small");
        }
        int bytesRead;
        for (int arrayOffset = offset, bytesToRead = length; bytesToRead > 0; bytesToRead -= bytesRead, arrayOffset += bytesRead) {
            bytesRead = inputStream.read(buffer, arrayOffset, bytesToRead);
            if (bytesRead < 0) {
                throw new EOFException();
            }
        }
    }
    
    static int readInt(final InputStream inputStream, final byte[] buffer) throws IOException {
        readFully(inputStream, buffer, 0, 4);
        return readInt(buffer);
    }
    
    static int readInt(final byte[] buffer) {
        return readInt(buffer, 0);
    }
    
    static int readInt(final byte[] buffer, final int offset) {
        int x = 0;
        x |= (0xFF & buffer[offset + 0]) << 0;
        x |= (0xFF & buffer[offset + 1]) << 8;
        x |= (0xFF & buffer[offset + 2]) << 16;
        x |= (0xFF & buffer[offset + 3]) << 24;
        return x;
    }
    
    static long readLong(final InputStream inputStream) throws IOException {
        return readLong(inputStream, new byte[8]);
    }
    
    static long readLong(final InputStream inputStream, final byte[] buffer) throws IOException {
        readFully(inputStream, buffer, 0, 8);
        return readLong(buffer);
    }
    
    static long readLong(final byte[] buffer) {
        return readLong(buffer, 0);
    }
    
    static long readLong(final byte[] buffer, final int offset) {
        long x = 0L;
        x |= (0xFFL & (long)buffer[offset + 0]) << 0;
        x |= (0xFFL & (long)buffer[offset + 1]) << 8;
        x |= (0xFFL & (long)buffer[offset + 2]) << 16;
        x |= (0xFFL & (long)buffer[offset + 3]) << 24;
        x |= (0xFFL & (long)buffer[offset + 4]) << 32;
        x |= (0xFFL & (long)buffer[offset + 5]) << 40;
        x |= (0xFFL & (long)buffer[offset + 6]) << 48;
        x |= (0xFFL & (long)buffer[offset + 7]) << 56;
        return x;
    }
}
