Class ZeroCopyConsumer<T>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, org.apache.http.concurrent.Cancellable, org.apache.http.nio.protocol.HttpAsyncResponseConsumer<T>

    public abstract class ZeroCopyConsumer<T>
    extends org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
    HttpAsyncResponseConsumer implementation that streams content entity enclosed in an HTTP response directly into a file without an intermediate in-memory buffer.

    This consumer can be useful for file downloads.

    Since:
    4.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.RandomAccessFile accessfile  
      private org.apache.http.Header contentEncoding  
      private org.apache.http.entity.ContentType contentType  
      private java.io.File file  
      private java.nio.channels.FileChannel fileChannel  
      private long idx  
      private org.apache.http.HttpResponse response  
    • Constructor Summary

      Constructors 
      Constructor Description
      ZeroCopyConsumer​(java.io.File file)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected T buildResult​(org.apache.http.protocol.HttpContext context)  
      protected void onContentReceived​(org.apache.http.nio.ContentDecoder decoder, org.apache.http.nio.IOControl ioctrl)  
      protected void onEntityEnclosed​(org.apache.http.HttpEntity entity, org.apache.http.entity.ContentType contentType)  
      protected void onResponseReceived​(org.apache.http.HttpResponse response)  
      protected abstract T process​(org.apache.http.HttpResponse response, java.io.File file, org.apache.http.entity.ContentType contentType)
      Invoked to process received file.
      protected void releaseResources()  
      • Methods inherited from class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer

        cancel, close, consumeContent, failed, getContentType, getException, getResult, isDone, onClose, responseCompleted, responseReceived
      • Methods inherited from class java.lang.Object

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

      • file

        private final java.io.File file
      • accessfile

        private final java.io.RandomAccessFile accessfile
      • response

        private org.apache.http.HttpResponse response
      • contentType

        private org.apache.http.entity.ContentType contentType
      • contentEncoding

        private org.apache.http.Header contentEncoding
      • fileChannel

        private java.nio.channels.FileChannel fileChannel
      • idx

        private long idx
    • Constructor Detail

      • ZeroCopyConsumer

        public ZeroCopyConsumer​(java.io.File file)
                         throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
    • Method Detail

      • onResponseReceived

        protected void onResponseReceived​(org.apache.http.HttpResponse response)
        Specified by:
        onResponseReceived in class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
      • onEntityEnclosed

        protected void onEntityEnclosed​(org.apache.http.HttpEntity entity,
                                        org.apache.http.entity.ContentType contentType)
                                 throws java.io.IOException
        Specified by:
        onEntityEnclosed in class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
        Throws:
        java.io.IOException
      • onContentReceived

        protected void onContentReceived​(org.apache.http.nio.ContentDecoder decoder,
                                         org.apache.http.nio.IOControl ioctrl)
                                  throws java.io.IOException
        Specified by:
        onContentReceived in class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
        Throws:
        java.io.IOException
      • process

        protected abstract T process​(org.apache.http.HttpResponse response,
                                     java.io.File file,
                                     org.apache.http.entity.ContentType contentType)
                              throws java.lang.Exception
        Invoked to process received file.
        Parameters:
        response - original response head.
        file - file containing response content.
        contentType - the cotnent type.
        Returns:
        result of the response processing
        Throws:
        java.lang.Exception
      • buildResult

        protected T buildResult​(org.apache.http.protocol.HttpContext context)
                         throws java.lang.Exception
        Specified by:
        buildResult in class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
        Throws:
        java.lang.Exception
      • releaseResources

        protected void releaseResources()
        Specified by:
        releaseResources in class org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>