@@ -27,31 +27,91 @@ public final class Blob implements Serializable {
2727 private static final long serialVersionUID = 1441087101882481208L ;
2828
2929 private final ByteString byteString ;
30+ private final int subtype ;
31+ private final boolean isBson ;
3032
31- private Blob (ByteString byteString ) {
33+ private Blob (ByteString byteString , int subtype , boolean isBson ) {
34+ if (subtype < 0 || subtype > 255 ) {
35+ throw new IllegalArgumentException (
36+ "The subtype for Blob must be a value in the inclusive [0, 255] range." );
37+ }
3238 this .byteString = byteString ;
39+ this .subtype = subtype ;
40+ this .isBson = isBson ;
3341 }
3442
3543 /**
36- * Creates a new Blob instance from the provided ByteString.
44+ * Creates a new Blob instance from the provided ByteString. Defaults to subtype 0 and native
45+ * representation.
3746 *
3847 * @param byteString The byteString to use for this Blob instance.
3948 * @return The new Blob instance
4049 */
4150 @ Nonnull
4251 public static Blob fromByteString (@ Nonnull ByteString byteString ) {
43- return new Blob (byteString );
52+ return new Blob (byteString , 0 , false );
4453 }
4554
4655 /**
4756 * Creates a new Blob instance from the provided bytes. Makes a copy of the bytes passed in.
57+ * Defaults to subtype 0 and native representation.
4858 *
4959 * @param bytes The bytes to use for this Blob instance.
5060 * @return The new Blob instance
5161 */
5262 @ Nonnull
5363 public static Blob fromBytes (@ Nonnull byte [] bytes ) {
54- return new Blob (ByteString .copyFrom (bytes ));
64+ return new Blob (ByteString .copyFrom (bytes ), 0 , false );
65+ }
66+
67+ /**
68+ * Creates a new Blob instance representing a BSON binary data type. Sets subtype to 0 and
69+ * representation to BSON.
70+ *
71+ * @param bytes The bytes to use for this Blob instance.
72+ * @return The new Blob instance
73+ */
74+ @ Nonnull
75+ public static Blob createBsonBinary (@ Nonnull byte [] bytes ) {
76+ return new Blob (ByteString .copyFrom (bytes ), 0 , true );
77+ }
78+
79+ /**
80+ * Creates a new Blob instance representing a BSON binary data type. Sets subtype to 0 and
81+ * representation to BSON.
82+ *
83+ * @param data The ByteString to use for this Blob instance.
84+ * @return The new Blob instance
85+ */
86+ @ Nonnull
87+ public static Blob createBsonBinary (@ Nonnull ByteString data ) {
88+ return new Blob (data , 0 , true );
89+ }
90+
91+ /**
92+ * Creates a new Blob instance representing a BSON binary data type with a specific subtype. Sets
93+ * representation to BSON.
94+ *
95+ * @param subtype The subtype to use for this instance.
96+ * @param bytes The bytes to use for this Blob instance.
97+ * @return The new Blob instance
98+ */
99+ @ Nonnull
100+ public static Blob createBsonBinary (int subtype , @ Nonnull byte [] bytes ) {
101+ return new Blob (ByteString .copyFrom (bytes ), subtype , true );
102+ }
103+
104+ /**
105+ * Creates a new Blob instance representing a BSON binary data type with a specific subtype. Sets
106+ * representation to BSON.
107+ *
108+ * @param subtype The subtype to use for this instance.
109+ * @param data The ByteString to use for this Blob instance.
110+ * @return The new Blob instance
111+ */
112+ @ Nonnull
113+ public static Blob createBsonBinary (int subtype , @ Nonnull ByteString data ) {
114+ return new Blob (data , subtype , true );
55115 }
56116
57117 /**
@@ -74,6 +134,25 @@ public byte[] toBytes() {
74134 return byteString .toByteArray ();
75135 }
76136
137+ /**
138+ * Returns the subtype of this binary data. Defaults to 0 for both native binary and BSON binary
139+ * if not specified.
140+ *
141+ * @return The subtype of the binary data.
142+ */
143+ public int subtype () {
144+ return this .subtype ;
145+ }
146+
147+ /**
148+ * Returns whether this Blob represents a BSON binary data type.
149+ *
150+ * @return True if BSON representation, false if native representation.
151+ */
152+ public boolean isBson () {
153+ return this .isBson ;
154+ }
155+
77156 /**
78157 * Returns true if this Blob is equal to the provided object.
79158 *
@@ -89,11 +168,29 @@ public boolean equals(Object obj) {
89168 return false ;
90169 }
91170 Blob blob = (Blob ) obj ;
92- return Objects .equals (byteString , blob .byteString );
171+ return this . subtype == blob . subtype && Objects .equals (byteString , blob .byteString );
93172 }
94173
95174 @ Override
96175 public int hashCode () {
97- return Objects .hash (byteString );
176+ return Objects .hash (byteString , subtype );
177+ }
178+
179+ com .google .firestore .v1 .MapValue toProto () {
180+ return UserDataConverter .encodeBsonBinaryData (subtype , byteString );
181+ }
182+
183+ @ Nonnull
184+ @ Override
185+ public String toString () {
186+ return "Blob{subtype="
187+ + this .subtype
188+ + ", isBson="
189+ + this .isBson
190+ + ", data="
191+ + com .google .common .io .BaseEncoding .base16 ()
192+ .lowerCase ()
193+ .encode (this .byteString .toByteArray ())
194+ + "}" ;
98195 }
99196}
0 commit comments