2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
public class ByteBufferInputStream extends InputStream {
|
|
|
|
|
2008-03-27 00:28:06 +00:00
|
|
|
private final ByteBuffer buffer;
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
public ByteBufferInputStream(ByteBuffer buffer) {
|
2008-11-22 15:30:33 +00:00
|
|
|
this.buffer = buffer.duplicate();
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized int read() throws IOException {
|
2008-04-26 16:26:16 +00:00
|
|
|
if (buffer.remaining() <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return buffer.get();
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized int read(byte[] b, int off, int len) throws IOException {
|
2008-04-26 16:26:16 +00:00
|
|
|
if (buffer.remaining() <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
int length = Math.min(len, buffer.remaining());
|
|
|
|
|
|
|
|
buffer.get(b, off, length);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized int available() throws IOException {
|
|
|
|
return buffer.remaining();
|
|
|
|
}
|
|
|
|
|
2008-10-06 19:13:58 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean markSupported() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void mark(int readlimit) {
|
|
|
|
buffer.mark();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void reset() throws IOException {
|
|
|
|
buffer.reset();
|
|
|
|
}
|
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|