package org.dynmap.utils;

import java.io.IOException;
import java.io.InputStream;

public class BufferInputStream extends InputStream {
    private byte[] buf;
    private int len;
    private int off = 0;
    private int mark = 0;
    
    public BufferInputStream(byte[] b) {
        this.len = b.length;
        this.buf = b;
    }
    
    public BufferInputStream(byte[] b, int len) {
        this.len = len;
        this.buf = b;
    }

    public byte[] buffer() { return buf; }
    public int length() { return len; }
    
    @Override
    public int available() {
        return (len - off);
    }
    
    @Override
    public void mark(int readAheadLimit) {
        mark = off;
    }
    
    @Override
    public boolean markSupported() {
        return true;
    }
   
    @Override
    public void reset() {
        this.off = this.mark;
    }
    
    @Override
    public void close() {
    }
    
    @Override
    public int read() {
        if (off < len) {
            off++;
            return 0xFF & buf[off-1];
        }
        else {
            return -1;
        }
    }
    
    @Override
    public int read(byte[] b, int off, int len) {
        if (b == null) {
            throw new NullPointerException();
        }
        else if ((off < 0) || (len < 0) || ((len + this.off) > this.len)) {
            throw new IndexOutOfBoundsException();
        }
        if (this.off >= this.len) {
            return -1;
        }
        if ((this.off + len) > this.len) {
            len = this.len - this.off;  // Remainder of buffer
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(buf, this.off, b, off, len);
        this.off += len;
        return len;
    }
    
    @Override
    public long skip(long n) {
        if ((this.off + n) > this.len) {
            n = this.len - this.off;
        }
        if (n < 0) {
            return 0;
        }
        this.off += n;
        return n;
    }
}

@@ -1151,35 +1151,54 @@
 nt off, int len)
+ throws IOException
  %7B%0A
-
         if (b ==
@@ -1232,19 +1232,10 @@
 new 
-NullPointer
+IO
 Exce
@@ -1232,32 +1232,41 @@
 new IOException(
+%22No data%22
 );%0A        %7D%0A   
@@ -1365,33 +1365,42 @@
 ew I
-ndexOutOfBoundsException(
+OException (%22Offset out of bounds%22
 );%0A 

Make BufferInputStream throw IOExceptions for out-of-bounds accesses