fix subtle length vs. limit bug in ByteArrayInputStream

This commit is contained in:
Joel Dice 2007-10-31 10:11:14 -06:00
parent df5a81a73c
commit 7a4cca45c2

View File

@ -3,16 +3,16 @@ package java.io;
public class ByteArrayInputStream extends InputStream { public class ByteArrayInputStream extends InputStream {
private final byte[] array; private final byte[] array;
private int position; private int position;
private final int length; private final int limit;
public ByteArrayInputStream(byte[] array, int offset, int length) { public ByteArrayInputStream(byte[] array, int offset, int length) {
this.array = array; this.array = array;
position = offset; position = offset;
this.length = length; this.limit = offset + length;
} }
public int read() { public int read() {
if (position < length) { if (position < limit) {
return array[position++] & 0xff; return array[position++] & 0xff;
} else { } else {
return -1; return -1;
@ -20,10 +20,13 @@ public class ByteArrayInputStream extends InputStream {
} }
public int read(byte[] buffer, int offset, int bufferLength) { public int read(byte[] buffer, int offset, int bufferLength) {
if (position >= length) { if (bufferLength == 0) {
return 0;
}
if (position >= limit) {
return -1; return -1;
} }
int remaining = length-position; int remaining = limit-position;
if (remaining < bufferLength) { if (remaining < bufferLength) {
bufferLength = remaining; bufferLength = remaining;
} }
@ -33,6 +36,6 @@ public class ByteArrayInputStream extends InputStream {
} }
public int available() { public int available() {
return length - position; return limit - position;
} }
} }