Search in sources :

Example 41 with BsonBinary

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

the class JsonReaderTest method testBinaryWithNumericType.

@Test
public void testBinaryWithNumericType() {
    String json = "{ \"$binary\" : \"AQID\", \"$type\" : 0 }";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.BINARY, bsonReader.readBsonType());
    BsonBinary binary = bsonReader.readBinaryData();
    assertEquals(BsonBinarySubType.BINARY.getValue(), binary.getType());
    assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
    assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Also used : BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 42 with BsonBinary

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

the class MaxDocumentSizeTest method setUp.

@Before
public void setUp() {
    message = new InsertMessage("test.test", true, ACKNOWLEDGED, asList(new InsertRequest(new BsonDocument("bytes", new BsonBinary(new byte[2048])))), MessageSettings.builder().maxDocumentSize(1024).build());
    buffer = new ByteBufferBsonOutput(new SimpleBufferProvider());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) InsertRequest(com.mongodb.bulk.InsertRequest) Before(org.junit.Before)

Example 43 with BsonBinary

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

the class MaxMessageSizeTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    BsonBinary binary = new BsonBinary(new byte[2048]);
    message = new InsertMessage("test.test", true, ACKNOWLEDGED, Arrays.asList(new InsertRequest(new BsonDocument("bytes", binary)), new InsertRequest(new BsonDocument("bytes", binary)), new InsertRequest(new BsonDocument("bytes", binary))), MessageSettings.builder().maxMessageSize(4500).build());
    buffer = new ByteBufferBsonOutput(new SimpleBufferProvider());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) InsertRequest(com.mongodb.bulk.InsertRequest) Before(org.junit.Before)

Example 44 with BsonBinary

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

the class GridFSTest method parseHexDocument.

private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
Also used : BsonBinary(org.bson.BsonBinary)

Example 45 with BsonBinary

use of org.bson.BsonBinary in project drill by axbaretto.

the class BsonRecordReader method writeBinary.

private void writeBinary(BsonReader reader, final MapOrListWriterImpl writer, String fieldName, boolean isList) {
    final VarBinaryHolder vb = new VarBinaryHolder();
    BsonBinary readBinaryData = reader.readBinaryData();
    byte[] data = readBinaryData.getData();
    Byte type = (Byte) readBinaryData.getType();
    // Based on specified binary type, cast it accordingly
    switch(type.intValue()) {
        case 1:
            // Double 1
            writeDouble(ByteBuffer.wrap(data).getDouble(), writer, fieldName, isList);
            break;
        case 2:
            // String 2
            writeString(new String(data), writer, fieldName, isList);
            break;
        case 8:
            // Boolean 8
            boolean boolValue = (data == null || data.length == 0) ? false : data[0] != 0x00;
            writeBoolean(boolValue, writer, fieldName, isList);
            break;
        case 9:
            // Date 9
            writeDateTime(ByteBuffer.wrap(data).getLong(), writer, fieldName, isList);
            break;
        case 13:
            // JavaScript 13
            writeString(new String(data), writer, fieldName, isList);
            break;
        case 14:
            // Symbol 14
            writeString(new String(data), writer, fieldName, isList);
            break;
        case 15:
            // JavaScript (with scope) 15
            writeString(new String(data), writer, fieldName, isList);
            break;
        case 16:
            // 32-bit integer 16
            writeInt32(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
            break;
        case 17:
            // Timestamp 17
            writeTimeStamp(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
            break;
        case 18:
            // 64-bit integer 18
            writeInt64(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList);
            break;
        default:
            // In case of Object(3)/Binary data (5)/Object id (7) or in other case
            // considering as VarBinary
            final byte[] bytes = readBinaryData.getData();
            writeBinary(writer, fieldName, isList, vb, bytes);
            break;
    }
}
Also used : VarBinaryHolder(org.apache.drill.exec.expr.holders.VarBinaryHolder) BsonBinary(org.bson.BsonBinary)

Aggregations

BsonBinary (org.bson.BsonBinary)78 Test (org.junit.Test)51 BsonDocument (org.bson.BsonDocument)32 BsonString (org.bson.BsonString)16 Document (org.bson.Document)16 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)13 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)11 BSONException (org.bson.BSONException)9 Map (java.util.Map)8 BasicDBObject (com.mongodb.BasicDBObject)6 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 MongoClientSettings (com.mongodb.MongoClientSettings)6 SecureRandom (java.security.SecureRandom)6 HashMap (java.util.HashMap)6 ClientEncryption (com.mongodb.client.vault.ClientEncryption)5 UUID (java.util.UUID)5 Binary (org.bson.types.Binary)5 MongoNamespace (com.mongodb.MongoNamespace)4 IndexOptions (com.mongodb.client.model.IndexOptions)4