public class InputStreamLineBuffer
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 InputStreamBuffer
if you are interested in the raw bytes,
InputStreamSink
if you are not interested at all 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 |
DEFAULT_BUFFER_SIZE
Default size for line buffers.
|
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 |
---|
InputStreamLineBuffer(java.io.InputStream is,
int maxSize)
Constructor using the system's default charset.
|
InputStreamLineBuffer(java.io.InputStream is,
int maxSize,
java.nio.charset.Charset charset)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
clearOutput()
Clear the output.
|
java.io.IOException |
getError()
Return a possible error which happened during reading the stream
when using the
run() method. |
java.lang.String[] |
getLines()
Get the collected output lines.
|
java.lang.String |
getOutput()
Get the output as text, using the default charset.
|
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 InputStreamLineBuffer |
startOn(java.io.InputStream inStream)
Start a sink on the given stream.
|
static InputStreamLineBuffer |
startOn(java.io.InputStream inStream,
int bufferSize)
Start a sink on the given stream.
|
Indexable<java.lang.String> |
toLineIndexable()
Get the buffered lines as an independent indexable.
|
java.util.List<java.lang.String> |
toLineList()
Get the buffered lines as an independent indexable.
|
java.lang.String |
toString() |
Indexable<java.lang.String> |
viewLines()
Get a view to the buffered lines.
|
public static final int MAXIMUM_ARRAY_SIZE
public static final int DEFAULT_BUFFER_SIZE
public InputStreamLineBuffer(@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 number of buffered lines, implementation might
store one line morepublic InputStreamLineBuffer(@NotNull java.io.InputStream is, int maxSize, @NotNull java.nio.charset.Charset charset)
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 number of buffered lines, implementation might
store one line morecharset
- charset used to decode the input stream@Nullable public java.io.IOException getError()
run()
method.null
,
if no error was caught@NotNull public Indexable<java.lang.String> viewLines()
toLineIndexable()
,
toLineList()
, or getLines()
.toLineIndexable()
,
toLineList()
,
getLines()
@NotNull public Indexable<java.lang.String> toLineIndexable()
viewLines()
,
toLineList()
,
getLines()
@NotNull public java.util.List<java.lang.String> toLineList()
viewLines()
,
toLineIndexable()
,
getLines()
@NotNull public java.lang.String[] getLines()
viewLines()
,
toLineIndexable()
,
toLineList()
@NotNull public java.lang.String getOutput()
public 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 InputStreamLineBuffer startOn(@NotNull java.io.InputStream inStream)
DEFAULT_BUFFER_SIZE
as buffer size.inStream
- input streamstartOn(InputStream, int)
@NotNull public static InputStreamLineBuffer startOn(@NotNull java.io.InputStream inStream, int bufferSize)
inStream
- input streambufferSize
- maximum number of cached linespublic java.lang.String toString()
toString
in class java.lang.Object