Class Blob

  • All Implemented Interfaces:
    com.couchbase.lite.internal.fleece.FLEncodable

    public final class Blob
    extends java.lang.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).

    • Constructor Summary

      Constructors 
      Constructor Description
      Blob​(java.lang.String contentType, byte[] content)
      Construct a Blob with the given in-memory data.
      Blob​(java.lang.String contentType, java.io.InputStream stream)
      Construct a Blob with the given stream of data.
      Blob​(java.lang.String contentType, java.net.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
      java.lang.String digest()
      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 equals​(java.lang.Object o)
      Compare for equality.
      byte[] getContent()
      Gets the contents of this blob as in in-memory byte array.
      java.io.InputStream getContentStream()
      Get a the contents of this blob as a stream.
      java.lang.String getContentType()
      Return the type of of the content this blob contains.
      java.util.Map<java.lang.String,​java.lang.Object> getProperties()
      Get the blob metadata
      int hashCode()
      Get the blob hash code.
      static boolean isBlob​(java.util.Map<java.lang.String,​?> props)  
      long length()
      The number of byte of content this blob contains.
      java.lang.String toJSON()  
      java.lang.String toString()
      Returns a string representation of the object.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ENCODER_ARG_QUERY_PARAM

        public static final java.lang.String ENCODER_ARG_QUERY_PARAM
        See Also:
        Constant Field Values
    • Constructor Detail

      • Blob

        public Blob​(@NonNull
                    java.lang.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
                    java.lang.String contentType,
                    @NonNull
                    java.io.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
                    java.lang.String contentType,
                    @NonNull
                    java.net.URL fileURL)
             throws java.io.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:
        java.io.IOException - on failure to open the file URL
    • Method Detail

      • isBlob

        public static boolean isBlob​(@Nullable
                                     java.util.Map<java.lang.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 java.io.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 java.lang.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 java.lang.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 java.lang.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 java.util.Map<java.lang.String,​java.lang.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 java.lang.String toString()
        Returns a string representation of the object.
        Overrides:
        toString in class java.lang.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 java.lang.Object
        Returns:
        hash code for the object
      • equals

        public boolean equals​(java.lang.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 java.lang.Object
        Returns:
        true if this object is the same as that one.