Java ByteBuffer Put inputStream(ByteBuffer bytes)

Here you can find the source of inputStream(ByteBuffer bytes)

Description

input Stream

License

Apache License

Declaration

public static InputStream inputStream(ByteBuffer bytes) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class Main {
    public static InputStream inputStream(ByteBuffer bytes) {
        final ByteBuffer copy = bytes.duplicate();

        return new InputStream() {
            public int read() throws IOException {
                if (!copy.hasRemaining())
                    return -1;

                return copy.get() & 0xFF;
            }//from  w  w  w  . j ava 2 s  .c o m

            @Override
            public int read(byte[] bytes, int off, int len) throws IOException {
                if (!copy.hasRemaining())
                    return -1;

                len = Math.min(len, copy.remaining());
                copy.get(bytes, off, len);
                return len;
            }

            @Override
            public int available() throws IOException {
                return copy.remaining();
            }
        };
    }
}

Related

  1. convert(Object lock, CharsetEncoder encoder, ByteBuffer bytes, CharBuffer chars, OutputStream out)
  2. deserializeString(ByteBuffer inputBuffer, Charset charset)
  3. extractString(ByteArrayOutputStream byteBuffer)
  4. flushOutputStreamWriter(OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock)
  5. getZeroTerminatedStringBytesArray( ByteBuffer inputBuffer)
  6. inputStreamToByteBuffer(InputStream input)
  7. inputStreamToByteBuffer(InputStream is)
  8. makeInputStream(final ByteBuffer buffer)
  9. moveBufferToStream(OutputStream out, ByteBuffer in, int length)