2007-09-26 17:27:09 +00:00
|
|
|
package java.io;
|
|
|
|
|
|
|
|
public class ByteArrayInputStream extends InputStream {
|
|
|
|
private final byte[] array;
|
|
|
|
private int position;
|
|
|
|
private final int length;
|
|
|
|
|
|
|
|
public ByteArrayInputStream(byte[] array, int offset, int length) {
|
|
|
|
this.array = array;
|
|
|
|
position = offset;
|
|
|
|
this.length = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read() {
|
|
|
|
if (position < length) {
|
|
|
|
return array[position++] & 0xff;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-30 21:08:49 +00:00
|
|
|
public int read(byte[] buffer, int offset, int bufferLength) {
|
|
|
|
if (position < length) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-30 21:37:46 +00:00
|
|
|
int remaining = length-position;
|
|
|
|
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-09-26 17:45:44 +00:00
|
|
|
return length - position;
|
2007-09-26 17:27:09 +00:00
|
|
|
}
|
|
|
|
}
|