use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufferBsonOutput method getByteBuffers.
@Override
public List<ByteBuf> getByteBuffers() {
ensureOpen();
List<ByteBuf> buffers = new ArrayList<ByteBuf>(bufferList.size());
for (final ByteBuf cur : bufferList) {
buffers.add(cur.duplicate().flip());
}
return buffers;
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method create.
static List<ByteBufBsonDocument> create(final ResponseBuffers responseBuffers) {
int numDocuments = responseBuffers.getReplyHeader().getNumberReturned();
ByteBuf documentsBuffer = responseBuffers.getBodyByteBuffer();
documentsBuffer.order(ByteOrder.LITTLE_ENDIAN);
List<ByteBufBsonDocument> documents = new ArrayList<ByteBufBsonDocument>(numDocuments);
while (documents.size() < numDocuments) {
int documentSizeInBytes = documentsBuffer.getInt();
documentsBuffer.position(documentsBuffer.position() - 4);
ByteBuf documentBuffer = documentsBuffer.duplicate();
documentBuffer.limit(documentBuffer.position() + documentSizeInBytes);
documents.add(new ByteBufBsonDocument(documentBuffer));
documentsBuffer.position(documentsBuffer.position() + documentSizeInBytes);
}
return documents;
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
ByteBuf duplicate = byteBuf.duplicate();
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
try {
jsonWriter.pipe(reader);
return stringWriter.toString();
} finally {
duplicate.release();
reader.close();
}
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method create.
static List<ByteBufBsonDocument> create(final ByteBufferBsonOutput bsonOutput, final int startPosition) {
CompositeByteBuf outputByteBuf = new CompositeByteBuf(bsonOutput.getByteBuffers());
outputByteBuf.position(startPosition);
List<ByteBufBsonDocument> documents = new ArrayList<ByteBufBsonDocument>();
int curDocumentStartPosition = startPosition;
while (outputByteBuf.hasRemaining()) {
int documentSizeInBytes = outputByteBuf.getInt();
ByteBuf slice = outputByteBuf.duplicate();
slice.position(curDocumentStartPosition);
slice.limit(curDocumentStartPosition + documentSizeInBytes);
documents.add(new ByteBufBsonDocument(slice));
curDocumentStartPosition += documentSizeInBytes;
outputByteBuf.position(outputByteBuf.position() + documentSizeInBytes - 4);
}
return documents;
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufferBsonOutput method write.
protected void write(final int absolutePosition, final int value) {
ensureOpen();
if (absolutePosition < 0) {
throw new IllegalArgumentException(String.format("position must be >= 0 but was %d", absolutePosition));
}
if (absolutePosition > position - 1) {
throw new IllegalArgumentException(String.format("position must be <= %d but was %d", position - 1, absolutePosition));
}
BufferPositionPair bufferPositionPair = getBufferPositionPair(absolutePosition);
ByteBuf byteBuffer = getByteBufferAtIndex(bufferPositionPair.bufferIndex);
byteBuffer.put(bufferPositionPair.position++, (byte) value);
}
Aggregations