Search in sources :

Example 6 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput 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 7 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.

the class InternalStreamConnection method receiveResponseBuffers.

private ResponseBuffers receiveResponseBuffers() throws IOException {
    ByteBuf headerByteBuffer = stream.read(REPLY_HEADER_LENGTH);
    ReplyHeader replyHeader;
    ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(headerByteBuffer);
    try {
        replyHeader = new ReplyHeader(headerInputBuffer, description.getMaxMessageSize());
    } finally {
        headerInputBuffer.close();
    }
    ByteBuf bodyByteBuffer = null;
    if (replyHeader.getNumberReturned() > 0) {
        bodyByteBuffer = stream.read(replyHeader.getMessageLength() - REPLY_HEADER_LENGTH);
    }
    return new ResponseBuffers(replyHeader, bodyByteBuffer);
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 8 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.

the class DBEncoderAdapter method encode.

// TODO: this can be optimized to reduce copying of buffers.  For that we'd need an InputBuffer that could iterate
//       over an array of ByteBuffer instances from a PooledByteBufferOutputBuffer
@Override
public void encode(final BsonWriter writer, final DBObject document, final EncoderContext encoderContext) {
    BasicOutputBuffer buffer = new BasicOutputBuffer();
    try {
        encoder.writeObject(buffer, document);
        BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(buffer.toByteArray()))));
        try {
            writer.pipe(reader);
        } finally {
            reader.close();
        }
    } finally {
        buffer.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 9 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.

the class TestInternalConnection method sendMessage.

@Override
public void sendMessage(final List<ByteBuf> byteBuffers, final int lastRequestId) {
    // repackage all byte buffers into a single byte buffer...
    int totalSize = 0;
    for (ByteBuf buf : byteBuffers) {
        totalSize += buf.remaining();
    }
    ByteBuffer combined = ByteBuffer.allocate(totalSize);
    for (ByteBuf buf : byteBuffers) {
        combined.put(buf.array(), 0, buf.remaining());
    }
    combined.flip();
    Interaction interaction = replies.getFirst();
    if (interaction.responseBuffers != null) {
        ReplyHeader header = replaceResponseTo(interaction.responseBuffers.getReplyHeader(), lastRequestId);
        interaction.responseBuffers = (new ResponseBuffers(header, interaction.responseBuffers.getBodyByteBuffer()));
        sent.add(new ByteBufferBsonInput(new ByteBufNIO(combined)));
    } else if (interaction.sendException != null) {
        replies.removeFirst();
        throw interaction.sendException;
    }
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 10 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.

the class BasicBSONDecoder method decode.

@Override
public int decode(final byte[] bytes, final BSONCallback callback) {
    BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
    try {
        BsonWriter writer = new BSONCallbackAdapter(new BsonWriterSettings(), callback);
        writer.pipe(reader);
        //TODO check this.
        return reader.getBsonInput().getPosition();
    } finally {
        reader.close();
    }
}
Also used : ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)24 Test (org.junit.Test)11 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)10 ByteBufNIO (org.bson.ByteBufNIO)9 BsonBinaryReader (org.bson.BsonBinaryReader)7 ByteBuffer (java.nio.ByteBuffer)5 ByteBuf (org.bson.ByteBuf)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 StringWriter (java.io.StringWriter)1 Document (org.bson.Document)1 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)1 JsonWriter (org.bson.json.JsonWriter)1 ObjectId (org.bson.types.ObjectId)1