Search in sources :

Example 1 with BsonBinary

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

the class JsonReaderTest method testUserDefinedBinary.

@Test
public void testUserDefinedBinary() {
    String json = "{ \"$binary\" : \"AQID\", \"$type\" : \"80\" }";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.BINARY, bsonReader.readBsonType());
    BsonBinary binary = bsonReader.readBinaryData();
    assertEquals(BsonBinarySubType.USER_DEFINED.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 2 with BsonBinary

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

the class JsonWriterTest method testBinaryStrict.

@Test
@SuppressWarnings("deprecation")
public void testBinaryStrict() {
    List<TestData<BsonBinary>> tests = asList(new TestData<BsonBinary>(new BsonBinary(new byte[0]), "{ \"$binary\" : \"\", " + "\"$type\" : \"00\" }"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1 }), "{ \"$binary\" : \"AQ==\", " + "\"$type\" : \"00\" }"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2 }), "{ \"$binary\" : \"AQI=\", " + "\"$type\" : \"00\" }"), new TestData<BsonBinary>(new BsonBinary(new byte[] { 1, 2, 3 }), "{ \"$binary\" : \"AQID\", " + "\"$type\" : \"00\" }"), new TestData<BsonBinary>(new BsonBinary((byte) 0x80, new byte[] { 1, 2, 3 }), "{ \"$binary\" : \"AQID\", " + "\"$type\" : \"80\" }"));
    for (final TestData<BsonBinary> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build());
        writer.writeStartDocument();
        writer.writeBinaryData("binary", cur.value);
        writer.writeEndDocument();
        String expected = "{ \"binary\" : " + cur.expected + " }";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Example 3 with BsonBinary

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

the class JsonReaderTest method testUserDefinedBinaryWithNumericType.

@Test
public void testUserDefinedBinaryWithNumericType() {
    String json = "{ \"$binary\" : \"AQID\", \"$type\" : 128 }";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.BINARY, bsonReader.readBsonType());
    BsonBinary binary = bsonReader.readBinaryData();
    assertEquals(BsonBinarySubType.USER_DEFINED.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 4 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 5 with BsonBinary

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

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