Java File Read via ByteBuffer readInputStream(InputStream input)

Here you can find the source of readInputStream(InputStream input)

Description

read Input Stream

License

Open Source License

Declaration

public static ByteBuffer readInputStream(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.nio.ByteBuffer;

public class Main {
    public static final int BUFFER_SIZE = 16384;

    public static ByteBuffer readInputStream(InputStream input) throws IOException {
        byte[] buf = new byte[BUFFER_SIZE];
        int idx = 0;
        for (;;) {
            int rd = input.read(buf, idx, buf.length - idx);
            if (rd < 0)
                break;
            idx += rd;//from   w w  w .j  a  v  a 2  s  .co  m
            if (idx == buf.length) {
                byte[] nbuf = new byte[idx * 2];
                System.arraycopy(buf, 0, nbuf, 0, idx);
                buf = nbuf;
            }
        }
        return ByteBuffer.wrap(buf, 0, idx);
    }
}

Related

  1. readFull(Reader in)
  2. readFully(final Reader input, final char[] buffer, final int offset, final int length)
  3. readFully(InputStream in)
  4. readFullyAndClose(InputStream input)
  5. readHeaderArea(InputStream in)
  6. readInputStreamToString(InputStream inputStream)
  7. readInt(byte[] bytes, int index)
  8. readInt(byte[] bytes, int offset, java.nio.ByteOrder byteorder)
  9. readInt(byte[] in)