[Java] ObjectID::fromRandom sets proper flags (#5548)

This commit is contained in:
Kai Yang
2019-08-28 11:31:06 +08:00
committed by Hao Chen
parent ddfababb82
commit fadfa5f30b
2 changed files with 10 additions and 13 deletions
@@ -12,8 +12,6 @@ public class ObjectId extends BaseId implements Serializable {
public static final int LENGTH = 20;
public static final ObjectId NIL = genNil();
/**
* Create an ObjectId from a ByteBuffer.
*/
@@ -21,21 +19,17 @@ public class ObjectId extends BaseId implements Serializable {
return new ObjectId(byteBuffer2Bytes(bb));
}
/**
* Generate a nil ObjectId.
*/
private static ObjectId genNil() {
byte[] b = new byte[LENGTH];
Arrays.fill(b, (byte) 0xFF);
return new ObjectId(b);
}
/**
* Generate an ObjectId with random value.
*/
public static ObjectId fromRandom() {
// This is tightly coupled with ObjectID definition in C++. If that changes,
// this must be changed as well.
// The following logic should be kept consistent with `ObjectID::FromRandom` in
// C++.
byte[] b = new byte[LENGTH];
new Random().nextBytes(b);
Arrays.fill(b, TaskId.LENGTH, LENGTH, (byte) 0);
return new ObjectId(b);
}