mirror of
https://github.com/corda/corda.git
synced 2025-01-04 04:04:27 +00:00
fix subtle length vs. limit bug in ByteArrayInputStream
This commit is contained in:
parent
df5a81a73c
commit
7a4cca45c2
@ -3,16 +3,16 @@ package java.io;
|
||||
public class ByteArrayInputStream extends InputStream {
|
||||
private final byte[] array;
|
||||
private int position;
|
||||
private final int length;
|
||||
private final int limit;
|
||||
|
||||
public ByteArrayInputStream(byte[] array, int offset, int length) {
|
||||
this.array = array;
|
||||
position = offset;
|
||||
this.length = length;
|
||||
this.limit = offset + length;
|
||||
}
|
||||
|
||||
public int read() {
|
||||
if (position < length) {
|
||||
if (position < limit) {
|
||||
return array[position++] & 0xff;
|
||||
} else {
|
||||
return -1;
|
||||
@ -20,10 +20,13 @@ public class ByteArrayInputStream extends InputStream {
|
||||
}
|
||||
|
||||
public int read(byte[] buffer, int offset, int bufferLength) {
|
||||
if (position >= length) {
|
||||
if (bufferLength == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (position >= limit) {
|
||||
return -1;
|
||||
}
|
||||
int remaining = length-position;
|
||||
int remaining = limit-position;
|
||||
if (remaining < bufferLength) {
|
||||
bufferLength = remaining;
|
||||
}
|
||||
@ -33,6 +36,6 @@ public class ByteArrayInputStream extends InputStream {
|
||||
}
|
||||
|
||||
public int available() {
|
||||
return length - position;
|
||||
return limit - position;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user