java.lang.Object
org.miaixz.bus.http.plugin.httpv.ResultBody
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionprotected CharsetThe character set of the response body.protected CoverTasks.ExecutorThe executor for handling callbacks and data conversions. -
Constructor Summary
ConstructorsConstructorDescriptionResultBody(CoverHttp<?> coverHttp, Response response, CoverTasks.Executor executor) Constructs a new ResultBody. -
Method Summary
Modifier and TypeMethodDescriptioncache()Caches the response body in memory after the first read.close()Closes the response body without consuming it.longGets the length of the response body in bytes.org.miaixz.bus.core.lang.MediaTypegetType()Gets theMediaTypeof the response body.nextOnIO()Specifies that the next download-related callback should be executed on an I/O thread.setOnProcess(Callback<Progress> onProcess) Sets a progress callback for monitoring the download of the response body.Configures the progress callback to ignore the HTTP Range header, calculating progress from 0.stepBytes(long stepBytes) Sets the interval in bytes for progress callback invocations.stepRate(double stepRate) Sets the interval as a rate (percentage) for progress callback invocations.toArray()Deserializes the response body into a schemaless list-like object.<T> TDeserializes the response body into an object of the specified type.byte[]toBytes()Reads the entire response body into a byte array.Converts the response body to a byte stream.org.miaixz.bus.core.io.ByteStringReads the entire response body into a ByteString.Converts the response body to a character stream using the response's charset.Downloads the response body to the specified file.Downloads the response body to a file at the specified path.Downloads the response body to the specified directory.Downloads the response body to the specified directory.<T> List<T> Deserializes the response body into a list of objects of the specified type.toString()Reads the entire response body into a string using the response's charset.toWapper()Deserializes the response body into a schemaless map-like object.
-
Field Details
-
executor
The executor for handling callbacks and data conversions. -
charset
The character set of the response body.
-
-
Constructor Details
-
ResultBody
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
Description copied from interface:CoverResult.BodyDeserializes the response body into a schemaless map-like object.- Specified by:
toWapperin interfaceCoverResult.Body- Returns:
- A
CoverWapperrepresenting the data structure.
-
toArray
Description copied from interface:CoverResult.BodyDeserializes the response body into a schemaless list-like object.- Specified by:
toArrayin interfaceCoverResult.Body- Returns:
- A
CoverArrayrepresenting the data structure.
-
toBean
Description copied from interface:CoverResult.BodyDeserializes the response body into an object of the specified type.- Specified by:
toBeanin interfaceCoverResult.Body- Type Parameters:
T- The target generic type.- Parameters:
type- The class of the target object.- Returns:
- An instance of the target type.
-
toList
Description copied from interface:CoverResult.BodyDeserializes the response body into a list of objects of the specified type.- Specified by:
toListin interfaceCoverResult.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.BodyGets theMediaTypeof the response body.- Specified by:
getTypein interfaceCoverResult.Body- Returns:
- The media type.
-
getLength
public long getLength()Description copied from interface:CoverResult.BodyGets the length of the response body in bytes.- Specified by:
getLengthin interfaceCoverResult.Body- Returns:
- The length of the body.
-
nextOnIO
Description copied from interface:CoverResult.BodySpecifies that the next download-related callback should be executed on an I/O thread.- Specified by:
nextOnIOin interfaceCoverResult.Body- Returns:
- This
Bodyinstance for chaining.
-
setOnProcess
Description copied from interface:CoverResult.BodySets a progress callback for monitoring the download of the response body.- Specified by:
setOnProcessin interfaceCoverResult.Body- Parameters:
onProcess- The progress callback function.- Returns:
- This
Bodyinstance for chaining.
-
stepBytes
Description copied from interface:CoverResult.BodySets the interval in bytes for progress callback invocations. Defaults to 8KB (8192).- Specified by:
stepBytesin interfaceCoverResult.Body- Parameters:
stepBytes- The step size in bytes.- Returns:
- This
Bodyinstance for chaining.
-
stepRate
Description copied from interface:CoverResult.BodySets the interval as a rate (percentage) for progress callback invocations.- Specified by:
stepRatein interfaceCoverResult.Body- Parameters:
stepRate- The step rate, from 0.0 to 1.0.- Returns:
- This
Bodyinstance for chaining.
-
setRangeIgnored
Description copied from interface:CoverResult.BodyConfigures 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:
setRangeIgnoredin interfaceCoverResult.Body- Returns:
- This
Bodyinstance for chaining.
-
toByteStream
Description copied from interface:CoverResult.BodyConverts the response body to a byte stream.- Specified by:
toByteStreamin interfaceCoverResult.Body- Returns:
- An
InputStreamof the response body.
-
toBytes
public byte[] toBytes()Description copied from interface:CoverResult.BodyReads the entire response body into a byte array. This method consumes the body.- Specified by:
toBytesin interfaceCoverResult.Body- Returns:
- A byte array containing the response body.
-
toCharStream
Description copied from interface:CoverResult.BodyConverts the response body to a character stream using the response's charset.- Specified by:
toCharStreamin interfaceCoverResult.Body- Returns:
- A
Readerfor the response body.
-
toString
Description copied from interface:CoverResult.BodyReads the entire response body into a string using the response's charset. This method consumes the body.- Specified by:
toStringin interfaceCoverResult.Body- Overrides:
toStringin classObject- Returns:
- The response body as a
String.
-
toByteString
public org.miaixz.bus.core.io.ByteString toByteString()Description copied from interface:CoverResult.BodyReads the entire response body into a ByteString. This method consumes the body.- Specified by:
toByteStringin interfaceCoverResult.Body- Returns:
- A
ByteStringcontaining the response body.
-
toFile
Description copied from interface:CoverResult.BodyDownloads the response body to a file at the specified path.- Specified by:
toFilein interfaceCoverResult.Body- Parameters:
filePath- The absolute or relative path to the target file.- Returns:
- A
Downloadsobject to control the download process.
-
toFile
Description copied from interface:CoverResult.BodyDownloads the response body to the specified file.- Specified by:
toFilein interfaceCoverResult.Body- Parameters:
file- The target file.- Returns:
- A
Downloadsobject to control the download process.
-
toFolder
Description copied from interface:CoverResult.BodyDownloads the response body to the specified directory. The filename is automatically resolved from the 'Content-Disposition' header or the URL.- Specified by:
toFolderin interfaceCoverResult.Body- Parameters:
dirPath- The path to the target directory.- Returns:
- A
Downloadsobject to control the download process.
-
toFolder
Description copied from interface:CoverResult.BodyDownloads the response body to the specified directory. The filename is automatically resolved from the 'Content-Disposition' header or the URL.- Specified by:
toFolderin interfaceCoverResult.Body- Parameters:
dir- The target directory.- Returns:
- A
Downloadsobject to control the download process.
-
cache
Description copied from interface:CoverResult.BodyCaches 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:
cachein interfaceCoverResult.Body- Returns:
- This
Bodyinstance for chaining.
-
close
Description copied from interface:CoverResult.BodyCloses the response body without consuming it.- Specified by:
closein interfaceCoverResult.Body- Returns:
- This
Bodyinstance.
-