Input stream with two modes: in default mode, inputs data written in the
same format as DataOutputStream; in "block data" mode, inputs data
bracketed by block data markers (see object serialization specification
for details). Buffering depends on block data mode: when in default
mode, no data is buffered in advance; when in block data mode, all data
for the current data block is read in at once (and buffered).
| Method from java.io.ObjectInputStream$BlockDataInputStream Detail: |
public int available() throws IOException {
if (blkmode) {
if ((pos == end) && (unread == 0)) {
int n;
while ((n = readBlockHeader(false)) == 0) ;
switch (n) {
case HEADER_BLOCKED:
break;
case -1:
pos = 0;
end = -1;
break;
default:
pos = 0;
end = 0;
unread = n;
break;
}
}
// avoid unnecessary call to in.available() if possible
int unreadAvail = (unread > 0) ?
Math.min(in.available(), unread) : 0;
return (end >= 0) ? (end - pos) + unreadAvail : 0;
} else {
return in.available();
}
}
|
public void close() throws IOException {
if (blkmode) {
pos = 0;
end = -1;
unread = 0;
}
in.close();
}
|
int currentBlockRemaining() {
if (blkmode) {
return (end >= 0) ? (end - pos) + unread : 0;
} else {
throw new IllegalStateException();
}
}
If in block data mode, returns the number of unconsumed bytes
remaining in the current data block. If not in block data mode,
throws an IllegalStateException. |
boolean getBlockDataMode() {
return blkmode;
}
Returns true if the stream is currently in block data mode, false
otherwise. |
int peek() throws IOException {
if (blkmode) {
if (pos == end) {
refill();
}
return (end >= 0) ? (buf[pos] & 0xFF) : -1;
} else {
return in.peek();
}
}
Peeks at (but does not consume) and returns the next byte value in
the stream, or -1 if the end of the stream/block data (if in block
data mode) has been reached. |
byte peekByte() throws IOException {
int val = peek();
if (val < 0) {
throw new EOFException();
}
return (byte) val;
}
Peeks at (but does not consume) and returns the next byte value in
the stream, or throws EOFException if end of stream/block data has
been reached. |
public int read() throws IOException {
if (blkmode) {
if (pos == end) {
refill();
}
return (end >= 0) ? (buf[pos++] & 0xFF) : -1;
} else {
return in.read();
}
}
|
public int read(byte[] b,
int off,
int len) throws IOException {
return read(b, off, len, false);
}
|
int read(byte[] b,
int off,
int len,
boolean copy) throws IOException {
if (len == 0) {
return 0;
} else if (blkmode) {
if (pos == end) {
refill();
}
if (end < 0) {
return -1;
}
int nread = Math.min(len, end - pos);
System.arraycopy(buf, pos, b, off, nread);
pos += nread;
return nread;
} else if (copy) {
int nread = in.read(buf, 0, Math.min(len, MAX_BLOCK_SIZE));
if (nread > 0) {
System.arraycopy(buf, 0, b, off, nread);
}
return nread;
} else {
return in.read(b, off, len);
}
}
Attempts to read len bytes into byte array b at offset off. Returns
the number of bytes read, or -1 if the end of stream/block data has
been reached. If copy is true, reads values into an intermediate
buffer before copying them to b (to avoid exposing a reference to
b). |
public boolean readBoolean() throws IOException {
int v = read();
if (v < 0) {
throw new EOFException();
}
return (v != 0);
}
|
void readBooleans(boolean[] v,
int off,
int len) throws IOException {
int stop, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
int span = Math.min(endoff - off, MAX_BLOCK_SIZE);
in.readFully(buf, 0, span);
stop = off + span;
pos = 0;
} else if (end - pos < 1) {
v[off++] = din.readBoolean();
continue;
} else {
stop = Math.min(endoff, off + end - pos);
}
while (off < stop) {
v[off++] = Bits.getBoolean(buf, pos++);
}
}
}
|
public byte readByte() throws IOException {
int v = read();
if (v < 0) {
throw new EOFException();
}
return (byte) v;
}
|
public char readChar() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 2);
} else if (end - pos < 2) {
return din.readChar();
}
char v = Bits.getChar(buf, pos);
pos += 2;
return v;
}
|
void readChars(char[] v,
int off,
int len) throws IOException {
int stop, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
int span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 1);
in.readFully(buf, 0, span < < 1);
stop = off + span;
pos = 0;
} else if (end - pos < 2) {
v[off++] = din.readChar();
continue;
} else {
stop = Math.min(endoff, off + ((end - pos) > > 1));
}
while (off < stop) {
v[off++] = Bits.getChar(buf, pos);
pos += 2;
}
}
}
|
public double readDouble() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 8);
} else if (end - pos < 8) {
return din.readDouble();
}
double v = Bits.getDouble(buf, pos);
pos += 8;
return v;
}
|
void readDoubles(double[] v,
int off,
int len) throws IOException {
int span, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 3);
in.readFully(buf, 0, span < < 3);
pos = 0;
} else if (end - pos < 8) {
v[off++] = din.readDouble();
continue;
} else {
span = Math.min(endoff - off, ((end - pos) > > 3));
}
bytesToDoubles(buf, pos, v, off, span);
off += span;
pos += span < < 3;
}
}
|
public float readFloat() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 4);
} else if (end - pos < 4) {
return din.readFloat();
}
float v = Bits.getFloat(buf, pos);
pos += 4;
return v;
}
|
void readFloats(float[] v,
int off,
int len) throws IOException {
int span, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 2);
in.readFully(buf, 0, span < < 2);
pos = 0;
} else if (end - pos < 4) {
v[off++] = din.readFloat();
continue;
} else {
span = Math.min(endoff - off, ((end - pos) > > 2));
}
bytesToFloats(buf, pos, v, off, span);
off += span;
pos += span < < 2;
}
}
|
public void readFully(byte[] b) throws IOException {
readFully(b, 0, b.length, false);
}
|
public void readFully(byte[] b,
int off,
int len) throws IOException {
readFully(b, off, len, false);
}
|
public void readFully(byte[] b,
int off,
int len,
boolean copy) throws IOException {
while (len > 0) {
int n = read(b, off, len, copy);
if (n < 0) {
throw new EOFException();
}
off += n;
len -= n;
}
}
|
public int readInt() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 4);
} else if (end - pos < 4) {
return din.readInt();
}
int v = Bits.getInt(buf, pos);
pos += 4;
return v;
}
|
void readInts(int[] v,
int off,
int len) throws IOException {
int stop, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
int span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 2);
in.readFully(buf, 0, span < < 2);
stop = off + span;
pos = 0;
} else if (end - pos < 4) {
v[off++] = din.readInt();
continue;
} else {
stop = Math.min(endoff, off + ((end - pos) > > 2));
}
while (off < stop) {
v[off++] = Bits.getInt(buf, pos);
pos += 4;
}
}
}
|
public String readLine() throws IOException {
return din.readLine(); // deprecated, not worth optimizing
}
|
public long readLong() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 8);
} else if (end - pos < 8) {
return din.readLong();
}
long v = Bits.getLong(buf, pos);
pos += 8;
return v;
}
|
String readLongUTF() throws IOException {
return readUTFBody(readLong());
}
Reads in string written in "long" UTF format. "Long" UTF format is
identical to standard UTF, except that it uses an 8 byte header
(instead of the standard 2 bytes) to convey the UTF encoding length. |
void readLongs(long[] v,
int off,
int len) throws IOException {
int stop, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
int span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 3);
in.readFully(buf, 0, span < < 3);
stop = off + span;
pos = 0;
} else if (end - pos < 8) {
v[off++] = din.readLong();
continue;
} else {
stop = Math.min(endoff, off + ((end - pos) > > 3));
}
while (off < stop) {
v[off++] = Bits.getLong(buf, pos);
pos += 8;
}
}
}
|
public short readShort() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 2);
} else if (end - pos < 2) {
return din.readShort();
}
short v = Bits.getShort(buf, pos);
pos += 2;
return v;
}
|
void readShorts(short[] v,
int off,
int len) throws IOException {
int stop, endoff = off + len;
while (off < endoff) {
if (!blkmode) {
int span = Math.min(endoff - off, MAX_BLOCK_SIZE > > 1);
in.readFully(buf, 0, span < < 1);
stop = off + span;
pos = 0;
} else if (end - pos < 2) {
v[off++] = din.readShort();
continue;
} else {
stop = Math.min(endoff, off + ((end - pos) > > 1));
}
while (off < stop) {
v[off++] = Bits.getShort(buf, pos);
pos += 2;
}
}
}
|
public String readUTF() throws IOException {
return readUTFBody(readUnsignedShort());
}
|
public int readUnsignedByte() throws IOException {
int v = read();
if (v < 0) {
throw new EOFException();
}
return v;
}
|
public int readUnsignedShort() throws IOException {
if (!blkmode) {
pos = 0;
in.readFully(buf, 0, 2);
} else if (end - pos < 2) {
return din.readUnsignedShort();
}
int v = Bits.getShort(buf, pos) & 0xFFFF;
pos += 2;
return v;
}
|
boolean setBlockDataMode(boolean newmode) throws IOException {
if (blkmode == newmode) {
return blkmode;
}
if (newmode) {
pos = 0;
end = 0;
unread = 0;
} else if (pos < end) {
throw new IllegalStateException("unread block data");
}
blkmode = newmode;
return !blkmode;
}
Sets block data mode to the given mode (true == on, false == off)
and returns the previous mode value. If the new mode is the same as
the old mode, no action is taken. Throws IllegalStateException if
block data mode is being switched from on to off while unconsumed
block data is still present in the stream. |
public long skip(long len) throws IOException {
long remain = len;
while (remain > 0) {
if (blkmode) {
if (pos == end) {
refill();
}
if (end < 0) {
break;
}
int nread = (int) Math.min(remain, end - pos);
remain -= nread;
pos += nread;
} else {
int nread = (int) Math.min(remain, MAX_BLOCK_SIZE);
if ((nread = in.read(buf, 0, nread)) < 0) {
break;
}
remain -= nread;
}
}
return len - remain;
}
|
void skipBlockData() throws IOException {
if (!blkmode) {
throw new IllegalStateException("not in block data mode");
}
while (end >= 0) {
refill();
}
}
If in block data mode, skips to the end of the current group of data
blocks (but does not unset block data mode). If not in block data
mode, throws an IllegalStateException. |
public int skipBytes(int n) throws IOException {
return din.skipBytes(n);
}
|