2008-02-13 15:08:06 -05:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
public class ByteBufferInputStream extends InputStream {
|
|
|
|
|
2008-03-26 20:28:06 -04:00
|
|
|
private final ByteBuffer buffer;
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
public ByteBufferInputStream(ByteBuffer buffer) {
|
2008-12-27 06:58:39 -05:00
|
|
|
this.buffer = buffer;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
@Override
|
2009-07-16 08:06:51 -04:00
|
|
|
public int read() throws IOException {
|
2012-01-01 22:48:24 -05:00
|
|
|
return (buffer.position() < buffer.limit()) ? (buffer.get() & 0xff) : -1;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
@Override
|
2009-07-16 08:06:51 -04:00
|
|
|
public int read(byte[] b, int off, int len) throws IOException {
|
2012-01-01 22:48:24 -05:00
|
|
|
if (b == null) {
|
|
|
|
throw new NullPointerException();
|
|
|
|
} else if (off < 0 || len < 0 || len > b.length - off) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer.position() >= buffer.limit()) {
|
2008-04-26 12:26:16 -04:00
|
|
|
return -1;
|
2012-01-01 22:48:24 -05:00
|
|
|
}
|
2008-04-26 12:26:16 -04:00
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
if (len > buffer.remaining()) {
|
|
|
|
len = buffer.remaining();
|
|
|
|
}
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
if (len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
buffer.get(b, off, len);
|
|
|
|
return len;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
@Override
|
2009-07-16 08:06:51 -04:00
|
|
|
public int available() throws IOException {
|
2008-02-13 15:08:06 -05:00
|
|
|
return buffer.remaining();
|
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-10-06 15:13:58 -04:00
|
|
|
@Override
|
|
|
|
public boolean markSupported() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-10-06 15:13:58 -04:00
|
|
|
@Override
|
2009-07-16 08:06:51 -04:00
|
|
|
public void mark(int readlimit) {
|
2008-10-06 15:13:58 -04:00
|
|
|
buffer.mark();
|
|
|
|
}
|
|
|
|
|
2012-01-01 22:48:24 -05:00
|
|
|
|
2008-10-06 15:13:58 -04:00
|
|
|
@Override
|
2009-07-16 08:06:51 -04:00
|
|
|
public void reset() throws IOException {
|
2008-10-06 15:13:58 -04:00
|
|
|
buffer.reset();
|
|
|
|
}
|
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|