Class ResponseBodyReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class ResponseBodyReader
    extends java.io.FilterReader
    A custom Reader that wraps the Reader from the OkHttp Response and ensures that the OkHttp Response object is properly closed when the reader is closed. This class extends FilterReader and takes an OkHttp Response object as a parameter. It retrieves the Reader from the Response and overrides the close method to close both the Reader and the Response object, ensuring proper resource management and preventing premature closure of the underlying HTTP connection.
    • Field Summary

      • Fields inherited from class java.io.FilterReader

        in
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      ResponseBodyReader​(okhttp3.Response response)
      Constructs a ResponseBodyReader that wraps the Reader from the given OkHttp Response object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the Reader and the associated OkHttp Response object.
      • Methods inherited from class java.io.FilterReader

        mark, markSupported, read, read, ready, reset, skip
      • Methods inherited from class java.io.Reader

        nullReader, read, read, transferTo
      • Methods inherited from class java.lang.Object

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

      • ResponseBodyReader

        public ResponseBodyReader​(okhttp3.Response response)
                           throws java.io.IOException
        Constructs a ResponseBodyReader that wraps the Reader from the given OkHttp Response object.
        Parameters:
        response - the OkHttp Response object from which the Reader is retrieved
        Throws:
        java.io.IOException - if an I/O error occurs while retrieving the Reader
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes the Reader and the associated OkHttp Response object. This ensures that the underlying HTTP connection is properly closed after the reader is no longer needed.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterReader
        Throws:
        java.io.IOException - if an I/O error occurs