Java ByteBuffer to InputStream newInputStream(final ByteBuffer buf)

Here you can find the source of newInputStream(final ByteBuffer buf)

Description

new Input Stream

License

Open Source License

Declaration

public static InputStream newInputStream(final ByteBuffer buf) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2013 Vladimir Rodionov. All Rights Reserved
*
* This code is released under the GNU Affero General Public License.
*
* See: http://www.fsf.org/licensing/licenses/agpl-3.0.html
*
* VLADIMIR RODIONOV MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
* OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. Vladimir Rodionov SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
* BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
* ITS DERIVATIVES.//w  w w .  j  av a2 s  . com
*
* Author: Vladimir Rodionov
*
*******************************************************************************/

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    public static InputStream newInputStream(final ByteBuffer buf) {
        return new InputStream() {
            private int mark = -1;

            @Override
            public boolean markSupported() {
                //System.out.println("markSupported ");

                return true;
            }

            @Override
            public void mark(int readLimit) {
                //System.out.println("mark "+readLimit);
                mark = buf.position();
            }

            @Override
            public void reset() {
                if (mark >= 0) {
                    //System.out.println("reset to "+ mark);

                    buf.position(mark);
                    mark = -1;
                }
            }

            public synchronized int read() throws IOException {
                //System.out.println("read ");

                if (!buf.hasRemaining()) {
                    return -1;
                }
                return buf.get();
            }

            public synchronized int read(byte[] bytes, int off, int len) throws IOException {
                // Read only what's left
                //System.out.println("read "+ len+" off="+off+" array.length="+bytes.length+" rem="+buf.remaining());
                if (buf.remaining() == 0)
                    return -1;
                len = Math.min(len, buf.remaining());
                buf.get(bytes, off, len);

                return len;
            }

            @Override
            public int available() {
                //System.out.println("available "+buf.remaining());

                return buf.remaining();
            }

            @Override
            public long skip(long v) {
                //System.out.println("skip "+v);

                if (v > 0) {
                    v = Math.min(v, buf.remaining());
                } else {
                    //v = Math.max( - buf.position(), v); 
                    return 0;
                }

                int off = buf.position();
                buf.position(off + (int) v);
                return v;
            }
        };
    }
}

Related

  1. newInputStream(final ByteBuffer buf)