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