Class PersistentHttpStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    HeartbeatingHttpStream, YoutubePersistentHttpStream

    public class PersistentHttpStream
    extends SeekableInputStream
    implements java.lang.AutoCloseable
    Use an HTTP endpoint as a stream, where the connection resetting is handled gracefully by reopening the connection and using a closed stream will just reopen the connection.
    • Field Detail

      • contentUrl

        protected final java.net.URI contentUrl
      • currentContent

        protected java.io.InputStream currentContent
      • position

        protected long position
    • Constructor Detail

      • PersistentHttpStream

        public PersistentHttpStream​(HttpInterface httpInterface,
                                    java.net.URI contentUrl,
                                    java.lang.Long contentLength)
        Parameters:
        httpInterface - The HTTP interface to use for requests
        contentUrl - The URL of the resource
        contentLength - The length of the resource in bytes
    • Method Detail

      • checkStatusCode

        public int checkStatusCode()
                            throws java.io.IOException
        Connect and return status code or return last status code if already connected. This causes the internal status code checker to be disabled, so non-success status codes will be returned instead of being thrown as they would be otherwise.
        Returns:
        The status code when connecting to the URL
        Throws:
        java.io.IOException - On IO error
      • getCurrentResponse

        public org.apache.http.HttpResponse getCurrentResponse()
        Returns:
        An HTTP response if one is currently open.
      • getConnectUrl

        protected java.net.URI getConnectUrl()
      • useHeadersForRange

        protected boolean useHeadersForRange()
      • connect

        protected void connect​(boolean skipStatusCheck)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • createContentInputStream

        public java.io.InputStream createContentInputStream​(org.apache.http.HttpResponse response)
                                                     throws java.io.IOException
        Returns:
        An InputStream implementation for the current http stream.
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • internalRead

        protected int internalRead​(byte[] b,
                                   int off,
                                   int len,
                                   boolean attemptReconnect)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • internalSkip

        protected long internalSkip​(long n,
                                    boolean attemptReconnect)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.InputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • releaseConnection

        public void releaseConnection()
        Detach from the current connection, making sure not to close the connection when the stream is closed.
      • seekHard

        protected void seekHard​(long position)
                         throws java.io.IOException
        Specified by:
        seekHard in class SeekableInputStream
        Throws:
        java.io.IOException
      • canSeekHard

        public boolean canSeekHard()
        Specified by:
        canSeekHard in class SeekableInputStream
        Returns:
        true if it is possible to seek to an arbitrary position in this stream, even when it is behind the current position.