Class RetryableHttpStream

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

    public class RetryableHttpStream
    extends java.io.InputStream
    InputStream implementation around HttpURLConnection that automatically reconnects if the connection fails in the middle.
    Author:
    Kohsuke Kawaguchi
    • Field Summary

      Fields 
      Modifier and Type Field Description
      java.net.HttpURLConnection connection
      HttpURLConnection to allow the caller to access HTTP resposne headers.
      protected java.net.Proxy proxy
      Proxy, or null none is explicitly given (Java runtime may still decide to use a proxy, though.)
      protected int read
      Number of bytes read so far.
      int totalLength
      Total bytes of the entity.
      java.net.URL url
      Where are we downloading from?
    • Constructor Summary

      Constructors 
      Constructor Description
      RetryableHttpStream​(java.net.URL url)
      Connects to the given HTTP/HTTPS URL, by using the proxy auto-configured by the Java runtime.
      RetryableHttpStream​(java.net.URL url, java.net.Proxy proxy)
      Connects to the given HTTP/HTTPS URL, by using the specified proxy.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.net.HttpURLConnection connect()
      Opens the URL and makes a connection.
      int read()  
      int read​(byte[] b, int off, int len)  
      protected void shallWeRetry()
      Subclass can override this method to determine if we should continue to retry, or abort.
      • Methods inherited from class java.io.InputStream

        available, close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • url

        public final java.net.URL url
        Where are we downloading from?
      • proxy

        protected final java.net.Proxy proxy
        Proxy, or null none is explicitly given (Java runtime may still decide to use a proxy, though.)
      • totalLength

        public final int totalLength
        Total bytes of the entity.
      • read

        protected int read
        Number of bytes read so far.
      • connection

        public final java.net.HttpURLConnection connection
        HttpURLConnection to allow the caller to access HTTP resposne headers. Do not use URLConnection.getInputStream(), however.
    • Constructor Detail

      • RetryableHttpStream

        public RetryableHttpStream​(java.net.URL url)
                            throws java.io.IOException
        Connects to the given HTTP/HTTPS URL, by using the proxy auto-configured by the Java runtime.
        Throws:
        java.io.IOException
      • RetryableHttpStream

        public RetryableHttpStream​(java.net.URL url,
                                   java.net.Proxy proxy)
                            throws java.io.IOException
        Connects to the given HTTP/HTTPS URL, by using the specified proxy.
        Parameters:
        proxy - To force a direct connection, pass in Proxy.NO_PROXY.
        Throws:
        java.io.IOException
    • Method Detail

      • connect

        protected java.net.HttpURLConnection connect()
                                              throws java.io.IOException
        Opens the URL and makes a connection.
        Throws:
        java.io.IOException
      • shallWeRetry

        protected void shallWeRetry()
                             throws java.io.IOException
        Subclass can override this method to determine if we should continue to retry, or abort.

        If this method returns normally, we'll retry. By default, this method retries 5 times then quits.

        Throws:
        java.io.IOException - to abort the processing.
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        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