public abstract class RandomStream extends InputStream
Modifier and Type | Field and Description |
---|---|
static String |
READ_ONLY |
static String |
READ_WRITE |
Modifier | Constructor and Description |
---|---|
protected |
RandomStream() |
protected |
RandomStream(String mode) |
Modifier and Type | Method and Description |
---|---|
boolean |
canSetLength()
Return if this Stream has the capability to set the stream length in Read/Write mode.
|
void |
flush()
This method forces any data that may have been buffered to be written
to the underlying output device.
|
long |
getLength()
Return the full length of the Stream.
|
long |
getPosition()
Return the current read/write stream pointer.
|
boolean |
isOpenForWriting()
Returns true if this Stream was open in Read-Write mode as opposed to Read-Only mode.
|
static boolean |
isReadWrite(String mode)
Checks if the specified mode is read/write or read only.
|
void |
mark(int readLimit)
This method marks a position in the input to which the stream can
be "reset" by calling the
reset() method. |
boolean |
markSupported()
This method returns a boolean that indicates whether the mark/reset
methods are supported in this class.
|
void |
reset()
This method resets a stream to the point where the
mark() method was called. |
void |
setLength(long length)
Attempt to set the length of the stream if this is supported.
|
protected void |
setMode(String mode)
Set the mode of this RandomStream.
|
void |
setPosition(long newPosition)
Set the Stream position.
|
long |
skip(long num)
This method skips the specified number of bytes in the stream.
|
protected void |
throwCantWrite()
Throw an IOException indicating that you cannot write to this RandomStream.
|
OutputStream |
toOutputStream()
Get an OutputStream for writing to the RandomStream.
|
static String |
validateMode(String mode)
Validate the specified mode, which must be "r" or "rw".
|
void |
write(byte[] b)
This method all the writes bytes from the passed array to the
output stream.
|
void |
write(byte[] data,
int offset,
int count)
This method writes
len bytes from the specified array
b starting at index off into the array. |
void |
write(int b)
This method writes a single byte to the output stream.
|
public static final String READ_ONLY
public static final String READ_WRITE
protected RandomStream(String mode) throws IllegalArgumentException
IllegalArgumentException
protected RandomStream()
public static String validateMode(String mode) throws IllegalArgumentException
mode
- the mode to validate.IllegalArgumentException
- if the mode parameter is not valid.public static boolean isReadWrite(String mode) throws IllegalArgumentException
mode
- the mode.IllegalArgumentException
- if it is not "r" or "rw".public long getLength() throws IOException
IOException
- if an IO error occured.public void setLength(long length) throws IOException
IOException
- if the length cannot be set or if there was an error.public long getPosition() throws IOException
IOException
- on error.public void setPosition(long newPosition) throws IOException
newPosition
- the new position to set.IOException
- on error.public boolean isOpenForWriting()
public boolean canSetLength()
protected void setMode(String mode) throws IllegalArgumentException
mode
- either "r" or "rw". Any other throws an IllegalArgumentExceptionIllegalArgumentException
- if the mode is not "r" or "rw"public OutputStream toOutputStream() throws IOException
IOException
- if an OutputStream could not be created.public void mark(int readLimit)
InputStream
reset()
method. The
parameter @code{readlimit} is the number of bytes that can be read
from the stream after setting the mark before the mark becomes
invalid. For example, if mark()
is called with a
read limit of 10, then when 11 bytes of data are read from the
stream before the reset()
method is called, then the
mark is invalid and the stream object instance is not required to
remember the mark.
This method does nothing in this class, but subclasses may override it to provide mark/reset functionality.
mark
in class InputStream
public void reset() throws IOException
InputStream
mark()
method was called. Any bytes that were read
after the mark point was set will be re-read during subsequent
reads.
This method always throws an IOException in this class, but subclasses can override this method if they provide mark/reset functionality.
reset
in class InputStream
IOException
- Always thrown for this classpublic boolean markSupported()
InputStream
This method always returns false
in this class, but
subclasses can override this method to return true
if they support mark/reset functionality.
markSupported
in class InputStream
true
if mark/reset functionality is
supported, false
otherwisepublic long skip(long num) throws IOException
InputStream
This method reads and discards bytes into a byte array until the specified number of bytes were skipped or until either the end of stream is reached or a read attempt returns a short count. Subclasses can override this metho to provide a more efficient implementation where one exists.
skip
in class InputStream
num
- The requested number of bytes to skipIOException
- If an error occurspublic void write(int b) throws IOException
int
passed as the argument.
Subclasses must provide an implementation of this method.
b
- The byte to be written to the output stream, passed as
the low eight bits of an int
IOException
- If an error occurspublic void write(byte[] b) throws IOException, NullPointerException
write(b, 0,
buf.length)
which is exactly how it is implemented in this
class.b
- The array of bytes to writeIOException
- If an error occursNullPointerException
public void write(byte[] data, int offset, int count) throws IOException, NullPointerException, IndexOutOfBoundsException
len
bytes from the specified array
b
starting at index off
into the array.
This method in this class calls the single byte write()
method in a loop until all bytes have been written. Subclasses should
override this method if possible in order to provide a more efficent
implementation.
data
- The array of bytes to write fromoffset
- The index into the array to start writing fromcount
- The number of bytes to writeIOException
- If an error occursNullPointerException
IndexOutOfBoundsException
public void flush() throws IOException
This method in this class does nothing.
IOException
- If an error occursprotected void throwCantWrite() throws IOException
IOException
- always thrown.