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;
    }
  }

  public int read(byte[] buffer, int offset, int bufferLength) {
    if (position >= length) {
      return -1;
    }
    int remaining = length-position;
    if (remaining < bufferLength) {
      bufferLength = remaining;
    }
    System.arraycopy(array, position, buffer, offset, bufferLength);
    position += bufferLength;
    return bufferLength;
  }

  public int available() {
    return length - position;
  }
}

@@ -142,21 +142,20 @@
 al int l
-ength
+imit
 ;%0A%0A  pub
@@ -276,23 +276,31 @@
   this.l
-ength =
+imit = offset +
  length;
@@ -347,21 +347,20 @@
 tion %3C l
-ength
+imit
 ) %7B%0A    
@@ -496,24 +496,75 @@
 erLength) %7B%0A
+    if (bufferLength == 0) %7B%0A      return 0;%0A    %7D%0A
     if (posi
@@ -572,21 +572,20 @@
 ion %3E= l
-ength
+imit
 ) %7B%0A    
@@ -624,21 +624,20 @@
 ning = l
-ength
+imit
 -positio
@@ -881,21 +881,20 @@
 return l
-ength
+imit
  - posit

fix subtle length vs. limit bug in ByteArrayInputStream