2007-09-26 17:27:09 +00:00
|
|
|
package java.io;
|
|
|
|
|
|
|
|
public class ByteArrayInputStream extends InputStream {
|
|
|
|
private final byte[] array;
|
|
|
|
private int position;
|
2007-10-31 16:11:14 +00:00
|
|
|
private final int limit;
|
2007-09-26 17:27:09 +00:00
|
|
|
|
|
|
|
public ByteArrayInputStream(byte[] array, int offset, int length) {
|
|
|
|
this.array = array;
|
|
|
|
position = offset;
|
2007-10-31 16:11:14 +00:00
|
|
|
this.limit = offset + length;
|
2007-09-26 17:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int read() {
|
2007-10-31 16:11:14 +00:00
|
|
|
if (position < limit) {
|
2007-09-26 17:27:09 +00:00
|
|
|
return array[position++] & 0xff;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-30 21:08:49 +00:00
|
|
|
public int read(byte[] buffer, int offset, int bufferLength) {
|
2007-10-31 16:11:14 +00:00
|
|
|
if (bufferLength == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (position >= limit) {
|
2007-10-30 21:08:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-10-31 16:11:14 +00:00
|
|
|
int remaining = limit-position;
|
2007-10-30 21:37:46 +00:00
|
|
|
if (remaining < bufferLength) {
|
|
|
|
bufferLength = remaining;
|
2007-10-30 21:08:49 +00:00
|
|
|
}
|
2007-10-30 21:10:32 +00:00
|
|
|
System.arraycopy(array, position, buffer, offset, bufferLength);
|
2007-10-30 21:08:49 +00:00
|
|
|
position += bufferLength;
|
|
|
|
return bufferLength;
|
|
|
|
}
|
|
|
|
|
2007-09-26 17:27:09 +00:00
|
|
|
public int available() {
|
2007-10-31 16:11:14 +00:00
|
|
|
return limit - position;
|
2007-09-26 17:27:09 +00:00
|
|
|
}
|
|
|
|
}
|