Search in sources :

Example 6 with ByteBufNIO

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())));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufNIO(org.bson.ByteBufNIO) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 7 with ByteBufNIO

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);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 8 with ByteBufNIO

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());
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 9 with ByteBufNIO

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);
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 10 with ByteBufNIO

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());
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) 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