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
|
|
|
|
|
|
|
|
|
|
|
public ByteBufferInputStream(ByteBuffer buffer) {
|
2008-12-27 06:58:39 -05:00
|
|
|
this.buffer = buffer;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized int read() throws IOException {
|
2008-04-26 12:26:16 -04:00
|
|
|
if (buffer.remaining() <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return buffer.get();
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized int read(byte[] b, int off, int len) throws IOException {
|
2008-04-26 12:26:16 -04:00
|
|
|
if (buffer.remaining() <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-02-13 15:08:06 -05: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 15:13:58 -04: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 15:08:06 -05:00
|
|
|
}
|