BufferedConnectionAdapterpublic abstract class BufferedConnectionAdapter extends ConnectionBaseAdapter This class adds read buffering to the
ConnectionBaseAdapter .
Implements {@link InputStream#available()}, however a subclass must
specify a buffer size greater than 0 and override
{@link #readBytesNonBlocking(byte[], int, int)} in order to for available
to work properly. |
Fields Summary |
---|
protected boolean | eofThe end of file flag. | protected byte[] | bufThe internal buffer array where the data is stored.
When necessary, it may be replaced by another array
of a different size. | protected int | countThe index one greater than the index of the last valid
byte in the buffer.
This value is always in the range
0 through buf.length ;
elements buf[0] through buf[count-1]
contain buffered input data obtained
from the underlying input stream. | protected int | posThe current position in the buffer. This is the index
of the next character to be read from the
buf array.
This value is always in the range 0
through count . If it is less
than count , then buf[pos]
is the next byte to be supplied as input;
if it is equal to count , then
the next read or skip
operation will require more bytes to be
read from the contained input stream. |
Constructors Summary |
---|
protected BufferedConnectionAdapter(int sizeOfBuffer)Initializes the connection.
if (sizeOfBuffer > 0) {
buf = new byte[sizeOfBuffer];
}
|
Methods Summary |
---|
public int | available()Returns the number of bytes that can be read (or skipped over) from
this input stream without blocking by the next caller of a method for
this input stream. The next caller might be the same thread or
another thread.
The available method always returns 0 if
{@link #readBytesNonBlocking(byte[], int, int)} is
not overridden by the subclass or there is not buffer.
int bytesRead;
if (buf == null) {
return 0;
}
if (count > 0) {
return count;
}
bytesRead = readBytesNonBlocking(buf, 0, buf.length);
if (bytesRead == -1) {
return 0;
}
/*
* Reset the current buffer position and count of bytes
* available. These variables must be reset to match
* the processing in readBytes.
*/
pos = 0;
count = bytesRead;
return count;
| protected abstract int | nonBufferedRead(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into
an array of bytes, blocks until at least one byte is available.
Sets the eof field of the connection when there is
no more data in the stream to read.
| public int | readBytes(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into
an array of bytes, blocks until at least one byte is available.
Do not use this method if openInputStream has been called
since the input stream may be buffering data.
int bytesRead;
if (count == 0) {
if (eof) {
return -1;
}
if (buf == null || len >= buf.length) {
return nonBufferedRead(b, off, len);
} else {
int res = nonBufferedRead(buf, 0, buf.length);
pos = 0;
if (res <= 0) {
return res;
} else {
count = res;
}
}
}
if (len > count) {
len = count;
}
System.arraycopy(buf, pos, b, off, len);
count -= len;
pos += len;
return len;
| protected int | readBytesNonBlocking(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into
an array of bytes, but does not block if no bytes available. A subclass
should implement this to so the available method on the InputStream
will be useful.
Sets the eof field of the connection when the native read
returns -1.
The readBytesNonBlocking method of
ConnectionBaseAdapter does nothing and returns 0.
return 0;
|
|