Class OutputStreamContentProvider

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable<java.nio.ByteBuffer>, ContentProvider, AsyncContentProvider, Callback, Invocable

    public class OutputStreamContentProvider
    extends java.lang.Object
    implements AsyncContentProvider, Callback, java.io.Closeable
    A ContentProvider that provides content asynchronously through an OutputStream similar to DeferredContentProvider.

    OutputStreamContentProvider can only be used in conjunction with Request.send(Response.CompleteListener) (and not with its blocking counterpart Request.send()) because it provides content asynchronously.

    The deferred content is provided once by writing to the output stream and then fully consumed. Invocations to the iterator() method after the first will return an "empty" iterator because the stream has been consumed on the first invocation. However, it is possible for subclasses to support multiple invocations of iterator() by overriding write(ByteBuffer) and close(), copying the bytes and making them available for subsequent invocations.

    Content must be provided by writing to the output stream, that must be closed when all content has been provided.

    Example usage:

     HttpClient httpClient = ...;
    
     // Use try-with-resources to autoclose the output stream
     OutputStreamContentProvider content = new OutputStreamContentProvider();
     try (OutputStream output = content.getOutputStream())
     {
         httpClient.newRequest("localhost", 8080)
                 .content(content)
                 .send(new Response.CompleteListener()
                 {
                     @Override
                     public void onComplete(Result result)
                     {
                         // Your logic here
                     }
                 });
    
         // At a later time...
         output.write("some content".getBytes());
     }
     
    • Constructor Detail

      • OutputStreamContentProvider

        public OutputStreamContentProvider()
    • Method Detail

      • getLength

        public long getLength()
        Specified by:
        getLength in interface ContentProvider
        Returns:
        the content length, if known, or -1 if the content length is unknown
      • iterator

        public java.util.Iterator<java.nio.ByteBuffer> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.nio.ByteBuffer>
      • getOutputStream

        public java.io.OutputStream getOutputStream()
      • write

        protected void write​(java.nio.ByteBuffer buffer)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • failed

        public void failed​(java.lang.Throwable failure)
        Description copied from interface: Callback

        Callback invoked when the operation fails.

        Specified by:
        failed in interface Callback
        Parameters:
        failure - the reason for the operation failure