Search in sources :

Example 1 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-hadoop by mongodb.

the class BSONWritable method write.

/**
     * {@inheritDoc}
     *
     * @see Writable#write(DataOutput)
     */
public void write(final DataOutput out) throws IOException {
    BSONEncoder enc = new BasicBSONEncoder();
    BasicOutputBuffer buf = new BasicOutputBuffer();
    enc.set(buf);
    enc.putObject(doc);
    enc.done();
    buf.pipe(new DataOutputOutputStreamAdapter(out));
}
Also used : BasicBSONEncoder(org.bson.BasicBSONEncoder) BasicBSONEncoder(org.bson.BasicBSONEncoder) BSONEncoder(org.bson.BSONEncoder) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 2 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-hadoop by mongodb.

the class MongoUpdateWritable method write.

/**
     * /** {@inheritDoc}
     *
     * @see Writable#write(DataOutput)
     */
public void write(final DataOutput out) throws IOException {
    BasicOutputBuffer buf = new BasicOutputBuffer();
    enc.set(buf);
    enc.putObject(query);
    enc.done();
    enc.set(buf);
    enc.putObject(modifiers);
    enc.done();
    buf.pipe(new DataOutputOutputStreamAdapter(out));
    out.writeBoolean(upsert);
    out.writeBoolean(multiUpdate);
    out.writeBoolean(replace);
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 3 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer 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 4 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class GenericBsonTest method encodeToHex.

private String encodeToHex(final BsonDocument decodedDocument) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    new BsonDocumentCodec().encode(new BsonBinaryWriter(outputBuffer), decodedDocument, EncoderContext.builder().build());
    return DatatypeConverter.printHexBinary(outputBuffer.toByteArray());
}
Also used : BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 5 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer 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)

Aggregations

BasicOutputBuffer (org.bson.io.BasicOutputBuffer)29 Test (org.junit.Test)12 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)10 BsonBinaryWriter (org.bson.BsonBinaryWriter)6 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)5 OutputBuffer (org.bson.io.OutputBuffer)5 ByteBufNIO (org.bson.ByteBufNIO)4 BsonBinaryReader (org.bson.BsonBinaryReader)3 Before (org.junit.Before)2 BsonParser (de.undercouch.bson4jackson.BsonParser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteBuffer (java.nio.ByteBuffer)1 BSONEncoder (org.bson.BSONEncoder)1 BasicBSONEncoder (org.bson.BasicBSONEncoder)1 BsonDocument (org.bson.BsonDocument)1 Document (org.bson.Document)1 RawBsonDocument (org.bson.RawBsonDocument)1 JsonReader (org.bson.json.JsonReader)1