|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.InputStream
com.tffenterprises.io.ByteArrayInputStream
public class ByteArrayInputStream
com.tffenterprises.io.ByteArrayInputStream
was originally
intended as
a drop-in replacement for java.io.ByteArrayInputStream
.
It can function in exactly the same way as the standard java class,
with the advantage that the method
read(byte[] b)
neither declares nor throws any exceptions.
It also departs from the original in that ByteArrayInputStream
also implements the java.io.DataInput
interface,
with methods that only throw the exception java.io.EOFException
as exceptions (when the buffer has run out of bytes).
A final point in which it differs from the original is that it implements
the AccountingInput
interface, keeping track of the number of read/skipped bytes.
The buffer that a ByteArrayInputStream
encapsulates
is a byte array. Internal variables keeps track of the next byte
to be supplied by the read method (the variable pos
)
and of the last byte that can
be supplied by the stream (the variable last
.
The current implementation makes no effort to be thread safe.
Field Summary | |
---|---|
private byte[] |
buf
The byte array from which the bytes are to be read. |
private int |
first
The first byte that can be read from the byte array. |
private int |
last
The last byte that can be read from the byte array. |
private int |
mark
The earliest byte that can be read from the byte array. |
private int |
pos
The current read position in the byte array. |
Constructor Summary | |
---|---|
private |
ByteArrayInputStream()
Private default constructor, useful for a hypothetical clone() method. |
|
ByteArrayInputStream(byte[] buf)
Creates a ByteArrayInputStream so that it uses
buf as its buffer array. |
|
ByteArrayInputStream(byte[] buf,
int offset,
int length)
Creates a ByteArrayInputStream that uses buf
as its buffer array. |
Method Summary | |
---|---|
int |
available()
Returns the number of bytes that remain to be read in the byte array. |
void |
close()
Closes this stream. |
long |
consumed()
Returns the number of bytes that have been read from the byte array. |
void |
mark(int readLimit)
Set the current marked position in the stream. |
boolean |
markSupported()
Reports that the mark() method from InputStream is
supported. |
int |
read()
Reads the next byte of data from the byte array. |
int |
read(byte[] b)
Reads a number of bytes of data into an array of bytes from this input stream. |
int |
read(byte[] b,
int offset,
int length)
Reads a number of bytes of data into an array of bytes from this input stream. |
boolean |
readBoolean()
Returns a boolean value from the encapsulated byte array. |
byte |
readByte()
Returns a byte value from the encapsulated byte array. |
char |
readChar()
Returns a char value from the byte array. |
double |
readDouble()
Returns a double value from the encapsulated byte array. |
float |
readFloat()
Returns a float value from the encapsulated byte array. |
void |
readFully(byte[] b)
Reads some bytes from an input stream and stores them into the buffer array b. |
void |
readFully(byte[] b,
int offset,
int length)
Reads some byte from an input stream and stores them into the buffer array b, starting from the offset offset . |
int |
readInt()
Returns an int value from the encapsulated byte array. |
java.lang.String |
readLine()
Returns a string representing one line of data (encoded as ISO-8859-1). |
long |
readLong()
Returns a long value from the encapsulated byte array. |
short |
readShort()
Returns a short value from the encapsulated byte array. |
int |
readUnsignedByte()
Returns an unsigned byte value from the encapsulated byte array. |
int |
readUnsignedShort()
Returns an unsigned short value from the bencapsulated yte array. |
java.lang.String |
readUTF()
Returns a String that had been encoded as the java modified UTF-8 format. |
void |
reset()
Resets the buffer to the marked position. |
long |
skip(long n)
Skips n bytes of input from this input stream. |
int |
skipBytes(int n)
Attempt to skip n bytes from the buffer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private byte[] buf
private int pos
private int last
private int first
private int mark
Constructor Detail |
---|
private ByteArrayInputStream()
public ByteArrayInputStream(byte[] buf) throws java.lang.IllegalArgumentException
ByteArrayInputStream
so that it uses
buf
as its buffer array. The buffer array is not
copied.
Using this constructor is equivalent to using
ByteArrayInputStream(buf, 0, buf.length)
.
buf
- the input buffer.
java.lang.IllegalArgumentException
- if buf is null.public ByteArrayInputStream(byte[] buf, int offset, int length) throws java.lang.IllegalArgumentException
ByteArrayInputStream
that uses buf
as its buffer array. The initial value of pos
is
offset
and the initial value of count
is offset+len
. The buffer array is not copied.
Unlike the
java.io.ByteArrayInputStream
class, elements
buf[0]
through buf[pos-1]
cannot become
available for input.
buf
- the input buffer.offset
- the offset in the buffer of the first byte to read.length
- the maximum number of bytes to read from the buffer.
java.lang.IllegalArgumentException
- if buf is null.Method Detail |
---|
public int available()
last - pos
, which is the number of
bytes remaining to be read from the byte array.
available
in class java.io.InputStream
public long consumed()
pos - first
.
consumed
in interface AccountingInput
public boolean markSupported()
InputStream
is
supported.
markSupported
in class java.io.InputStream
mark()
method works.public void mark(int readLimit)
ByteArrayInputStream
objects are marked at their initial
position when constructed. They may be marked at another position
within the buffer by this method.
mark
in class java.io.InputStream
readLimit
- the number of bytes to be read before the marked
position gets invalidated. This parameter is ignored.public void reset()
reset
in class java.io.InputStream
public void close()
close
in interface java.io.Closeable
close
in class java.io.InputStream
public int read()
This method cannot block.
read
in class java.io.InputStream
public int read(byte[] b)
pos
equals count
,
then -1 is returned to indicate an end-of-file situation.
Otherwise, the number k
of bytes read is equal to the smaller
of buf.length and count-pos
. If k
is positive, then bytes buf[pos]
through
buf[pos+k-1]
are copied into b[0]
through
b[k-1]
in the manner performed by System.arraycopy. The
value k
is added into pos
and k
is returned.
This read method cannot block.
- Overrides:
read
in class java.io.InputStream
- Parameters:
b
- the buffer into which the data is read from the encapsulated
byte array.
- Returns:
- the total number of bytes read into the buffer, or -1 if there is
no more data because the end of the stream has been reached.
public int read(byte[] b, int offset, int length)
pos
equals last
,
then -1 is returned to indicate an end-of-file situation.
Otherwise, the number k
of bytes read is equal to the smaller
of buf.length and last-pos
. If k
is positive, then bytes buf[pos]
through
buf[pos+k-1]
are copied into b[0]
through
b[k-1]
in the manner performed by System.arraycopy. The
value k
is added into pos
and k
is returned.
This read method cannot block.
- Overrides:
read
in class java.io.InputStream
- Parameters:
b
- the buffer into which to copy the bytes to be read.offset
- the offset at which to start copying bytes into b.length
- the number of bytes to be read and copied.
- Returns:
- the total number of bytes read into the buffer, or -1 if there is
no more data because the end of the stream has been reached.
public long skip(long n)
n
and
last-pos
. The number of bytes that were skipped is returned.
This skip method cannot block.
skip
in class java.io.InputStream
n
- the number of bytes to be skipped.
public boolean readBoolean() throws java.io.EOFException
readBoolean
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public final byte readByte() throws java.io.EOFException
readByte
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public int readUnsignedByte() throws java.io.EOFException
readUnsignedByte
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public final short readShort() throws java.io.EOFException
readShort
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public int readUnsignedShort() throws java.io.EOFException
readUnsignedShort
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public final char readChar() throws java.io.EOFException
readChar
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public int readInt() throws java.io.EOFException
readInt
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public long readLong() throws java.io.EOFException
readLong
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public final float readFloat() throws java.io.EOFException
readFloat
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public final double readDouble() throws java.io.EOFException
readDouble
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.public java.lang.String readLine()
If end of file is encountered before even one byte can be read, then null is returned. Otherwise, each byte that is read is converted to type char by zero-extension. If the character '\n' is encountered, it is discarded and reading ceases. If the character '\r' is encountered, it is discarded and, if the following byte converts to the character '\n', then that is discarded also; reading then ceases. If end of file is encountered before either of the characters '\n' and '\r' is encountered, reading ceases. Once reading has ceased, a String is returned that contains all the characters read and not discarded, taken in order. Note that every character in this string will have a value less than ?, that is, (char)256.
readLine
in interface java.io.DataInput
public java.lang.String readUTF() throws java.io.EOFException, java.io.UTFDataFormatException
readUTF
in interface java.io.DataInput
java.io.EOFException
- if EOF is reached before the bytes are read.
java.io.UTFDataFormatException
- if the UTF data is malformed.public void readFully(byte[] b) throws java.io.EOFException
readFully
in interface java.io.DataInput
b
- the buffer into which to copy the bytes to be read.
java.io.EOFException
- if the end of the input buffer is reached before
all the bytes have been read.public void readFully(byte[] b, int offset, int length) throws java.io.EOFException
offset
. The number
of bytes read is equal to the parameter length
readFully
in interface java.io.DataInput
b
- the buffer into which to copy the bytes to be read.offset
- the offset at which to start copying bytes into b.length
- the number of bytes to be read and copied.
java.io.EOFException
- if the end of the input buffer is reached before
all the bytes have been read.public int skipBytes(int n)
skipBytes
in interface java.io.DataInput
n
- the number of bytes to be skipped.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |