/** * Reads bytes from the input stream and puts them into the given byte buffer. The equivalent of calling * {@link #readFully(java.io.InputStream, byte[], int, int)} with an offset of zero and a length equal to the length of the buffer. * * @param inputStream the input stream to read from * @param buffer the buffer into which the data is read. * @throws IOException if there's an error reading from the {@code inputStream} */ public static void readFully(final InputStream inputStream, final byte[] buffer) throws IOException { readFully(inputStream, buffer, buffer.length); }
/** * Reads bytes from the input stream and puts them into the given byte buffer. The equivalent of calling * {@link #readFully(java.io.InputStream, byte[], int, int)} with an offset of zero. * * @param inputStream the input stream to read from * @param buffer the buffer into which the data is read. * @param length the maximum number of bytes to read. * @throws IOException if there's an error reading from the {@code inputStream} */ public static void readFully(final InputStream inputStream, final byte[] buffer, final int length) throws IOException { readFully(inputStream, buffer, 0, length); }
private byte[] readFully(final InputStream input) throws IOException { byte[] sizeBytes = new byte[4]; Bits.readFully(input, sizeBytes); int size = Bits.readInt(sizeBytes); byte[] buffer = new byte[size]; System.arraycopy(sizeBytes, 0, buffer, 0, 4); Bits.readFully(input, buffer, 4, size - 4); return buffer; } }
/** * Reads and returns a single integer value from the input stream. * * @param inputStream the input stream to read from * @param buffer the buffer to write the input stream bytes into * @return the integer value * @throws IOException if there's an error reading from the {@code inputStream} */ public static int readInt(final InputStream inputStream, final byte[] buffer) throws IOException { readFully(inputStream, buffer, 4); return readInt(buffer); }
/** * Reads and returns a single long value from the input stream. * * @param inputStream the input stream to read from * @param buffer the buffer to write the input stream bytes into * @return the long value * @throws IOException if there's an error reading from the {@code inputStream} */ public static long readLong(final InputStream inputStream, final byte[] buffer) throws IOException { readFully(inputStream, buffer, 8); return readLong(buffer); }
@Override public int decode(final InputStream in, final BSONCallback callback) throws IOException { byte[] documentSizeBuffer = new byte[BYTES_IN_INTEGER]; int documentSize = Bits.readInt(in, documentSizeBuffer); byte[] documentBytes = Arrays.copyOf(documentSizeBuffer, documentSize); Bits.readFully(in, documentBytes, BYTES_IN_INTEGER, documentSize - BYTES_IN_INTEGER); // note that we are handing off ownership of the documentBytes byte array to the callback callback.gotBinary(null, (byte) 0, documentBytes); return documentSize; } }
public static void readFully( InputStream in, byte[] b, int length ) throws IOException { readFully(in, b, 0, length); }
public static void readFully( InputStream in, byte[] b ) throws IOException { readFully( in , b , b.length ); }
public static int readInt( InputStream in , byte[] data ) throws IOException { readFully(in, data, 4); return readInt(data); }
public static long readLong( InputStream in , byte[] data ) throws IOException { readFully(in, data, 8); return readLong(data); }