= Blob
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

[source,java]
.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)     // <.>
  Try<Blob> tryRead(String name, CommonMimeType mimeType, DataSource dataSource)     // <.>
  Try<Blob> tryRead(String name, CommonMimeType mimeType, File file)     // <.>
  String getName()
  MimeType getMimeType()
  byte[] getBytes()
  Clob toClob(Charset charset)     // <.>
  void writeBytesTo(OutputStream os)     // <.>
  void writeTo(File file)     // <.>
  DataSource asDataSource()     // <.>
  Blob zip()     // <.>
  Blob zip(String zipEntryNameIfAny)     // <.>
  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()     // <.>
}
----

<.> xref:#of_String_CommonMimeType_byte[of(String, CommonMimeType, byte)]
+
--
Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] of given _name_ , _mimeType_ and _content_ .
--
<.> xref:#tryRead_String_CommonMimeType_DataSource[tryRead(String, CommonMimeType, DataSource)]
+
--
Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] of given _name_ , _mimeType_ and content from _dataSource_ , wrapped with a xref:refguide:commons:index/functional/Try.adoc[Try] .
--
<.> xref:#tryRead_String_CommonMimeType_File[tryRead(String, CommonMimeType, File)]
+
--
Shortcut for _tryRead(name, mimeType, DataSource.ofFile(file))_
--
<.> xref:#toClob_Charset[toClob(Charset)]
+
--
Converts to a xref:refguide:applib:index/value/Clob.adoc[Clob] , using given _Charset_ for the underlying byte[] to String conversion.
--
<.> xref:#writeBytesTo_OutputStream[writeBytesTo(OutputStream)]
+
--
Does not close the OutputStream.
--
<.> xref:#writeTo_File[writeTo(File)]
+
--
Writes this xref:refguide:applib:index/value/Blob.adoc[Blob] to the file represented by the specified `File` object.
--
<.> xref:#asDataSource_[asDataSource()]
+
--
Returns a new xref:refguide:commons:index/io/DataSource.adoc[DataSource] for underlying byte array.
--
<.> xref:#zip_[zip()]
+
--
Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] that has this Blob's underlying byte array zipped into a zip-entry using this Blob's name.
--
<.> xref:#zip_String[zip(String)]
+
--
Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] that has this Blob's underlying byte array zipped into a zip-entry with given zip-entry name.
--
<.> xref:#asImage_[asImage()]

== Members

[#of_String_CommonMimeType_byte]
=== of(String, CommonMimeType, byte)

Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] of given _name_ , _mimeType_ and _content_ .

_name_ may or may not include the desired filename extension, as it is guaranteed, that the resulting xref:refguide:applib:index/value/Blob.adoc[Blob] has the appropriate extension as constraint by the given _mimeType_ .

For more fine-grained control use one of the xref:refguide:applib:index/value/Blob.adoc[Blob] constructors directly.

[#tryRead_String_CommonMimeType_DataSource]
=== tryRead(String, CommonMimeType, DataSource)

Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] of given _name_ , _mimeType_ and content from _dataSource_ , wrapped with a xref:refguide:commons:index/functional/Try.adoc[Try] .

_name_ may or may not include the desired filename extension, as it is guaranteed, that the resulting xref:refguide:applib:index/value/Blob.adoc[Blob] has the appropriate extension as constraint by the given _mimeType_ .

For more fine-grained control use one of the xref:refguide:applib:index/value/Blob.adoc[Blob] factories directly.

[#tryRead_String_CommonMimeType_File]
=== tryRead(String, CommonMimeType, File)

Shortcut for _tryRead(name, mimeType, DataSource.ofFile(file))_

[#toClob_Charset]
=== toClob(Charset)

Converts to a xref:refguide:applib:index/value/Clob.adoc[Clob] , using given _Charset_ for the underlying byte[] to String conversion.

[#writeBytesTo_OutputStream]
=== writeBytesTo(OutputStream)

Does not close the OutputStream.

[#writeTo_File]
=== writeTo(File)

Writes this xref:refguide:applib:index/value/Blob.adoc[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_]
=== asDataSource()

Returns a new xref:refguide:commons:index/io/DataSource.adoc[DataSource] for underlying byte array.

[#zip_]
=== zip()

Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] that has this Blob's underlying byte array zipped into a zip-entry using this Blob's name.

[#zip_String]
=== zip(String)

Returns a new xref:refguide:applib:index/value/Blob.adoc[Blob] that has this Blob's underlying byte array zipped into a zip-entry with given zip-entry name.

[#asImage_]
=== asImage()

include::hooks/Blob_usage-notes.adoc[]
