Blob (record)

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
record 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)
  Clob toClob(Charset charset)     (4)
  Clob toClobUtf8()     (5)
  void writeBytesTo(OutputStream os)     (6)
  void writeTo(File file)     (7)
  DataSource asDataSource()     (8)
  Blob flatMap(UnaryOperator<Blob> mapper)     (9)
  Blob zip()     (10)
  Blob zip(String zipEntryNameIfAny)     (11)
  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()     (12)
}
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 toClobUtf8()

Converts to a Clob , using UTF-8 for the underlying byte[] to String conversion.

6 writeBytesTo(OutputStream)

Does not close the OutputStream.

7 writeTo(File)

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

8 asDataSource()

Returns a new DataSource for underlying byte array.

9 flatMap(UnaryOperator)

Allows fluent chaining of Blob operators.

10 zip()

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

11 zip(String)

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

12 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.

toClobUtf8()

Converts to a Clob , using UTF-8 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.

flatMap(UnaryOperator)

Allows fluent chaining of Blob operators.

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 JPA/Eclipselink, Blob properties can be mapped using the BlobJpaEmbeddable:

@AttributeOverrides({
    @AttributeOverride(name="name",    column=@Column(name="attachment_name")),
    @AttributeOverride(name="mimeType",column=@Column(name="attachment_mimeType")),
    @AttributeOverride(name="bytes",   column=@Column(name="attachment_bytes"))
})
@Embedded
private BlobJpaEmbeddable attachment;

@Property()
@PropertyLayout()
public Blob getPdf() {
  return BlobJpaEmbeddable.toBlob(pdf);
}
public void setPdf(final Blob pdf) {
  this.pdf = BlobJpaEmbeddable.fromBlob(pdf);
}