corda/classpath/java/io/ByteArrayInputStream.java

42 lines
911 B
Java
Raw Normal View History

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 limit;
2007-09-26 17:27:09 +00:00
public ByteArrayInputStream(byte[] array, int offset, int length) {
this.array = array;
position = offset;
this.limit = offset + length;
2007-09-26 17:27:09 +00:00
}
public int read() {
if (position < limit) {
2007-09-26 17:27:09 +00:00
return array[position++] & 0xff;
} else {
return -1;
}
}
public int read(byte[] buffer, int offset, int bufferLength) {
if (bufferLength == 0) {
return 0;
}
if (position >= limit) {
return -1;
}
int remaining = limit-position;
if (remaining < bufferLength) {
bufferLength = remaining;
}
2007-10-30 21:10:32 +00:00
System.arraycopy(array, position, buffer, offset, bufferLength);
position += bufferLength;
return bufferLength;
}
2007-09-26 17:27:09 +00:00
public int available() {
return limit - position;
2007-09-26 17:27:09 +00:00
}
}