@@ -27,31 +27,89 @@ public final class Blob implements Serializable {
2727 private static final long serialVersionUID = 1441087101882481208L ;
2828
2929 private final ByteString byteString ;
30+ private final int subtype ;
3031
31- private Blob (ByteString byteString ) {
32+ private Blob (ByteString byteString , int subtype ) {
33+ if (subtype < 0 || subtype > 255 ) {
34+ throw new IllegalArgumentException (
35+ "The subtype for Blob must be a value in the inclusive [0, 255] range." );
36+ }
3237 this .byteString = byteString ;
38+ this .subtype = subtype ;
3339 }
3440
3541 /**
36- * Creates a new Blob instance from the provided ByteString.
42+ * Creates a new Blob instance from the provided ByteString. Defaults to subtype 0 and native
43+ * representation.
3744 *
3845 * @param byteString The byteString to use for this Blob instance.
3946 * @return The new Blob instance
4047 */
4148 @ Nonnull
4249 public static Blob fromByteString (@ Nonnull ByteString byteString ) {
43- return new Blob (byteString );
50+ return new Blob (byteString , 0 );
4451 }
4552
4653 /**
4754 * Creates a new Blob instance from the provided bytes. Makes a copy of the bytes passed in.
55+ * Defaults to subtype 0 and native representation.
4856 *
4957 * @param bytes The bytes to use for this Blob instance.
5058 * @return The new Blob instance
5159 */
5260 @ Nonnull
5361 public static Blob fromBytes (@ Nonnull byte [] bytes ) {
54- return new Blob (ByteString .copyFrom (bytes ));
62+ return new Blob (ByteString .copyFrom (bytes ), 0 );
63+ }
64+
65+ /**
66+ * Creates a new Blob instance representing a BSON binary data type. Sets subtype to 0 and
67+ * representation to BSON.
68+ *
69+ * @param bytes The bytes to use for this Blob instance.
70+ * @return The new Blob instance
71+ */
72+ @ Nonnull
73+ public static Blob createBsonBinary (@ Nonnull byte [] bytes ) {
74+ return new Blob (ByteString .copyFrom (bytes ), 0 );
75+ }
76+
77+ /**
78+ * Creates a new Blob instance representing a BSON binary data type. Sets subtype to 0 and
79+ * representation to BSON.
80+ *
81+ * @param data The ByteString to use for this Blob instance.
82+ * @return The new Blob instance
83+ */
84+ @ Nonnull
85+ public static Blob createBsonBinary (@ Nonnull ByteString data ) {
86+ return new Blob (data , 0 );
87+ }
88+
89+ /**
90+ * Creates a new Blob instance representing a BSON binary data type with a specific subtype. Sets
91+ * representation to BSON.
92+ *
93+ * @param subtype The subtype to use for this instance.
94+ * @param bytes The bytes to use for this Blob instance.
95+ * @return The new Blob instance
96+ */
97+ @ Nonnull
98+ public static Blob createBsonBinary (int subtype , @ Nonnull byte [] bytes ) {
99+ return new Blob (ByteString .copyFrom (bytes ), subtype );
100+ }
101+
102+ /**
103+ * Creates a new Blob instance representing a BSON binary data type with a specific subtype. Sets
104+ * representation to BSON.
105+ *
106+ * @param subtype The subtype to use for this instance.
107+ * @param data The ByteString to use for this Blob instance.
108+ * @return The new Blob instance
109+ */
110+ @ Nonnull
111+ public static Blob createBsonBinary (int subtype , @ Nonnull ByteString data ) {
112+ return new Blob (data , subtype );
55113 }
56114
57115 /**
@@ -74,6 +132,16 @@ public byte[] toBytes() {
74132 return byteString .toByteArray ();
75133 }
76134
135+ /**
136+ * Returns the subtype of this binary data. Defaults to 0 for both native binary and BSON binary
137+ * if not specified.
138+ *
139+ * @return The subtype of the binary data.
140+ */
141+ public int subtype () {
142+ return this .subtype ;
143+ }
144+
77145 /**
78146 * Returns true if this Blob is equal to the provided object.
79147 *
@@ -89,11 +157,32 @@ public boolean equals(Object obj) {
89157 return false ;
90158 }
91159 Blob blob = (Blob ) obj ;
92- return Objects .equals (byteString , blob .byteString );
160+ return this . subtype == blob . subtype && Objects .equals (byteString , blob .byteString );
93161 }
94162
95163 @ Override
96164 public int hashCode () {
97- return Objects .hash (byteString );
165+ return Objects .hash (byteString , subtype );
166+ }
167+
168+ @ Nonnull
169+ @ Override
170+ public String toString () {
171+ String dataStr ;
172+ if (this .byteString .size () <= 100 ) {
173+ dataStr =
174+ com .google .common .io .BaseEncoding .base16 ()
175+ .lowerCase ()
176+ .encode (this .byteString .toByteArray ());
177+ } else {
178+ dataStr =
179+ com .google .common .io .BaseEncoding .base16 ()
180+ .lowerCase ()
181+ .encode (this .byteString .substring (0 , 20 ).toByteArray ())
182+ + "... (size="
183+ + this .byteString .size ()
184+ + ")" ;
185+ }
186+ return "Blob{subtype=" + this .subtype + ", data=" + dataStr + "}" ;
98187 }
99188}
0 commit comments