public class ByteArrayRandomStream extends RandomStream
Modifier and Type | Field and Description |
---|---|
ByteArray |
data
This is the data being read from or written to.
|
READ_ONLY, READ_WRITE
Constructor and Description |
---|
ByteArrayRandomStream()
Create a new ByteArrayRandomStream that you can read and write
to.
|
ByteArrayRandomStream(byte[] bytes,
int start,
int length,
String mode)
Create a ByteArray random stream from an array of bytes.
|
ByteArrayRandomStream(ByteArray dataToUse,
String mode) |
ByteArrayRandomStream(InputStream in,
String mode)
This creates a ByteArrayRandomStream from an input File.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
This method closes the stream.
|
long |
getLength()
Return the full length of the Stream.
|
long |
getPosition()
Return the current read/write stream pointer.
|
int |
read()
This method reads an unsigned byte from the input stream and returns it
as an int in the range of 0-255.
|
int |
read(byte[] buf,
int start,
int count)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
void |
setLength(long length)
Attempt to set the length of the stream if this is supported.
|
void |
setPosition(long pos)
Set the Stream position.
|
void |
write(byte[] buf,
int start,
int count)
This method writes
len bytes from the specified array
b starting at index off into the array. |
void |
write(int ch)
This method writes a single byte to the output stream.
|
canSetLength, flush, isOpenForWriting, isReadWrite, mark, markSupported, reset, setMode, skip, throwCantWrite, toOutputStream, validateMode, write
available, finalize, hashCode, read
public ByteArray data
public ByteArrayRandomStream()
public ByteArrayRandomStream(byte[] bytes, int start, int length, String mode)
bytes
- start
- length
- mode
- either "r" or "rw".public ByteArrayRandomStream(InputStream in, String mode) throws IOException
IOException
public void close() throws IOException
InputStream
IOException
This method does nothing in this class, but subclasses may override this method in order to provide additional functionality.
close
in class InputStream
IOException
- If an error occurs, which can only happen
in a subclasspublic int read(byte[] buf, int start, int count) throws IOException
InputStream
off
into the buffer and attempts to read
len
bytes. This method can return before reading the
number of bytes requested. The actual number of bytes read is
returned as an int. A -1 is returned to indicate the end of the
stream.
This method will block until some data can be read.
This method operates by calling the single byte read()
method
in a loop until the desired number of bytes are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a bytes fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read
in class InputStream
buf
- The array into which the bytes read should be storedstart
- The offset into the array to start storing bytescount
- The requested number of bytes to readIOException
- If an error occurs.public int read() throws IOException
InputStream
This method will block until the byte can be read.
read
in class InputStream
IOException
- If an error occurspublic void write(byte[] buf, int start, int count) throws IOException
RandomStream
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.
write
in class RandomStream
buf
- The array of bytes to write fromstart
- The index into the array to start writing fromcount
- The number of bytes to writeIOException
- If an error occurspublic void write(int ch) throws IOException
RandomStream
int
passed as the argument.
Subclasses must provide an implementation of this method.
write
in class RandomStream
ch
- The byte to be written to the output stream, passed as
the low eight bits of an int
IOException
- If an error occurspublic long getLength() throws IOException
RandomStream
getLength
in class RandomStream
IOException
- if an IO error occured.public void setPosition(long pos) throws IOException
RandomStream
setPosition
in class RandomStream
pos
- the new position to set.IOException
- on error.public long getPosition() throws IOException
RandomStream
getPosition
in class RandomStream
IOException
- on error.public void setLength(long length) throws IOException
RandomStream
setLength
in class RandomStream
IOException
- if the length cannot be set or if there was an error.