Module bus.http

Class RequestBody

java.lang.Object
org.miaixz.bus.http.bodys.RequestBody
Direct Known Subclasses:
FormBody, MultipartBody, ProgressBody

public abstract class RequestBody extends Object
The body of an HTTP request.

This class represents the content of an HTTP request and supports creating request bodies from strings, byte arrays, files, and other sources. It provides functionality for specifying the media type, content length, and writing the content. It also supports special cases for duplex and one-shot transmission.

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of bytes that will be written to sink when this request body is transmitted, or -1 if that count is unknown.
    abstract org.miaixz.bus.core.lang.MediaType
    Returns the media type of this request body.
    create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content)
    Creates a new request body from a byte array.
    create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content, int offset, int byteCount)
    Creates a new request body from a portion of a byte array.
    create(org.miaixz.bus.core.lang.MediaType contentType, File file)
    Creates a new request body from a file.
    create(org.miaixz.bus.core.lang.MediaType contentType, String content)
    Creates a new request body from a string.
    create(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content)
    Creates a new request body from a ByteString.
    boolean
    Returns whether this request body is a duplex body.
    boolean
    Returns whether this request body is a one-shot body.
    abstract void
    writeTo(org.miaixz.bus.core.io.sink.BufferSink sink)
    Writes the content of this request body to the given sink.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RequestBody

      public RequestBody()
  • Method Details

    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, String content)
      Creates a new request body from a string.

      If contentType is non-null and lacks a charset, UTF-8 will be used.

      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content string.
      Returns:
      A new RequestBody instance.
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content)
      Creates a new request body from a ByteString.
      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content as a ByteString.
      Returns:
      A new RequestBody instance.
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content)
      Creates a new request body from a byte array.
      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content as a byte array.
      Returns:
      A new RequestBody instance.
      Throws:
      NullPointerException - if content is null.
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content, int offset, int byteCount)
      Creates a new request body from a portion of a byte array.
      Parameters:
      contentType - The media type of the content, which may be null.
      content - The content as a byte array.
      offset - The starting offset in the byte array.
      byteCount - The number of bytes to use.
      Returns:
      A new RequestBody instance.
      Throws:
      NullPointerException - if content is null.
      ArrayIndexOutOfBoundsException - if the offset or byteCount are invalid.
    • create

      public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, File file)
      Creates a new request body from a file.
      Parameters:
      contentType - The media type of the content, which may be null.
      file - The file to use as the content.
      Returns:
      A new RequestBody instance.
      Throws:
      NullPointerException - if file is null.
    • contentType

      public abstract org.miaixz.bus.core.lang.MediaType contentType()
      Returns the media type of this request body.
      Returns:
      The media type, which may be null.
    • contentLength

      public long contentLength() throws IOException
      Returns the number of bytes that will be written to sink when this request body is transmitted, or -1 if that count is unknown.
      Returns:
      The content length.
      Throws:
      IOException - if the length cannot be determined.
    • writeTo

      public abstract void writeTo(org.miaixz.bus.core.io.sink.BufferSink sink) throws IOException
      Writes the content of this request body to the given sink.
      Parameters:
      sink - The sink to write to.
      Throws:
      IOException - if an I/O error occurs during writing.
    • isDuplex

      public boolean isDuplex()
      Returns whether this request body is a duplex body.

      A duplex request body allows for interleaved transmission of request and response data, which is only supported for HTTP/2. This returns false by default unless overridden by a subclass.

      Returns:
      true if this is a duplex request body.
    • isOneShot

      public boolean isOneShot()
      Returns whether this request body is a one-shot body.

      A one-shot request body can only be transmitted once, typically used for destructively-written scenarios. This returns false by default unless overridden by a subclass.

      Returns:
      true if this is a one-shot request body.