Search in sources :

Example 1 with ByteBuf

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;
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(org.bson.ByteBuf)

Example 2 with ByteBuf

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;
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(org.bson.ByteBuf)

Example 3 with ByteBuf

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();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 4 with ByteBuf

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;
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(org.bson.ByteBuf)

Example 5 with ByteBuf

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);
}
Also used : ByteBuf(org.bson.ByteBuf)

Aggregations

ByteBuf (org.bson.ByteBuf)48 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)9 ArrayList (java.util.ArrayList)6 BsonBinaryReader (org.bson.BsonBinaryReader)6 ByteBufNIO (org.bson.ByteBufNIO)4 MongoSocketReadException (com.mongodb.MongoSocketReadException)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)3 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 MongoInternalException (com.mongodb.MongoInternalException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 JsonWriter (org.bson.json.JsonWriter)2 MongoClientException (com.mongodb.MongoClientException)1 MongoCompressor (com.mongodb.MongoCompressor)1 MongoException (com.mongodb.MongoException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoSocketClosedException (com.mongodb.MongoSocketClosedException)1 MongoSocketException (com.mongodb.MongoSocketException)1