本文整理汇总了Java中org.bson.BsonBinary类的典型用法代码示例。如果您正苦于以下问题:Java BsonBinary类的具体用法?Java BsonBinary怎么用?Java BsonBinary使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BsonBinary类属于org.bson包,在下文中一共展示了BsonBinary类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertBinaryUUID
import org.bson.BsonBinary; //导入依赖的package包/类
/**
* Converts a binary uuid to a standard uuid
*
* @param json The json string (should contain "$binary" and "$type" or something like that)
* @return The uuid
*/
private UUID convertBinaryUUID(String key, String json) {
BsonDocument document = BsonDocument.parse(json);
BsonValue value = document.get(key);
if(!(value instanceof BsonBinary)) {
return null;
}
byte[] bytes = ((BsonBinary) value).getData();
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(ByteOrder.LITTLE_ENDIAN);
long l1 = bb.getLong();
long l2 = bb.getLong();
return new UUID(l1, l2);
}
示例2: getBsonDocument
import org.bson.BsonBinary; //导入依赖的package包/类
public static BsonDocument getBsonDocument() {
BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777));
List<BsonDocument> list = new ArrayList<BsonDocument>();
list.add(bsonObj);
list.add(bsonObj);
byte[] bytes = new byte[3];
bytes[0] = 3;
bytes[1] = 2;
bytes[2] = 1;
BsonDocument bsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99))
.append("testString", new BsonString("testStringV"))
.append("testArray", new BsonArray(list));
return new BsonDocument().append("testDouble", new BsonDouble(20.99))
.append("testString", new BsonString("testStringV"))
.append("testArray", new BsonArray(list))
.append("bson_test", bsonDocument)
.append("testBinary", new BsonBinary(bytes))
.append("testBsonUndefined", new BsonUndefined())
.append("testObjectId", new BsonObjectId())
.append("testStringObjectId", new BsonObjectId())
.append("testBoolean", new BsonBoolean(true))
.append("testDate", new BsonDateTime(time))
.append("testNull", new BsonNull())
.append("testInt", new BsonInt32(233))
.append("testLong", new BsonInt64(233332));
}
示例3: BsonCodecGenerator
import org.bson.BsonBinary; //导入依赖的package包/类
public BsonCodecGenerator(Models models) {
this.models = models;
Elements elems = models.getElemsUtil();
// Reusable things for multiple types
objectIdTypeMirror = elems.getTypeElement(ObjectId.class.getCanonicalName()).asType();
bsonBinaryTypeMirror = elems.getTypeElement(BsonBinary.class.getCanonicalName()).asType();
dateTypeMirror = elems.getTypeElement(Date.class.getCanonicalName()).asType();
instantTypeMirror = elems.getTypeElement(Instant.class.getCanonicalName()).asType();
stringTypeMirror = elems.getTypeElement(String.class.getCanonicalName()).asType();
longTypeMirror = elems.getTypeElement(Long.class.getCanonicalName()).asType();
classClassName = ClassName.get(Class.class);
bsonWriterParamSpec = ParameterSpec.builder(BsonWriter.class, "writer").addModifiers(Modifier.FINAL).build();
bsonReaderParamSpec = ParameterSpec.builder(BsonReader.class, "reader").addModifiers(Modifier.FINAL).build();
encoderCtxParamSpec = ParameterSpec.builder(EncoderContext.class, "ctx").addModifiers(Modifier.FINAL).build();
decoderCtxParamSpec = ParameterSpec.builder(DecoderContext.class, "ctx").addModifiers(Modifier.FINAL).build();
}
示例4: addEncodeStatements
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
public void addEncodeStatements(TypeMirror type, CodeGeneratorContext ctx) {
MethodSpec.Builder builder = ctx.builder();
if (type.getKind() == TypeKind.BYTE) {
builder.addStatement("writer.writeBinaryData(new $T($L))", BsonBinary.class,
ctx.getter());
}
else {
builder.addStatement("$T[] bw = $L", Byte.class, ctx.getter())
.addStatement("byte[] b = new byte[bw.length]")
.beginControlFlow("for (int i = 0; i < bw.length; i++)")
.addStatement("b[i] = bw[i]").endControlFlow()
.addStatement("writer.writeBinaryData(new $T(b))", BsonBinary.class);
}
}
示例5: encode
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
public void encode(BsonWriter writer, FileUploader value, EncoderContext encoderContext) {
writer.writeStartDocument();
ObjectId objectId = new ObjectId();
value.setOid(objectId.toString());
writer.writeObjectId("_id", objectId);
writer.writeBinaryData("file", new BsonBinary(value.getFile()));
writer.writeEndDocument();
}
示例6: encode
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
protected BsonBinary encode(Collection<Byte> value) {
final byte[] data = new byte[value.size()];
final Iterator<Byte> iterator = value.iterator();
int i = 0;
while(iterator.hasNext()) {
data[i++] = iterator.next();
}
return new BsonBinary(data);
}
示例7: decode
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
protected Collection<Byte> decode(BsonBinary bsonValue) {
final Collection<Byte> value = getInstance();
for(byte bsonByte : bsonValue.getData()) {
value.add(bsonByte);
}
return value;
}
示例8: primitiveByteArraySupport
import org.bson.BsonBinary; //导入依赖的package包/类
@Test
public void primitiveByteArraySupport() {
final byte[] value = new byte[1024];
new Random().nextBytes(value);
final BsonBinary bsonValue = new BsonBinary(value);
testSupport(value, bsonValue, byte[].class);
assertArrayEquals(value, parser.fromBson(bsonValue, byte[].class, null));
}
示例9: genericByteArraySupport
import org.bson.BsonBinary; //导入依赖的package包/类
@Test
public void genericByteArraySupport() {
final byte[] value = new byte[1024];
new Random().nextBytes(value);
final BsonBinary bsonValue = new BsonBinary(value);
testSupport(ArrayUtils.toObject(value), bsonValue, Byte[].class);
assertArrayEquals(ArrayUtils.toObject(value), parser.fromBson(bsonValue, Byte[].class, null));
}
示例10: serializeToBson_ShouldSerializePrimitiveByteArray_Successfully
import org.bson.BsonBinary; //导入依赖的package包/类
@Test
public void serializeToBson_ShouldSerializePrimitiveByteArray_Successfully() {
byte[] bytes = "a byte array".getBytes();
BsonValue bsonValue = this.parser.serializeToBson(bytes, null);
byte[] data = ((BsonBinary) bsonValue).getData();
assertEquals(bytes, data);
}
示例11: serializeToBson_ShouldSerializeWrapperByteArray_Successfully
import org.bson.BsonBinary; //导入依赖的package包/类
@Test
public void serializeToBson_ShouldSerializeWrapperByteArray_Successfully() {
byte[] bytes = "a byte array".getBytes();
Byte[] wrappedBytes = ArrayUtils.toObject(bytes);
BsonValue bsonValue = this.parser.serializeToBson(wrappedBytes, null);
byte[] data = ((BsonBinary) bsonValue).getData();
assertArrayEquals(bytes, data);
}
示例12: parseHexDocument
import org.bson.BsonBinary; //导入依赖的package包/类
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;
}
示例13: valueBinary
import org.bson.BsonBinary; //导入依赖的package包/类
public void valueBinary(byte[] data) {
delegate.writeBinaryData(new BsonBinary(data));
}
示例14: decode
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
public Binary decode(BsonValue bsonValue) {
BsonBinary bsonBinary = bsonValue.asBinary();
return new Binary(bsonBinary.getType(), bsonBinary.getData());
}
示例15: encode
import org.bson.BsonBinary; //导入依赖的package包/类
@Override
public BsonBinary encode(Binary object) {
return new BsonBinary(object.getType(), object.getData());
}