Package com.couchbase.lite
Class Blob
- java.lang.Object
-
- com.couchbase.lite.Blob
-
- All Implemented Interfaces:
FLEncodable
public final class Blob extends Object implements FLEncodable
A Couchbase Lite Blob. A Blob appears as a property of a Document and contains arbitrary binary data, tagged with MIME type. Blobs can be arbitrarily large, although some operations may require that the entire content be loaded into memory. The containing document's JSON contains only the Blob's metadata (type, length and digest). The data itself is stored in a file whose name is the content digest (like git).
-
-
Field Summary
Fields Modifier and Type Field Description static String
ENCODER_ARG_DB
static String
ENCODER_ARG_QUERY_PARAM
-
Constructor Summary
Constructors Constructor Description Blob(String contentType, byte[] content)
Construct a Blob with the given in-memory data.Blob(String contentType, InputStream stream)
Construct a Blob with the given stream of data.Blob(String contentType, URL fileURL)
Construct a Blob with the content of a file.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
digest()
The cryptographic digest of this Blob's contents, which uniquely identifies it.void
encodeTo(FLEncoder encoder)
This method is not part of the public API: Do not use it.boolean
equals(Object o)
Compare for equality.byte[]
getContent()
Gets the contents of this blob as in in-memory byte array.InputStream
getContentStream()
Get a the contents of this blob as a stream.String
getContentType()
Return the type of of the content this blob contains.Map<String,Object>
getProperties()
Get the blob metadataint
hashCode()
Get the blob hash code.static boolean
isBlob(Map<String,?> props)
long
length()
The number of byte of content this blob contains.String
toJSON()
String
toString()
Returns a string representation of the object.
-
-
-
Field Detail
-
ENCODER_ARG_DB
public static final String ENCODER_ARG_DB
- See Also:
- Constant Field Values
-
ENCODER_ARG_QUERY_PARAM
public static final String ENCODER_ARG_QUERY_PARAM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Blob
public Blob(@NonNull String contentType, @NonNull byte[] content)
Construct a Blob with the given in-memory data.- Parameters:
contentType
- The type of content this Blob will representcontent
- The data that this Blob will contain
-
Blob
public Blob(@NonNull String contentType, @NonNull InputStream stream)
Construct a Blob with the given stream of data. The passed stream will be closed when it is copied either to memory (seegetContent
) or to the database. If it is closed before that, by client code, the attempt to store the blob will fail. The converse is also true: the stream for a blob that is not saved or copied to memory will not be closed (except during garbage collection).- Parameters:
contentType
- The type of content this Blob will representstream
- The stream of data that this Blob will consume
-
Blob
public Blob(@NonNull String contentType, @NonNull URL fileURL) throws IOException
Construct a Blob with the content of a file. The blob can then be added as a property of a Document. This constructor creates a stream that is not closed until the blob is stored in the db, or copied to memory (except by garbage collection).- Parameters:
contentType
- The type of content this Blob will representfileURL
- A URL to a file containing the data that this Blob will represent.- Throws:
IOException
- on failure to open the file URL
-
-
Method Detail
-
getContent
@Nullable public byte[] getContent()
Gets the contents of this blob as in in-memory byte array. Using this method will cause the entire contents of the blob to be read into memory!- Returns:
- the contents of a Blob as a block of memory
-
getContentStream
@Nullable public InputStream getContentStream()
Get a the contents of this blob as a stream. The caller is responsible for closing the stream returned by this call. Closing or deleting the database before this call completes may cause it to fail. When called on a blob created from a stream (or a file path), this method will return null!- Returns:
- a stream of of this blobs contents; null if none exists or if this blob was initialized with a stream
-
getContentType
@NonNull public String getContentType()
Return the type of of the content this blob contains. By convention this is a MIME type.- Returns:
- the type of blobContent
-
toJSON
@NonNull public String toJSON()
-
length
public long length()
The number of byte of content this blob contains.- Returns:
- The length of the blob or 0 if initialized with a stream.
-
digest
@Nullable public String digest()
The cryptographic digest of this Blob's contents, which uniquely identifies it.- Returns:
- The cryptographic digest of this blob's contents; null if the content has not been saved in a database
-
getProperties
@NonNull public Map<String,Object> getProperties()
Get the blob metadata- Returns:
- metadata for this Blob
-
encodeTo
@Volatile public void encodeTo(@NonNull FLEncoder encoder)
This method is not part of the public API: Do not use it.- Specified by:
encodeTo
in interfaceFLEncodable
- Parameters:
encoder
- The FLEncoder to which to encode this object.
-
toString
@NonNull public String toString()
Returns a string representation of the object.
-
hashCode
public int hashCode()
Get the blob hash code. This method is quite expensive. Also, when called on a blob created from a stream (or a file path), it will cause the entire contents of that stream to be read into memory!
-
equals
public boolean equals(Object o)
Compare for equality. This method is quite expensive. Also, when called on a blob created from a stream (or a file path), it will cause the entire contents of that stream to be read into memory!
-
-