/** * Equivalent of Long.compareUnsigned in Java 8. * * @param first the first value * @param second the second value * @return 0 if the values are equal, a value greater than zero if first is greater than second, * a value less than zero if first is less than second */ public static int compare(final long first, final long second) { return compareLongs(first + Long.MIN_VALUE, second + Long.MIN_VALUE); }
@Override public int compareTo(final BsonTimestamp ts) { return UnsignedLongs.compare(value, ts.value); }
private String encodeBase64(final byte[] bytes) { return Base64.encode(bytes); }
private byte[] decodeBase64(final String str) { return Base64.decode(str); }
public static byte[] encodeUuidToBinary(final UUID uuid, final UuidRepresentation uuidRepresentation) { byte[] binaryData = new byte[16]; writeLongToArrayBigEndian(binaryData, 0, uuid.getMostSignificantBits()); writeLongToArrayBigEndian(binaryData, 8, uuid.getLeastSignificantBits()); switch(uuidRepresentation) { case C_SHARP_LEGACY: reverseByteArray(binaryData, 0, 4); reverseByteArray(binaryData, 4, 2); reverseByteArray(binaryData, 6, 2); break; case JAVA_LEGACY: reverseByteArray(binaryData, 0, 8); reverseByteArray(binaryData, 8, 8); break; case PYTHON_LEGACY: case STANDARD: break; default: throw new BSONException("Unexpected UUID representation"); } return binaryData; }
/** * Returns the binary as a UUID. The binary type must be 4. * * @return the uuid * @since 3.9 */ public UUID asUuid() { if (!BsonBinarySubType.isUuid(type)) { throw new BsonInvalidOperationException("type must be a UUID subtype."); } if (type != BsonBinarySubType.UUID_STANDARD.getValue()) { throw new BsonInvalidOperationException("uuidRepresentation must be set to return the correct UUID."); } return UuidHelper.decodeBinaryToUuid(this.data.clone(), this.type, UuidRepresentation.STANDARD); }
/** * Construct a new instance from the given UUID and UuidRepresentation * * @param uuid the UUID * @param uuidRepresentation the UUID representation * @since 3.9 */ public BsonBinary(final UUID uuid, final UuidRepresentation uuidRepresentation) { if (uuid == null) { throw new IllegalArgumentException("uuid may not be null"); } if (uuidRepresentation == null) { throw new IllegalArgumentException("uuidRepresentation may not be null"); } this.data = UuidHelper.encodeUuidToBinary(uuid, uuidRepresentation); this.type = uuidRepresentation == UuidRepresentation.STANDARD ? BsonBinarySubType.UUID_STANDARD.getValue() : BsonBinarySubType.UUID_LEGACY.getValue(); }
return byteArrayToString(buffer);
@Override public void convert(final BsonTimestamp value, final StrictJsonWriter writer) { writer.writeStartObject(); writer.writeStartObject("$timestamp"); writer.writeNumber("t", UnsignedLongs.toString(toUnsignedLong(value.getTime()))); writer.writeNumber("i", UnsignedLongs.toString(toUnsignedLong(value.getInc()))); writer.writeEndObject(); writer.writeEndObject(); }
private String encodeBase64(final String str) throws SaslException { return Base64.encode(decodeUTF8(str)); }
private BsonBinary visitBinDataConstructor() { verifyToken(JsonTokenType.LEFT_PAREN); JsonToken subTypeToken = popToken(); if (subTypeToken.getType() != JsonTokenType.INT32) { throw new JsonParseException("JSON reader expected a binary subtype but found '%s'.", subTypeToken.getValue()); } verifyToken(JsonTokenType.COMMA); JsonToken bytesToken = popToken(); if (bytesToken.getType() != JsonTokenType.UNQUOTED_STRING && bytesToken.getType() != JsonTokenType.STRING) { throw new JsonParseException("JSON reader expected a string but found '%s'.", bytesToken.getValue()); } verifyToken(JsonTokenType.RIGHT_PAREN); byte[] bytes = Base64.decode(bytesToken.getValue(String.class)); return new BsonBinary(subTypeToken.getValue(Integer.class).byteValue(), bytes); }
private static long divide(final long dividend, final long divisor) { if (divisor < 0) { // i.e., divisor >= 2^63: if (compare(dividend, divisor) < 0) { return 0; // dividend < divisor } else { return 1; // dividend >= divisor } } // Optimization - use signed division if dividend < 2^63 if (dividend >= 0) { return dividend / divisor; } // Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is // guaranteed to be either exact or one less than the correct value. This follows from fact that // floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not quite // trivial. long quotient = ((dividend >>> 1) / divisor) << 1; long rem = dividend - quotient * divisor; return quotient + (compare(rem, divisor) >= 0 ? 1 : 0); }
@Override public UUID decode(final BsonReader reader, final DecoderContext decoderContext) { byte subType = reader.peekBinarySubType(); if (subType != BsonBinarySubType.UUID_LEGACY.getValue() && subType != BsonBinarySubType.UUID_STANDARD.getValue()) { throw new BSONException("Unexpected BsonBinarySubType"); } byte[] bytes = reader.readBinaryData().getData(); return UuidHelper.decodeBinaryToUuid(bytes, subType, decoderUuidRepresentation); }
@Override public void encode(final BsonWriter writer, final UUID value, final EncoderContext encoderContext) { byte[] binaryData = UuidHelper.encodeUuidToBinary(value, encoderUuidRepresentation); // changed the default subtype to STANDARD since 3.0 if (encoderUuidRepresentation == UuidRepresentation.STANDARD) { writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, binaryData)); } else { writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_LEGACY, binaryData)); } }
protected void serialize(final byte[] bytes, final byte type, final StringBuilder buf) { DBObject temp = new BasicDBObject(); temp.put("$binary", Base64.encode(bytes)); temp.put("$type", type); serializer.serialize(temp, buf); } }
private static long remainder(final long dividend, final long divisor) { if (divisor < 0) { // i.e., divisor >= 2^63: if (compare(dividend, divisor) < 0) { return dividend; // dividend < divisor } else { return dividend - divisor; // dividend >= divisor } } // Optimization - use signed modulus if dividend < 2^63 if (dividend >= 0) { return dividend % divisor; } // Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is // guaranteed to be either exact or one less than the correct value. This follows from the fact // that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not // quite trivial. long quotient = ((dividend >>> 1) / divisor) << 1; long rem = dividend - quotient * divisor; return rem - (compare(rem, divisor) >= 0 ? divisor : 0); }
/** * Returns the binary as a UUID. * * @param uuidRepresentation the UUID representation * @return the uuid * @since 3.9 */ public UUID asUuid(final UuidRepresentation uuidRepresentation) { Assertions.notNull("uuidRepresentation", uuidRepresentation); final byte uuidType = uuidRepresentation == UuidRepresentation.STANDARD ? BsonBinarySubType.UUID_STANDARD.getValue() : BsonBinarySubType.UUID_LEGACY.getValue(); if (type != uuidType) { throw new BsonInvalidOperationException("uuidRepresentation does not match current uuidRepresentation."); } return UuidHelper.decodeBinaryToUuid(data.clone(), type, uuidRepresentation); }
@Override public void convert(final BsonBinary value, final StrictJsonWriter writer) { writer.writeRaw(format("new BinData(%s, \"%s\")", Integer.toString(value.getType() & 0xFF), Base64.encode(value.getData()))); } }
@Override public void convert(final BsonBinary value, final StrictJsonWriter writer) { writer.writeStartObject(); writer.writeString("$binary", Base64.encode(value.getData())); writer.writeString("$type", String.format("%02X", value.getType())); writer.writeEndObject(); } }
@Override public void convert(final BsonBinary value, final StrictJsonWriter writer) { writer.writeStartObject(); writer.writeStartObject("$binary"); writer.writeString("base64", Base64.encode(value.getData())); writer.writeString("subType", String.format("%02X", value.getType())); writer.writeEndObject(); writer.writeEndObject(); } }