Search in sources :

Example 1 with ByteBufNIO

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

the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.

static BsonBinaryReader prepareReaderWithObjectToBeDecoded(final Object objectToDecode) {
    //Need to encode it wrapped in a document to conform to the validation
    Document document = new Document("wrapperDocument", objectToDecode);
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    byte[] documentAsByteArrayForReader;
    try {
        new DocumentCodec().encode(writer, document, EncoderContext.builder().build());
        documentAsByteArrayForReader = outputBuffer.toByteArray();
    } finally {
        writer.close();
    }
    BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
    //have to read off the wrapper document so the reader is in the correct position for the test
    reader.readStartDocument();
    reader.readName();
    return reader;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) Document(org.bson.Document) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 2 with ByteBufNIO

use of org.bson.ByteBufNIO 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 3 with ByteBufNIO

use of org.bson.ByteBufNIO 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 4 with ByteBufNIO

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

the class ReplyMessageTest method shouldThrowExceptionIfOpCodeIsIncorrect.

@Test(expected = MongoInternalException.class)
public void shouldThrowExceptionIfOpCodeIsIncorrect() {
    int badOpCode = 2;
    ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
    headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    headerByteBuffer.putInt(36);
    headerByteBuffer.putInt(2456);
    headerByteBuffer.putInt(5);
    headerByteBuffer.putInt(badOpCode);
    headerByteBuffer.putInt(0);
    headerByteBuffer.putLong(0);
    headerByteBuffer.putInt(0);
    headerByteBuffer.putInt(0);
    headerByteBuffer.flip();
    ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(headerByteBuffer));
    ReplyHeader replyHeader = new ReplyHeader(headerInputBuffer, ConnectionDescription.getDefaultMaxMessageSize());
    new ReplyMessage<Document>(replyHeader, 5);
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 5 with ByteBufNIO

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

the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.

static <T> BsonBinaryReader prepareReaderWithObjectToBeDecoded(final T objectToDecode, final Codec<T> codec) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    byte[] documentAsByteArrayForReader;
    try {
        codec.encode(writer, objectToDecode, EncoderContext.builder().build());
        documentAsByteArrayForReader = outputBuffer.toByteArray();
    } finally {
        writer.close();
    }
    return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

ByteBufNIO (org.bson.ByteBufNIO)10 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)9 ByteBuffer (java.nio.ByteBuffer)6 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)4 BsonBinaryReader (org.bson.BsonBinaryReader)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)3 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BsonDocument (org.bson.BsonDocument)1 ByteBuf (org.bson.ByteBuf)1 Document (org.bson.Document)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1 OutputBuffer (org.bson.io.OutputBuffer)1 JsonReader (org.bson.json.JsonReader)1