Module bus.http

Class ResultBody

java.lang.Object
org.miaixz.bus.http.plugin.httpv.ResultBody
All Implemented Interfaces:
CoverResult.Body

public class ResultBody extends Object implements CoverResult.Body
An implementation of CoverResult.Body that wraps an HTTP Response. It provides a rich API for consuming the response body in various formats (e.g., String, byte array, deserialized objects) and supports download progress monitoring.
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • executor

      protected CoverTasks.Executor executor
      The executor for handling callbacks and data conversions.
    • charset

      protected Charset charset
      The character set of the response body.
  • Constructor Details

    • ResultBody

      public ResultBody(CoverHttp<?> coverHttp, Response response, CoverTasks.Executor executor)
      Constructs a new ResultBody.
      Parameters:
      coverHttp - The HTTP task that received the response.
      response - The raw HTTP response.
      executor - The task executor.
  • Method Details

    • toWapper

      public CoverWapper toWapper()
      Description copied from interface: CoverResult.Body
      Deserializes the response body into a schemaless map-like object.
      Specified by:
      toWapper in interface CoverResult.Body
      Returns:
      A CoverWapper representing the data structure.
    • toArray

      public CoverArray toArray()
      Description copied from interface: CoverResult.Body
      Deserializes the response body into a schemaless list-like object.
      Specified by:
      toArray in interface CoverResult.Body
      Returns:
      A CoverArray representing the data structure.
    • toBean

      public <T> T toBean(Class<T> type)
      Description copied from interface: CoverResult.Body
      Deserializes the response body into an object of the specified type.
      Specified by:
      toBean in interface CoverResult.Body
      Type Parameters:
      T - The target generic type.
      Parameters:
      type - The class of the target object.
      Returns:
      An instance of the target type.
    • toList

      public <T> List<T> toList(Class<T> type)
      Description copied from interface: CoverResult.Body
      Deserializes the response body into a list of objects of the specified type.
      Specified by:
      toList in interface CoverResult.Body
      Type Parameters:
      T - The target generic type for list elements.
      Parameters:
      type - The class of the elements in the list.
      Returns:
      A list of instances of the target type.
    • getType

      public org.miaixz.bus.core.lang.MediaType getType()
      Description copied from interface: CoverResult.Body
      Gets the MediaType of the response body.
      Specified by:
      getType in interface CoverResult.Body
      Returns:
      The media type.
    • getLength

      public long getLength()
      Description copied from interface: CoverResult.Body
      Gets the length of the response body in bytes.
      Specified by:
      getLength in interface CoverResult.Body
      Returns:
      The length of the body.
    • nextOnIO

      public CoverResult.Body nextOnIO()
      Description copied from interface: CoverResult.Body
      Specifies that the next download-related callback should be executed on an I/O thread.
      Specified by:
      nextOnIO in interface CoverResult.Body
      Returns:
      This Body instance for chaining.
    • setOnProcess

      public CoverResult.Body setOnProcess(Callback<Progress> onProcess)
      Description copied from interface: CoverResult.Body
      Sets a progress callback for monitoring the download of the response body.
      Specified by:
      setOnProcess in interface CoverResult.Body
      Parameters:
      onProcess - The progress callback function.
      Returns:
      This Body instance for chaining.
    • stepBytes

      public CoverResult.Body stepBytes(long stepBytes)
      Description copied from interface: CoverResult.Body
      Sets the interval in bytes for progress callback invocations. Defaults to 8KB (8192).
      Specified by:
      stepBytes in interface CoverResult.Body
      Parameters:
      stepBytes - The step size in bytes.
      Returns:
      This Body instance for chaining.
    • stepRate

      public CoverResult.Body stepRate(double stepRate)
      Description copied from interface: CoverResult.Body
      Sets the interval as a rate (percentage) for progress callback invocations.
      Specified by:
      stepRate in interface CoverResult.Body
      Parameters:
      stepRate - The step rate, from 0.0 to 1.0.
      Returns:
      This Body instance for chaining.
    • setRangeIgnored

      public CoverResult.Body setRangeIgnored()
      Description copied from interface: CoverResult.Body
      Configures the progress callback to ignore the HTTP Range header, calculating progress from 0. This is useful when the total file size is known but the download is of a partial chunk.
      Specified by:
      setRangeIgnored in interface CoverResult.Body
      Returns:
      This Body instance for chaining.
    • toByteStream

      public InputStream toByteStream()
      Description copied from interface: CoverResult.Body
      Converts the response body to a byte stream.
      Specified by:
      toByteStream in interface CoverResult.Body
      Returns:
      An InputStream of the response body.
    • toBytes

      public byte[] toBytes()
      Description copied from interface: CoverResult.Body
      Reads the entire response body into a byte array. This method consumes the body.
      Specified by:
      toBytes in interface CoverResult.Body
      Returns:
      A byte array containing the response body.
    • toCharStream

      public Reader toCharStream()
      Description copied from interface: CoverResult.Body
      Converts the response body to a character stream using the response's charset.
      Specified by:
      toCharStream in interface CoverResult.Body
      Returns:
      A Reader for the response body.
    • toString

      public String toString()
      Description copied from interface: CoverResult.Body
      Reads the entire response body into a string using the response's charset. This method consumes the body.
      Specified by:
      toString in interface CoverResult.Body
      Overrides:
      toString in class Object
      Returns:
      The response body as a String.
    • toByteString

      public org.miaixz.bus.core.io.ByteString toByteString()
      Description copied from interface: CoverResult.Body
      Reads the entire response body into a ByteString. This method consumes the body.
      Specified by:
      toByteString in interface CoverResult.Body
      Returns:
      A ByteString containing the response body.
    • toFile

      public Downloads toFile(String filePath)
      Description copied from interface: CoverResult.Body
      Downloads the response body to a file at the specified path.
      Specified by:
      toFile in interface CoverResult.Body
      Parameters:
      filePath - The absolute or relative path to the target file.
      Returns:
      A Downloads object to control the download process.
    • toFile

      public Downloads toFile(File file)
      Description copied from interface: CoverResult.Body
      Downloads the response body to the specified file.
      Specified by:
      toFile in interface CoverResult.Body
      Parameters:
      file - The target file.
      Returns:
      A Downloads object to control the download process.
    • toFolder

      public Downloads toFolder(String dirPath)
      Description copied from interface: CoverResult.Body
      Downloads the response body to the specified directory. The filename is automatically resolved from the 'Content-Disposition' header or the URL.
      Specified by:
      toFolder in interface CoverResult.Body
      Parameters:
      dirPath - The path to the target directory.
      Returns:
      A Downloads object to control the download process.
    • toFolder

      public Downloads toFolder(File dir)
      Description copied from interface: CoverResult.Body
      Downloads the response body to the specified directory. The filename is automatically resolved from the 'Content-Disposition' header or the URL.
      Specified by:
      toFolder in interface CoverResult.Body
      Parameters:
      dir - The target directory.
      Returns:
      A Downloads object to control the download process.
    • cache

      public CoverResult.Body cache()
      Description copied from interface: CoverResult.Body
      Caches the response body in memory after the first read. This allows multiple consumption methods (e.g., toBytes(), toString()) to be called. Note: Progress callbacks cannot be used when caching is enabled.
      Specified by:
      cache in interface CoverResult.Body
      Returns:
      This Body instance for chaining.
    • close

      public CoverResult.Body close()
      Description copied from interface: CoverResult.Body
      Closes the response body without consuming it.
      Specified by:
      close in interface CoverResult.Body
      Returns:
      This Body instance.