Class OpenSearchRequestBodyBuffer

java.lang.Object
org.opensearch.client.util.OpenSearchRequestBodyBuffer

public class OpenSearchRequestBodyBuffer extends Object
Serializes and captures an OpenSearch request body, and then provides access to it in convenient forms for HTTP requests. This is a utility class for use by various OpenSearchTransport implementations.

Request bodies can be automatically compressed when they exceed a given size.

  • Constructor Details

    • OpenSearchRequestBodyBuffer

      public OpenSearchRequestBodyBuffer(JsonpMapper mapper, int requestCompressionSize)
      Create a request body buffer
      Parameters:
      mapper - mapper used to serialize the content
      requestCompressionSize - When the captured data exceeds this size, it will be automatically compressed. Pass Integer.MAX_VALUE to prevent compression
  • Method Details

    • addContent

      public void addContent(Object content) throws IOException
      Add some content to the buffer. If the buffer already contains some data, or if the provided object implements NdJsonpSerializable, then the buffer will contain multiple objects in newline-delimited JSON format.
      Parameters:
      content - The new content object to add
      Throws:
      IOException
    • isCompressed

      public boolean isCompressed()
      Returns:
      true if the content has been compressed
    • isNdJson

      public boolean isNdJson()
      Returns:
      true if this buffer contains multiple newline-delimited objects.
    • getContentEncoding

      @CheckForNull public String getContentEncoding()
      Get the value of the Content-Encoding header that should be sent along with this buffer, or null if there shouldn't be one.
    • getContentType

      @Nonnull public String getContentType()
      Get the value of the Content-Type header that should be sent along with this buffer.
    • getContentLength

      public long getContentLength()
      Get the value of the Content-Length header that should be sent along with this buffer.

      This call finalizes the buffer. After this call, any attempt to add more content will throw an IOException.

      Returns:
      The length of the buffered content
    • getByteArray

      public byte[] getByteArray()
      Get the contents of this buffer as a byte array.

      This call finalizes the buffer. After this call, any attempt to add more content will throw an IOException.

      Returns:
      The buffered data
    • getInputStream

      public InputStream getInputStream()
      Get the contents of this buffer as a new InputStream.

      Calls to this method are cheap, since all the new streams will share the same underlying array

      This call finalizes the buffer. After this call, any attempt to add more content will throw an IOException.

      Returns:
      The buffered data
    • close

      public void close() throws IOException
      This call finalizes the buffer. After this call, any attempt to add more content will throw an IOException.
      Throws:
      IOException