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();
}
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());
}
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);
}
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());
}
Aggregations