use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoDocument.
@Test
public void testPipeDocumentIntoDocument() {
writer.writeStartDocument();
writer.writeString("str", "value");
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.writeName("doc");
newWriter.pipe(reader1);
newWriter.writeEndDocument();
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
assertEquals("doc", reader2.readName());
reader2.readStartDocument();
assertEquals("value", reader2.readString("str"));
reader2.readEndDocument();
reader2.readEndDocument();
}
use of org.bson.io.ByteBufferBsonInput 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.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoScopeDocument.
@Test
public void testPipeDocumentIntoScopeDocument() {
writer.writeStartDocument();
writer.writeInt32("i", 0);
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.writeJavaScriptWithScope("js", "i++");
newWriter.pipe(reader1);
newWriter.writeEndDocument();
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
reader2.readJavaScriptWithScope("js");
reader2.readStartDocument();
assertEquals(0, reader2.readInt32("i"));
reader2.readEndDocument();
reader2.readEndDocument();
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeOfDocumentWithInvalidSize.
@Test
public void testPipeOfDocumentWithInvalidSize() {
// minimum document size is 5;
byte[] bytes = { 4, 0, 0, 0 };
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
try {
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
try {
newWriter.pipe(reader);
fail("Pipe is expected to fail with document size is < 5");
} catch (BsonSerializationException e) {
// expected
} finally {
reader.close();
}
} finally {
newWriter.close();
}
}
use of org.bson.io.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class CommandProtocol method execute.
@Override
public T execute(final InternalConnection connection) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Sending command {%s : %s} to database %s on connection [%s] to server %s", getCommandName(), command.values().iterator().next(), namespace.getDatabaseName(), connection.getDescription().getConnectionId(), connection.getDescription().getServerAddress()));
}
long startTimeNanos = System.nanoTime();
CommandMessage commandMessage = new CommandMessage(namespace.getFullName(), command, slaveOk, fieldNameValidator, ProtocolHelper.getMessageSettings(connection.getDescription()));
ResponseBuffers responseBuffers = null;
try {
sendMessage(commandMessage, connection);
responseBuffers = connection.receiveMessage(commandMessage.getId());
if (!ProtocolHelper.isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())))) {
throw getCommandFailureException(getResponseDocument(responseBuffers, commandMessage, new BsonDocumentCodec()), connection.getDescription().getServerAddress());
}
T retval = getResponseDocument(responseBuffers, commandMessage, commandResultDecoder);
if (commandListener != null) {
sendSucceededEvent(connection.getDescription(), startTimeNanos, commandMessage, getResponseDocument(responseBuffers, commandMessage, new RawBsonDocumentCodec()));
}
LOGGER.debug("Command execution completed");
return retval;
} catch (RuntimeException e) {
sendFailedEvent(connection.getDescription(), startTimeNanos, commandMessage, e);
throw e;
} finally {
if (responseBuffers != null) {
responseBuffers.close();
}
}
}
Aggregations