Search in sources :

Example 21 with ByteBufferBsonInput

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

the class BsonBinaryWriterTest method testPipeDocumentIntoArray.

@Test
public void testPipeDocumentIntoArray() {
    writer.writeStartDocument();
    writer.writeEndDocument();
    byte[] bytes = buffer.toByteArray();
    BasicOutputBuffer newBuffer = new BasicOutputBuffer();
    BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
    BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
    newWriter.writeStartDocument();
    newWriter.writeStartArray("a");
    newWriter.pipe(reader1);
    newWriter.writeEndArray();
    newWriter.writeEndDocument();
    BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
    //checking what writer piped
    reader2.readStartDocument();
    reader2.readStartArray();
    reader2.readStartDocument();
    reader2.readEndDocument();
    reader2.readEndArray();
    reader2.readEndDocument();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 22 with ByteBufferBsonInput

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

the class MessageHelper method buildReplyHeader.

private static ReplyHeader buildReplyHeader(final int responseTo, final int numDocuments, final int documentsSize, final int responseFlags) {
    ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
    headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    // length
    headerByteBuffer.putInt(36 + documentsSize);
    //request id
    headerByteBuffer.putInt(2456);
    // response to
    headerByteBuffer.putInt(responseTo);
    // opcode
    headerByteBuffer.putInt(1);
    // responseFlags
    headerByteBuffer.putInt(responseFlags);
    // cursorId
    headerByteBuffer.putLong(0);
    // startingFrom
    headerByteBuffer.putInt(0);
    //numberReturned
    headerByteBuffer.putInt(numDocuments);
    headerByteBuffer.flip();
    ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(headerByteBuffer));
    return new ReplyHeader(headerInputBuffer, ConnectionDescription.getDefaultMaxMessageSize());
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 23 with ByteBufferBsonInput

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

the class ReplyMessageTest method shouldThrowExceptionIfRequestIdDoesNotMatchResponseTo.

@Test(expected = MongoInternalException.class)
public void shouldThrowExceptionIfRequestIdDoesNotMatchResponseTo() {
    int badResponseTo = 34565;
    int expectedResponseTo = 5;
    ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
    headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    headerByteBuffer.putInt(36);
    headerByteBuffer.putInt(2456);
    headerByteBuffer.putInt(badResponseTo);
    headerByteBuffer.putInt(1);
    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, expectedResponseTo);
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 24 with ByteBufferBsonInput

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

the class TestInternalConnection method replaceResponseTo.

private ReplyHeader replaceResponseTo(final ReplyHeader header, final int responseTo) {
    ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
    headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    headerByteBuffer.putInt(header.getMessageLength());
    headerByteBuffer.putInt(header.getRequestId());
    headerByteBuffer.putInt(responseTo);
    headerByteBuffer.putInt(1);
    headerByteBuffer.putInt(header.getResponseFlags());
    headerByteBuffer.putLong(header.getCursorId());
    headerByteBuffer.putInt(header.getStartingFrom());
    headerByteBuffer.putInt(header.getNumberReturned());
    headerByteBuffer.flip();
    ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(headerByteBuffer));
    return new ReplyHeader(headerInputBuffer, ConnectionDescription.getDefaultMaxMessageSize());
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) 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