/** * Writes the given integer value to the buffer. * * @param value the value to write * @see #writeInt32 */ public void writeInt(final int value) { writeInt32(value); }
/** * 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 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 void writeString(final String str) { writeInt(0); // making space for size int strLen = writeCharacters(str, false); writeInt32(getPosition() - strLen - 4, strLen); }
/** * Backpatches the message length into the beginning of the message. * * @param startPosition the start position of the message * @param bsonOutput the output */ protected void backpatchMessageLength(final int startPosition, final BsonOutput bsonOutput) { int messageLength = bsonOutput.getPosition() - startPosition; bsonOutput.writeInt32(bsonOutput.getPosition() - messageLength, messageLength); }
/** * Reads and returns a single long value from the input stream. * * @param inputStream the input stream to read from * @return the long value * @throws IOException if there's an error reading from the {@code inputStream} */ public static long readLong(final InputStream inputStream) throws IOException { return readLong(inputStream, new byte[8]); }
@Override public void write(final byte[] b) { ensureOpen(); write(b, 0, b.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 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); }
@Override public void skipCString() { ensureOpen(); readUntilNullByte(); }
@Override public void writeByte(final int value) { ensureOpen(); ensure(1); buffer[position++] = (byte) (0xFF & value); }
/** * Creates a new instance storing the current position of the {@link org.bson.io.BsonOutput}. */ protected Mark() { this.position = bsonOutput.getPosition(); }
private void writeKillCursorsPrologue(final int numCursors, final BsonOutput bsonOutput) { bsonOutput.writeInt32(0); // reserved bsonOutput.writeInt32(numCursors); } }
@Override public int getPosition() { ensureOpen(); return position; }
private void readUntilNullByte() { //CHECKSTYLE:OFF while (readByte() != 0) { //NOPMD //do nothing - checkstyle & PMD hate this, not surprisingly } //CHECKSTYLE:ON }
/** * Gets the output size in bytes. * @return the size */ public int size() { return getSize(); }
/** * Writes the given long value to the buffer. * * @param value the value to write * @see #writeInt64 */ public void writeLong(final long value) { writeInt64(value); }
/** * 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); }
@Override public void writeInt64(final long value) { write((byte) (0xFFL & (value >> 0))); write((byte) (0xFFL & (value >> 8))); write((byte) (0xFFL & (value >> 16))); write((byte) (0xFFL & (value >> 24))); write((byte) (0xFFL & (value >> 32))); write((byte) (0xFFL & (value >> 40))); write((byte) (0xFFL & (value >> 48))); write((byte) (0xFFL & (value >> 56))); }
/** * @return size of data so far */ @Override public int getSize() { ensureOpen(); return position; }