Class InputStreamRequestBody


  • public class InputStreamRequestBody
    extends okhttp3.RequestBody
    A custom implementation of OkHttp's RequestBody that wraps an InputStream. This class allows streaming of data from an InputStream directly to an HTTP request body, which is useful for file uploads or sending large amounts of data without loading it all into memory.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class okhttp3.RequestBody

        okhttp3.RequestBody.Companion
    • Field Summary

      • Fields inherited from class okhttp3.RequestBody

        Companion
    • Constructor Summary

      Constructors 
      Constructor Description
      InputStreamRequestBody​(okhttp3.MediaType contentType, java.io.InputStream inputStream)
      Constructs an InputStreamRequestBody with the specified content type and input stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long contentLength()
      Returns the content length of this request body, if known.
      okhttp3.MediaType contentType()
      Returns the content type of this request body.
      void writeTo​(okio.BufferedSink sink)
      Writes the content of the InputStream to the given BufferedSink.
      • Methods inherited from class okhttp3.RequestBody

        create, create, create, create, create, create, create, create, create, create, create, create, create, isDuplex, isOneShot
      • Methods inherited from class java.lang.Object

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

      • InputStreamRequestBody

        public InputStreamRequestBody​(@Nullable
                                      okhttp3.MediaType contentType,
                                      java.io.InputStream inputStream)
        Constructs an InputStreamRequestBody with the specified content type and input stream.
        Parameters:
        contentType - the MediaType of the content, or null if not known
        inputStream - the InputStream containing the data to be sent
        Throws:
        java.lang.NullPointerException - if inputStream is null
    • Method Detail

      • contentType

        @Nullable
        public okhttp3.MediaType contentType()
        Returns the content type of this request body.
        Specified by:
        contentType in class okhttp3.RequestBody
        Returns:
        the MediaType of the content, or null if not specified
      • contentLength

        public long contentLength()
                           throws java.io.IOException
        Returns the content length of this request body, if known. This method attempts to determine the length using the InputStream's available() method, which may not always accurately reflect the total length of the stream.
        Overrides:
        contentLength in class okhttp3.RequestBody
        Returns:
        the content length, or -1 if the length is unknown
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeTo

        public void writeTo​(okio.BufferedSink sink)
                     throws java.io.IOException
        Writes the content of the InputStream to the given BufferedSink. This method is responsible for transferring the data from the InputStream to the network request.
        Specified by:
        writeTo in class okhttp3.RequestBody
        Parameters:
        sink - the BufferedSink to write the content to
        Throws:
        java.io.IOException - if an I/O error occurs during writing