ByteBuffer from Input Stream - Java java.nio

Java examples for java.nio:ByteBuffer Stream

Description

ByteBuffer from Input Stream

Demo Code


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{
    static public ByteBuffer fromInputStream(InputStream strm) {
        try {/* ww w. j  a v a  2  s  .  com*/
            byte[] bytes = new byte[strm.available()];
            strm.read(bytes);
            return ByteBuffer.wrap(bytes);
        } catch (IOException ex) {
            Logger.getLogger(ByteBufferUtils.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        return null;
    }
}

Related Tutorials