public class MutableStringBufferInputStream extends InputStream
System.in
. It can be used as a
"smart" replacement for System.in
that allows the console
input to be provided by string contents, and allows the console
input to be reset whenever needed.
Note that it is possible to create a
MutableStringBufferInputStream
with null contents. Such
a stream is in an "unusable" state where no InputStream
methods
can be called until actual contents have been provided at some later point.
Any calls to InputStream
methods when a stream is unusable
are instead preempted, and a protected error handling method called
handleMissingContents()
is invoked instead, usually producing
an IllegalStateException
. The handleMissingContents()
method is protected, so that subclasses can override it to provide
customized error responses if desired.
At any point in time, resetContents(String)
(or
resetContents(InputStream)
) can be used to change the stream's
contents to a new value. This completely resets the stream's state.
Constructor and Description |
---|
MutableStringBufferInputStream(InputStream contents)
Create a new input stream with the specified contents.
|
MutableStringBufferInputStream(String contents)
Create a new input stream with the specified contents.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
String |
getName()
Get the human-readable name of this input stream, for diagnostic or
debugging purposes.
|
protected void |
handleMissingContents()
Handle access to an input stream that has not had its contents
defined.
|
int |
read() |
int |
read(byte[] b,
int off,
int len) |
void |
reset() |
void |
resetContents(InputStream newContents)
Reset this stream and replace its contents.
|
void |
resetContents(String newContents)
Reset this stream and replace its contents.
|
void |
setName(String name)
Set the human-readable name of this input stream, for diagnostic or
debugging purposes.
|
long |
skip(long n) |
close, mark, markSupported, read
public MutableStringBufferInputStream(String contents)
InputStream
methods cannot be called until actual contents
have been provided at some later point. Any attempt to access an
unusable stream is preempted by a call to
handleMissingContents()
, which typically produces an
exception. Contents can be changed at any time using
resetContents()
.contents
- The complete contents of this input stream, provided
as a String
.public MutableStringBufferInputStream(InputStream contents)
InputStream
methods cannot be called until actual contents
have been provided at some later point. Any attempt to access an
unusable stream is preempted by a call to
handleMissingContents()
, which typically produces an
exception. Contents can be changed at any time using
resetContents()
.contents
- A second input stream to draw contents from. All
stream operations will be delegated to this object,
until resetContents()
is called.public String getName()
setName(String)
where desired.public void setName(String name)
name
- The new name to use for this stream.public void resetContents(String newContents)
InputStream
methods cannot be called until actual contents
have been provided at some later point. Any attempt to access an
unusable stream is preempted by a call to
handleMissingContents()
, which typically produces an
exception. Contents can be changed again at any time using
resetContents()
.newContents
- The new contents (possibly null) for this stream.public void resetContents(InputStream newContents)
InputStream
methods cannot be called until actual contents
have been provided at some later point. Any attempt to access an
unusable stream is preempted by a call to
handleMissingContents()
, which typically produces an
exception. Contents can be changed again at any time using
resetContents()
.newContents
- The new contents (possibly null) for this stream.protected void handleMissingContents()
IllegalStateException
indicating that the stream
has not had its contents set. Subclasses can override this
method to provide custom error handling behaviors as needed.public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public long skip(long n) throws IOException
skip
in class InputStream
IOException
public int available() throws IOException
available
in class InputStream
IOException
public void reset() throws IOException
reset
in class InputStream
IOException