use of org.bson.ByteBufNIO in project mongo-java-driver by mongodb.
the class DocumentCodecTest method createInputBuffer.
// TODO: factor into common base class;
private BsonInput createInputBuffer() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer.pipe(baos);
return new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
}
use of org.bson.ByteBufNIO in project mongo-java-driver by mongodb.
the class MessageHelper method encodeJson.
private static ByteBuf encodeJson(final String json) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
JsonReader jsonReader = new JsonReader(json);
BsonDocumentCodec codec = new BsonDocumentCodec();
BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
codec.encode(writer, document, EncoderContext.builder().build());
ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
documentByteBuffer.put(outputBuffer.toByteArray());
return new ByteBufNIO(documentByteBuffer);
}
use of org.bson.ByteBufNIO 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.ByteBufNIO 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.ByteBufNIO 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