@@ -27,31 +27,67 @@ 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 from the provided ByteString and subtype. Sets representation to
69+ * BSON.
70+ *
71+ * @param subtype The subtype to use for this instance.
72+ * @param byteString The byteString to use for this Blob instance.
73+ * @return The new Blob instance
74+ */
75+ @ Nonnull
76+ public static Blob fromByteString (int subtype , @ Nonnull ByteString byteString ) {
77+ return new Blob (byteString , subtype , true );
78+ }
79+
80+ /**
81+ * Creates a new Blob instance from the provided bytes and subtype. Makes a copy of the bytes
82+ * passed in. Sets representation to BSON.
83+ *
84+ * @param subtype The subtype to use for this instance.
85+ * @param bytes The bytes to use for this Blob instance.
86+ * @return The new Blob instance
87+ */
88+ @ Nonnull
89+ public static Blob fromBytes (int subtype , @ Nonnull byte [] bytes ) {
90+ return new Blob (ByteString .copyFrom (bytes ), subtype , true );
5591 }
5692
5793 /**
@@ -74,6 +110,24 @@ public byte[] toBytes() {
74110 return byteString .toByteArray ();
75111 }
76112
113+ /**
114+ * Returns the subtype of this binary data.
115+ *
116+ * @return The subtype of the binary data.
117+ */
118+ public int subtype () {
119+ return this .subtype ;
120+ }
121+
122+ /**
123+ * Returns whether this Blob represents a BSON binary data type.
124+ *
125+ * @return True if BSON representation, false if native representation.
126+ */
127+ public boolean isBson () {
128+ return this .isBson ;
129+ }
130+
77131 /**
78132 * Returns true if this Blob is equal to the provided object.
79133 *
@@ -89,11 +143,27 @@ public boolean equals(Object obj) {
89143 return false ;
90144 }
91145 Blob blob = (Blob ) obj ;
92- return Objects .equals (byteString , blob .byteString );
146+ return this . subtype == blob . subtype && Objects .equals (byteString , blob .byteString );
93147 }
94148
95149 @ Override
96150 public int hashCode () {
97- return Objects .hash (byteString );
151+ return Objects .hash (byteString , subtype );
152+ }
153+
154+ com .google .firestore .v1 .MapValue toProto () {
155+ return UserDataConverter .encodeBsonBinaryData (subtype , byteString );
156+ }
157+
158+ @ Nonnull
159+ @ Override
160+ public String toString () {
161+ return "Blob{subtype="
162+ + this .subtype
163+ + ", isBson="
164+ + this .isBson
165+ + ", data="
166+ + this .byteString .toString ()
167+ + "}" ;
98168 }
99169}
0 commit comments