Class Blob

java.lang.Object
com.couchbase.lite.Blob
All Implemented Interfaces:
com.couchbase.lite.internal.fleece.FLEncodable

public final class Blob extends Object implements 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 Details

  • Constructor Details

    • 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 represent
      content - 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 (see getContent) 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 represent
      stream - 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 represent
      fileURL - 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

      public static boolean isBlob(@Nullable Map<String,?> props)
    • 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 com.couchbase.lite.internal.fleece.FLEncoder encoder)
      This method is not part of the public API: Do not use it.
      Specified by:
      encodeTo in interface com.couchbase.lite.internal.fleece.FLEncodable
      Parameters:
      encoder - The FLEncoder to which to encode this object.
    • toString

      @NonNull public String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class Object
      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!
      Overrides:
      hashCode in class Object
      Returns:
      hash code for the object
    • 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!
      Overrides:
      equals in class Object
      Returns:
      true if this object is the same as that one.