Class Blob

java.lang.Object
com.google.genai.gaos.utils.Blob

public class Blob extends Object
A utility class for creating data blobs from various input sources for use as request bodies.

This class provides convenient factory methods to create blobs from:

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 Details

    • from

      public static Blob from(Path path) throws FileNotFoundException
      Creates a Blob from a file path. The file is read on each use so the blob supports retries.
      Parameters:
      path - the path to the file to read
      Returns:
      a new Blob instance
      Throws:
      FileNotFoundException - if the file does not exist or cannot be read
      NullPointerException - if path is null
    • from

      public static Blob from(InputStream inputStream)
      Creates a Blob from an InputStream. The stream is read lazily when the request body is written.

      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 or from(byte[]) for in-memory data.

      Parameters:
      inputStream - the InputStream to read from
      Returns:
      a new Blob instance
      Throws:
      NullPointerException - if inputStream is null
    • from

      public static Blob from(String string)
      Creates a Blob from a String using UTF-8 encoding.
      Parameters:
      string - the string to convert to a Blob
      Returns:
      a new Blob instance
      Throws:
      NullPointerException - if string is null
    • from

      public static Blob from(byte[] data)
      Creates a Blob from a byte array.
      Parameters:
      data - the byte array to wrap as a Blob
      Returns:
      a new Blob instance
      Throws:
      NullPointerException - if data is null
    • from

      public static Blob from(ByteBuffer buffer)
      Creates a Blob from a single ByteBuffer. The buffer contents are copied so the blob supports retries.
      Parameters:
      buffer - the ByteBuffer to wrap as a Blob
      Returns:
      a new Blob instance
      Throws:
      NullPointerException - if buffer is null
    • from

      public static Blob from(List<ByteBuffer> buffers)
      Creates a Blob from a list of ByteBuffers. The buffer contents are copied so the blob supports retries.
      Parameters:
      buffers - the list of ByteBuffers to wrap as a Blob
      Returns:
      a new Blob instance
      Throws:
      NullPointerException - if buffers is null
    • toHttpBody

      public HttpBody toHttpBody()
      Returns this blob's content as a transport request body.
      Returns:
      the request body carrier
    • contentLength

      public long contentLength()
      Returns the number of bytes in the blob, or -1 if unknown.