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)
  Try<Blob> tryRead(String name, CommonMimeType mimeType, DataSource dataSource)     (2)
  Try<Blob> tryRead(String name, CommonMimeType mimeType, File file)     (3)
  String getName()
  MimeType getMimeType()
  byte[] getBytes()
  Clob toClob(Charset charset)     (4)
  void writeBytesTo(OutputStream os)     (5)
  void writeTo(File file)     (6)
  DataSource asDataSource()     (7)
  Blob zip()     (8)
  Blob zip(String zipEntryNameIfAny)     (9)
  Blob unZip(CommonMimeType resultingMimeType)
  Blob unZip(CommonMimeType resultingMimeType, ZipOptions zipOptions)
  Try<HashUtils.Hash> tryHash(HashAlgorithm hashAlgorithm)
  String md5Hex()
  String sha256Hex()
  boolean equals(Object o)
  int hashCode()
  String toString()
  Optional<BufferedImage> asImage()     (10)
}
1 of(String, CommonMimeType, byte)

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

2 tryRead(String, CommonMimeType, DataSource)

Returns a new Blob of given name , mimeType and content from dataSource , wrapped with a Try .

3 tryRead(String, CommonMimeType, File)

Shortcut for tryRead(name, mimeType, DataSource.ofFile(file))

4 toClob(Charset)

Converts to a Clob , using given Charset for the underlying byte[] to String conversion.

5 writeBytesTo(OutputStream)

Does not close the OutputStream.

6 writeTo(File)

Writes this Blob to the file represented by the specified File object.

7 asDataSource()

Returns a new DataSource for underlying byte array.

8 zip()

Returns a new Blob that has this Blob’s underlying byte array zipped into a zip-entry using this Blob’s name.

9 zip(String)

Returns a new Blob that has this Blob’s underlying byte array zipped into a zip-entry with given zip-entry name.

10 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, as 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.

tryRead(String, CommonMimeType, DataSource)

Returns a new Blob of given name , mimeType and content from dataSource , wrapped with a Try .

name may or may not include the desired filename extension, as 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 factories directly.

tryRead(String, CommonMimeType, File)

Shortcut for tryRead(name, mimeType, DataSource.ofFile(file))

toClob(Charset)

Converts to a Clob , using given Charset for the underlying byte[] to String conversion.

writeBytesTo(OutputStream)

Does not close the OutputStream.

writeTo(File)

Writes this Blob to the file represented by the specified File object.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

asDataSource()

Returns a new DataSource for underlying byte array.

zip()

Returns a new Blob that has this Blob’s underlying byte array zipped into a zip-entry using this Blob’s name.

zip(String)

Returns a new Blob that has this Blob’s underlying byte array zipped into a zip-entry with given zip-entry name.

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.