Search in sources :

Example 1 with ByteBufferBsonInput

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

Example 2 with ByteBufferBsonInput

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;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) Document(org.bson.Document) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 3 with ByteBufferBsonInput

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

Example 4 with ByteBufferBsonInput

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

Example 5 with ByteBufferBsonInput

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();
        }
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)24 Test (org.junit.Test)11 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)10 ByteBufNIO (org.bson.ByteBufNIO)9 BsonBinaryReader (org.bson.BsonBinaryReader)7 ByteBuffer (java.nio.ByteBuffer)5 ByteBuf (org.bson.ByteBuf)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 StringWriter (java.io.StringWriter)1 Document (org.bson.Document)1 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)1 JsonWriter (org.bson.json.JsonWriter)1 ObjectId (org.bson.types.ObjectId)1