ByteBufferInputStream.java
package sprout.server.websocket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
public class ByteBufferInputStream extends InputStream {
private final ByteBuffer buffer;
public ByteBufferInputStream(ByteBuffer buffer) {
this.buffer = buffer;
}
@Override
public int read() throws IOException {
if (!buffer.hasRemaining()) {
return -1;
}
return buffer.get() & 0xFF;
}
@Override
public int read(byte[] bytes, int off, int len) throws IOException {
if (!buffer.hasRemaining()) {
return -1;
}
int readLen = Math.min(len, buffer.remaining());
buffer.get(bytes, off, readLen);
return readLen;
}
@Override
public int available() throws IOException {
return buffer.remaining();
}
}