/** * Reads and returns a single integer value from the buffer. The equivalent of calling {@link #readInt(byte[], int)} * with an offset of zero. * * @param buffer the buffer to read from * @return the integer value */ public static int readInt(final byte[] buffer) { return readInt(buffer, 0); }
/** * Reads and returns a single integer value from the input stream. * * @param inputStream the input stream to read from * @return the integer value * @throws IOException if there's an error reading from the {@code inputStream} */ public static int readInt(final InputStream inputStream) throws IOException { return readInt(inputStream, new byte[4]); }
/** * 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); }
@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; } }
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; } }
public static int readInt( InputStream in ) throws IOException { return readInt( in , new byte[4] ); }
public static int readInt( byte[] data ) { return readInt( data , 0 ); }
@Override public int decode(byte[] b, BSONCallback callback) { _length = Bits.readInt(b); return _decode(b, 0, callback); }
private synchronized void checkHeader(){ // Read the BSON length from the start of the record byte[] l = new byte[4]; try { _input.readFully( l ); nextLen = org.bson.io.Bits.readInt( l ); nextHdr = l; hasMore.set( true ); } catch (Exception e) { log.debug( "Failed to get next header: " + e, e ); hasMore.set( false ); try { _input.close(); } catch ( IOException e1 ) { } } }
public BSONObject readObject(final InputStream pIn) throws IOException { // Slurp in the data and convert to a byte array. _length = Bits.readInt(pIn); if (_data == null || _data.length < _length) { _data = new byte[_length]; } (new DataInputStream(pIn)).readFully(_data, 4, (_length - 4)); return readObject(_data); }
@Override public int decode(byte[] b, int offset, BSONCallback callback) { _length = Bits.readInt(b, offset); return _decode(b, offset, callback); }
public static int readInt( InputStream in , byte[] data ) throws IOException { readFully(in, data, 4); return readInt(data); }
private synchronized void checkHeader(){ // Read the BSON length from the start of the record byte[] l = new byte[4]; try { _input.readFully( l ); nextLen = org.bson.io.Bits.readInt( l ); nextHdr = l; hasMore.set( true ); } catch (Exception e) { log.debug( "Failed to get next header: " + e, e ); hasMore.set( false ); try { _input.close(); } catch ( IOException e1 ) { } } }
private synchronized void checkHeader(){ // Read the BSON length from the start of the record byte[] l = new byte[4]; try { _input.readFully( l ); nextLen = org.bson.io.Bits.readInt( l ); nextHdr = l; hasMore.set( true ); } catch (Exception e) { log.debug( "Failed to get next header: " + e, e ); hasMore.set( false ); try { _input.close(); } catch ( IOException e1 ) { } } }
public int decode(final InputStream pIn, final BSONCallback pCallback) throws IOException { _length = Bits.readInt(pIn); if (_data == null || _data.length < _length) { _data = new byte[_length]; } (new DataInputStream(pIn)).readFully(_data, 4, (_length - 4)); return _decode(_data, 0, pCallback); }
private synchronized void checkHeader(){ // Read the BSON length from the start of the record byte[] l = new byte[4]; try { _input.readFully( l ); nextLen = org.bson.io.Bits.readInt( l ); nextHdr = l; hasMore.set( true ); } catch (Exception e) { log.debug( "Failed to get next header: " + e, e ); hasMore.set( false ); try { _input.close(); } catch ( IOException e1 ) { } } }
public int readInt() throws IOException { return org.bson.io.Bits.readInt( _inputBuffer , _need(4) ); }
static final int readInt32(ChannelBuffer buffer, byte[] fourBytes) { buffer.readBytes(fourBytes); return Bits.readInt(fourBytes); }
private final String readUtf8Str() { final int length = Bits.readInt(_data, _pos); _pos += 4; if (length <= 0 || length > MAX_STRING) throw new BSONException("String invalid - corruption"); try { final String str = new String(_data, _pos, (length - 1), DEFAULT_ENCODING); _pos += length; return str; } catch (final UnsupportedEncodingException uee) { throw new BSONException("What is in the db", uee); } }
@Override public BSONObject readObject(byte[] b, int offset) { _length = Bits.readInt(b, offset); final BasicBSONCallback c = new BasicBSONCallback(); _decode(b, offset, c); return (BSONObject)c.get(); }