Blob

Represents a binary large object.

Conceptually you can consider it as a set of bytes (a picture, a video etc), though in fact it wraps three pieces of information:

  • the set of bytes

  • a name

  • a mime type

API

Blob.java
class Blob {
  Blob(String name, String primaryType, String subtype, byte[] bytes)
  Blob(String name, String mimeTypeBase, byte[] bytes)
  Blob(String name, MimeType mimeType, byte[] bytes)
  Blob of(String name, CommonMimeType mimeType, byte[] content)     (1)
  String getName()
  MimeType getMimeType()
  byte[] getBytes()
  Clob toClob(Charset charset)
  void writeBytesTo(OutputStream os)     (2)
  void consume(Consumer<InputStream> consumer)     (3)
  R digest(Function<InputStream, R> digester)     (4)
  Blob zip()
  Blob unZip(CommonMimeType resultingMimeType)
  boolean equals(Object o)
  int hashCode()
  String toString()
  Optional<BufferedImage> asImage()     (5)
}
1 [of__String_CommonMimeType_byte][of(String, CommonMimeType, byte[])]

Returns a new Blob of given name , mimeType and content .

2 writeBytesTo(OutputStream)

Does not close the OutputStream.

3 consume(Consumer)

The InputStream involved is closed after consumption.

4 digest(Function)

The InputStream involved is closed after digestion.

5 asImage()

Members

of(String, CommonMimeType, byte[])

Returns a new Blob of given name , mimeType and content .

name may or may not include the desired filename extension, it is guaranteed, that the resulting Blob has the appropriate extension as constraint by the given mimeType .

For more fine-grained control use one of the Blob constructors directly.

writeBytesTo(OutputStream)

Does not close the OutputStream.

consume(Consumer)

The InputStream involved is closed after consumption.

digest(Function)

The InputStream involved is closed after digestion.

asImage()

Usage Notes

If using JDO/DataNucleus, Blob properties can be mapped using:

@javax.jdo.annotations.Persistent(defaultFetchGroup="false")
    @javax.jdo.annotations.Persistent(defaultFetchGroup="false", columns = {
            @javax.jdo.annotations.Column(name = "attachment_name"),
            @javax.jdo.annotations.Column(name = "attachment_mimetype"),
            @javax.jdo.annotations.Column(name = "attachment_bytes", jdbcType = "BLOB", sqlType = "BLOB")
    })
@Property(optionality = Optionality.OPTIONAL)
@Getter @Setter
private Blob attachment;

If the property is mandatory, add allowsNull = "false for each of the @Columns.