Class Blob
- java.lang.Object
-
- pl.gsmservice.gateway.utils.Blob
-
- All Implemented Interfaces:
java.net.http.HttpRequest.BodyPublisher,java.util.concurrent.Flow.Publisher<java.nio.ByteBuffer>
public class Blob extends java.lang.Object implements java.net.http.HttpRequest.BodyPublisherA utility class for creating data blobs from various input sources that implementsHttpRequest.BodyPublisher.This class provides convenient factory methods to create blobs from:
- File paths (
from(Path)) - InputStreams (
from(InputStream)) - Strings (
from(String)) - Byte arrays (
from(byte[])) - ByteBuffers (
from(ByteBuffer)) - Lists of ByteBuffers (
from(List)) - Reactive publishers (
from(Flow.Publisher))
Each blob can be used directly as a
HttpRequest.BodyPublishersince this class implements that interface.Additionally, this class provides consumption methods for reactive data processing:
- Get the stream as a
Flow.Publisher<ByteBuffer>(asPublisher()) - Collect the entire stream into a byte array (
toByteArray()) - Write the stream directly to a file (
toFile(Path))
Single-use consumption: When using consumption methods (
asPublisher(),toByteArray(),toFile()), eachBlobinstance can only be consumed once. After any consumption method is called, the instance is considered consumed and cannot be reused. Any further attempt to use a consumption method will result in anIllegalStateException.Retry compatibility: Most blob types support HTTP request retries effectively. However, InputStream-backed blobs (
from(InputStream)) do not support retries as the stream gets consumed during the first attempt. For retry-compatible scenarios, prefer file-based (from(Path)) or in-memory (from(byte[])) alternatives.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.Flow.Publisher<java.nio.ByteBuffer>asPublisher()Returns aFlow.Publisher<ByteBuffer>that emits individualByteBufferfrom the underlying stream.longcontentLength()static Blobfrom(byte[] data)Creates aBlobfrom a byte array.static Blobfrom(java.io.InputStream inputStream)Creates aBlobfrom anInputStream.static Blobfrom(java.lang.String string)Creates aBlobfrom a String using UTF-8 encoding.static Blobfrom(java.nio.ByteBuffer buffer)Creates aBlobfrom a singleByteBuffer.static Blobfrom(java.nio.file.Path path)Creates aBlobfrom a file path.static Blobfrom(java.util.concurrent.Flow.Publisher<java.util.List<java.nio.ByteBuffer>> sourcePublisher)Creates aBlobfrom aFlow.Publisher<List<ByteBuffer>>.static Blobfrom(java.util.List<java.nio.ByteBuffer> buffers)Creates aBlobfrom a list ofByteBuffers.voidsubscribe(java.util.concurrent.Flow.Subscriber<? super java.nio.ByteBuffer> subscriber)java.util.concurrent.CompletableFuture<byte[]>toByteArray()Collects the entire stream into a single byte array.java.util.concurrent.CompletableFuture<java.nio.file.Path>toFile(java.nio.file.Path destinationPath)Writes the entire stream to the specified file path.java.util.concurrent.CompletableFuture<java.io.InputStream>toInputStream()Converts the entire stream into anInputStreamfor traditional I/O operations.
-
-
-
Method Detail
-
from
public static Blob from(java.nio.file.Path path) throws java.io.FileNotFoundException
Creates aBlobfrom a file path.This method uses the Java HTTP client's
HttpRequest.BodyPublishers.ofFile()to create a reactive publisher from the file content.- Parameters:
path- the path to the file to read- Returns:
- a new
Blobinstance - Throws:
java.io.FileNotFoundException- if the file does not exist or cannot be readjava.lang.NullPointerException- ifpathisnull
-
from
public static Blob from(java.io.InputStream inputStream)
Creates aBlobfrom anInputStream.This method uses
HttpRequest.BodyPublishers.ofInputStream()to create a reactive publisher that reads from the InputStream lazily, avoiding blocking I/O operations.Important: InputStream-backed blobs do not support retries effectively. If the HTTP request fails and is retried, the InputStream will have already been consumed during the first attempt, causing subsequent retry attempts to send empty request bodies. For retry-compatible scenarios, consider using
from(Path)for file-based data orfrom(byte[])for in-memory data.- Parameters:
inputStream- the InputStream to read from- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifinputStreamisnull
-
from
public static Blob from(java.lang.String string)
Creates aBlobfrom a String using UTF-8 encoding.- Parameters:
string- the string to convert to a Blob- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifstringisnull
-
from
public static Blob from(byte[] data)
Creates aBlobfrom a byte array.This method uses HttpRequest.BodyPublishers.ofByteArray().
- Parameters:
data- the byte array to wrap as a Blob- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifdataisnull
-
from
public static Blob from(java.nio.ByteBuffer buffer)
Creates aBlobfrom a singleByteBuffer.- Parameters:
buffer- the ByteBuffer to wrap as a Blob- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifbufferisnull
-
from
public static Blob from(java.util.List<java.nio.ByteBuffer> buffers)
Creates aBlobfrom a list ofByteBuffers.- Parameters:
buffers- the list of ByteBuffers to wrap as a Blob- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifbuffersisnull
-
from
public static Blob from(java.util.concurrent.Flow.Publisher<java.util.List<java.nio.ByteBuffer>> sourcePublisher)
Creates aBlobfrom aFlow.Publisher<List<ByteBuffer>>.This method uses
ReactiveUtils.flatten()to convert the publisher of lists into a publisher of individual ByteBuffers.- Parameters:
sourcePublisher- the publisher that provides data as lists of ByteBuffers- Returns:
- a new
Blobinstance - Throws:
java.lang.NullPointerException- ifsourcePublisherisnull
-
asPublisher
public java.util.concurrent.Flow.Publisher<java.nio.ByteBuffer> asPublisher()
Returns aFlow.Publisher<ByteBuffer>that emits individualByteBufferfrom the underlying stream.Consumes this instance: After calling this method, this
Blobcannot be used again.- Returns:
- a publisher of individual
ByteBufferitems. - Throws:
java.lang.IllegalStateException- if this instance has already been consumed.
-
toByteArray
public java.util.concurrent.CompletableFuture<byte[]> toByteArray()
Collects the entire stream into a single byte array.Consumes this instance: After calling this method, this
Blobcannot be used again.The returned
CompletableFuturecompletes when all data has been received and assembled into the byte array, or completes exceptionally if an error occurs.- Returns:
- a
CompletableFuturecontaining the complete byte array. - Throws:
java.lang.IllegalStateException- if this instance has already been consumed.
-
toFile
public java.util.concurrent.CompletableFuture<java.nio.file.Path> toFile(java.nio.file.Path destinationPath)
Writes the entire stream to the specified file path.Consumes this instance: After calling this method, this
Blobcannot be used again.The returned
CompletableFuturecompletes with thePathto the written file when all data has been successfully written, or completes exceptionally if an error occurs.- Parameters:
destinationPath- the path where the stream data will be written. If the file exists, it will be truncated.- Returns:
- a
CompletableFuturecontaining thePathto the written file. - Throws:
java.lang.NullPointerException- ifdestinationPathisnull.java.lang.IllegalStateException- if this instance has already been consumed.
-
toInputStream
public java.util.concurrent.CompletableFuture<java.io.InputStream> toInputStream()
Converts the entire stream into anInputStreamfor traditional I/O operations.Consumes this instance: After calling this method, this
Blobcannot be used again.The returned
CompletableFuturecompletes with anInputStreamcontaining all the data from the stream when ready, or completes exceptionally if an error occurs. The resulting InputStream can be used with traditional Java I/O APIs.- Returns:
- a
CompletableFuturecontaining anInputStreamwith the stream data. - Throws:
java.lang.IllegalStateException- if this instance has already been consumed.
-
contentLength
public long contentLength()
- Specified by:
contentLengthin interfacejava.net.http.HttpRequest.BodyPublisher
-
subscribe
public void subscribe(java.util.concurrent.Flow.Subscriber<? super java.nio.ByteBuffer> subscriber)
- Specified by:
subscribein interfacejava.util.concurrent.Flow.Publisher<java.nio.ByteBuffer>
-
-