Class BufferPublisherInputStream

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

    public final class BufferPublisherInputStream
    extends java.io.InputStream
    An InputStream built around a Publisher<Buffer>, which can either be read OIO style or provide its wrapped Publisher. This allows us to provide JAX-RS with an InputStream and also short-circuit its usage when our code can directly deal with the Publisher<Buffer> it wraps.

    Not threadsafe and intended to be used internally only, where no concurrency occurs between read(), read(byte[], int, int) and bufferPublisher().

    • Constructor Detail

      • BufferPublisherInputStream

        public BufferPublisherInputStream​(Publisher<Buffer> publisher,
                                          int queueCapacity)
        Creates a new BufferPublisherInputStream instance.
        Parameters:
        publisher - the Publisher<Buffer> to read from.
        queueCapacity - the capacity hint for the intermediary queue that stores items.
    • Method Detail

      • 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
      • handleEntityStream

        public static <T> T handleEntityStream​(java.io.InputStream entityStream,
                                               BufferAllocator allocator,
                                               java.util.function.BiFunction<Publisher<Buffer>,​BufferAllocator,​T> bufferPublisherHandler,
                                               java.util.function.BiFunction<java.io.InputStream,​BufferAllocator,​T> inputStreamHandler)
        Helper method for dealing with a request entity InputStream that is potentially a BufferPublisherInputStream.
        Type Parameters:
        T - the type of data returned by the BiFunctions.
        Parameters:
        entityStream - the request entity InputStream
        allocator - the BufferAllocator to use
        bufferPublisherHandler - a BiFunction that is called in case the entity InputStream is a BufferPublisherInputStream
        inputStreamHandler - a BiFunction that is called in case the entity InputStream is not a BufferPublisherInputStream
        Returns:
        the data returned by one of the BiFunction.