Java examples for java.nio:ByteBuffer Stream
Returns an InputStream wrapping a ByteBuffer.
import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Iterator; import org.apache.log4j.Logger; public class Main{ /**//w w w .j a v a2 s . com * Returns an InputStream wrapping a ByteBuffer. * * @param buf * @return */ final static public InputStream asInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xFF; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } public void mark(int readlimit) { buf.mark(); } public void reset() { buf.reset(); } public boolean markSupported() { return true; } }; } }