Search in sources :

Example 11 with BasicOutputBuffer

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

the class BSONTest method shouldClearCustomEncoders.

@Test(expected = IllegalArgumentException.class)
public void shouldClearCustomEncoders() throws IOException {
    // given
    BSON.addEncodingHook(TestDate.class, new TestDateTransformer());
    BSONEncoder encoder = new BasicBSONEncoder();
    encoder.set(new BasicOutputBuffer());
    // when
    BSON.clearEncodingHooks();
    encoder.putObject(new BasicBSONObject("date", new TestDate(2009, 1, 23, 10, 53, 42)));
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Test(org.junit.Test)

Example 12 with BasicOutputBuffer

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

the class BSONTest method testOBBig1.

@Test
public void testOBBig1() {
    BasicOutputBuffer a = new BasicOutputBuffer();
    StringBuilder b = new StringBuilder();
    for (final String x : prepareData()) {
        a.write(x.getBytes());
        b.append(x);
    }
    assertEquals(new String(a.toByteArray(), Charset.forName("UTF-8")), b.toString());
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Test(org.junit.Test)

Example 13 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project immutables by immutables.

the class BsonEncoding method unmarshalDbObject.

public static <T> T unmarshalDbObject(DBObject dbObject, TypeAdapter<T> adaper) throws IOException {
    BasicOutputBuffer buffer = new BasicOutputBuffer();
    encoder().writeObject(buffer, dbObject);
    BsonParser parser = BSON_FACTORY.createParser(buffer.toByteArray());
    BsonReader reader = new BsonReader(parser);
    T instance = adaper.read(reader);
    reader.close();
    return instance;
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 14 with BasicOutputBuffer

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

the class ClientMetadataHelper method clientMetadataDocumentTooLarge.

static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
    BasicOutputBuffer buffer = new BasicOutputBuffer(MAXIMUM_CLIENT_METADATA_ENCODED_SIZE);
    new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
    return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 15 with BasicOutputBuffer

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

the class RawBsonDocumentCodec method decode.

@Override
public RawBsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
    BasicOutputBuffer buffer = new BasicOutputBuffer(0);
    BsonBinaryWriter writer = new BsonBinaryWriter(buffer);
    try {
        writer.pipe(reader);
        return new RawBsonDocument(buffer.getInternalBuffer(), 0, buffer.getPosition());
    } finally {
        writer.close();
        buffer.close();
    }
}
Also used : RawBsonDocument(org.bson.RawBsonDocument) BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

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