public class InputStreamBuffer
extends java.lang.Object
implements java.lang.Runnable
It is especially useful for handling the outputting streams of Process
.
Create via startOn(InputStream)
or startOn(InputStream, int)
which will automatically collect all bytes from the stream in a background thread.
See InputStreamSink
if you are not interested in the content of the streams,
and InputStreamPipe
which forwards all bytes from one stream to another.
Modifier and Type | Field and Description |
---|---|
static int |
MAXIMUM_ARRAY_SIZE
This is suggestion from OpenJDK's ArraySupport class which makes sure to work with all Java versions.
|
Constructor and Description |
---|
InputStreamBuffer(java.io.InputStream is,
int maxSize)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
clearOutput()
Clear the output.
|
byte[] |
getBinaryOutput()
Get the collected output as binary data.
|
java.io.IOException |
getError()
Return a possible error which happened during reading the stream
when using the
run() method. |
java.lang.String |
getOutput()
Get the output as text, using the default charset.
|
java.lang.String |
getOutput(java.nio.charset.Charset charset)
Get the output as text, using the given charset
Note that due to limited buffer size for multi-byte encodings
such as UTF-8 the first characters may be broken.
|
long |
getTotalByteCount()
Get the total number of bytes read by this sink.
|
void |
run()
When an object implementing interface
Runnable is used
to create a thread, starting the thread causes the object's
run method to be called in that separately executing
thread. |
static InputStreamBuffer |
startOn(java.io.InputStream inStream)
Start a buffer on the given stream.
|
static InputStreamBuffer |
startOn(java.io.InputStream inStream,
int bufferSize)
Start a buffer on the given stream.
|
java.lang.String |
toString() |
public static final int MAXIMUM_ARRAY_SIZE
public InputStreamBuffer(@NotNull java.io.InputStream is, int maxSize)
Note that in most use cases the factory methods startOn(InputStream)
and startOn(InputStream, int)
are better ways to
create an input stream sink.
is
- input streammaxSize
- maximum buffer size@Nullable public java.io.IOException getError()
run()
method.null
,
if no error was caught@NotNull public byte[] getBinaryOutput()
@NotNull public java.lang.String getOutput()
@NotNull public java.lang.String getOutput(@NotNull java.nio.charset.Charset charset)
charset
- charset to use for decoding the binary outputpublic void clearOutput()
public long getTotalByteCount()
public void run()
Runnable
is used
to create a thread, starting the thread causes the object's
run
method to be called in that separately executing
thread.
The general contract of the method run
is that it may
take any action whatsoever.
run
in interface java.lang.Runnable
Thread.run()
@NotNull public static InputStreamBuffer startOn(@NotNull java.io.InputStream inStream)
MAXIMUM_ARRAY_SIZE
as size for the ring buffer.inStream
- input stream@NotNull public static InputStreamBuffer startOn(@NotNull java.io.InputStream inStream, int bufferSize)
inStream
- input streambufferSize
- maximum number of cached bytespublic java.lang.String toString()
toString
in class java.lang.Object