Package no.digipost.io
Class ConsumingInputStream
- java.lang.Object
-
- java.io.InputStream
-
- no.digipost.io.ConsumingInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class ConsumingInputStream extends InputStream
AnInputStreamgetting its contents from consuming anOutputStream. The InputStream will be immediately available for reading after construction, regardless of how much data that will be produced by the OutputStream. This class abstracts the use of aPipedInputStreamand aPipedOutputStream, hiding the complexities required to orchestrate this multithreaded combination of streams.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConsumingInputStream.ProducerFailed
-
Constructor Summary
Constructors Constructor Description ConsumingInputStream(ExecutorService executorService, Consumer<? super OutputStream> write)Create a newConsumingInputStreamwhich will provide data once they are written to anOutputStreampassed to the givenConsumer.ConsumingInputStream(ExecutorService executorService, Function<OutputStream,S> outputStreamDecorator, Consumer<? super S> write)Create a newConsumingInputStreamwhich will provide data once they are written to anOutputStreampassed to the givenConsumer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()voidmark(int readlimit)booleanmarkSupported()intread()intread(byte[] b)intread(byte[] b, int off, int len)voidreset()longskip(long n)-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Constructor Detail
-
ConsumingInputStream
public ConsumingInputStream(ExecutorService executorService, Consumer<? super OutputStream> write)
Create a newConsumingInputStreamwhich will provide data once they are written to anOutputStreampassed to the givenConsumer.- Parameters:
executorService- The executorService to use to start producing data which will be readable by this inputstream.write- The data producing logic. TheConsumerwill be given theOutputStreamresulting from theoutputStreamDecorator.
-
ConsumingInputStream
public ConsumingInputStream(ExecutorService executorService, Function<OutputStream,S> outputStreamDecorator, Consumer<? super S> write)
Create a newConsumingInputStreamwhich will provide data once they are written to anOutputStreampassed to the givenConsumer.- Parameters:
executorService- The executorService to use to start producing data which will be readable by this inputstream.outputStreamDecorator- AFunctionto wrap the outputstream to push data to. ThisFunctionmust always wrap the given OutputStream in the OutputStream returned from thisFunction, i.e. the resulting OutputStream must be constructed by wrapping the given OutputStream as a constructor argument. (E.g.ZipOutputStream::new.write- The data producing logic. TheConsumerwill be given theOutputStreamresulting from theoutputStreamDecorator.
-
-
Method Detail
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
read
public int read(byte[] b) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
skip
public long skip(long n) throws IOException- Overrides:
skipin classInputStream- Throws:
IOException
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
mark
public void mark(int readlimit)
- Overrides:
markin classInputStream
-
reset
public void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classInputStream
-
-