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());
}
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());
}
}
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());
}
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;
}
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;
}
}
Aggregations