Package com.couchbase.lite
Class Blob
java.lang.Object
com.couchbase.lite.Blob
- All Implemented Interfaces:
com.couchbase.lite.internal.fleece.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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondigest()
The cryptographic digest of this Blob's contents, which uniquely identifies it.void
encodeTo
(com.couchbase.lite.internal.fleece.FLEncoder encoder) This method is not part of the public API: Do not use it.boolean
Compare for equality.byte[]
Gets the contents of this blob as in in-memory byte array.Get a the contents of this blob as a stream.Return the type of of the content this blob contains.Get the blob metadataint
hashCode()
Get the blob hash code.static boolean
long
length()
The number of byte of content this blob contains.toJSON()
toString()
Returns a string representation of the object.
-
Field Details
-
ENCODER_ARG_DB
- See Also:
-
ENCODER_ARG_QUERY_PARAM
- See Also:
-
-
Constructor Details
-
Blob
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
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
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 Details
-
isBlob
-
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
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
Return the type of of the content this blob contains. By convention this is a MIME type.- Returns:
- the type of blobContent
-
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
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
Get the blob metadata- Returns:
- metadata for this Blob
-
encodeTo
@Volatile public void encodeTo(@NonNull com.couchbase.lite.internal.fleece.FLEncoder encoder) This method is not part of the public API: Do not use it.- Specified by:
encodeTo
in interfacecom.couchbase.lite.internal.fleece.FLEncodable
- Parameters:
encoder
- The FLEncoder to which to encode this object.
-
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
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!
-