From 7a4cca45c28669a7950451eb5722e8944264925f Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Wed, 31 Oct 2007 10:11:14 -0600 Subject: [PATCH] fix subtle length vs. limit bug in ByteArrayInputStream --- classpath/java/io/ByteArrayInputStream.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/classpath/java/io/ByteArrayInputStream.java b/classpath/java/io/ByteArrayInputStream.java index 56be302f76..e077516b37 100644 --- a/classpath/java/io/ByteArrayInputStream.java +++ b/classpath/java/io/ByteArrayInputStream.java @@ -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; } }