Java File Read via ByteBuffer readAsByteArray(final InputStream source)

Here you can find the source of readAsByteArray(final InputStream source)

Description

Reads everything from the input stream using NIO and returns an array of bytes.

License

Open Source License

Parameter

Parameter Description
source input stream

Exception

Parameter Description
IOException by Channel.read()

Return

resultant byte array

Declaration

public static byte[] readAsByteArray(final InputStream source) 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;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Main {
    private static final int READ_BLOCK = 8192;

    /**/*w w w  .  j a v a  2  s .c o  m*/
     * Reads everything from the input stream using NIO and returns an array of bytes.
     *
     * @param source
     *            input stream
     * @return resultant byte array
     * @throws IOException
     *             by {@code Channel.read()}
     */
    public static byte[] readAsByteArray(final InputStream source) throws IOException {
        final ByteBuffer bb = readAsByteBuffer(source);
        final byte[] result = new byte[bb.limit()];
        bb.get(result);
        bb.clear();
        return result;
    }

    /**
     * Reads everything from the input stream using NIO and returns a byte buffer.
     *
     * @param source
     *            input stream
     * @return resultant byte buffer
     * @throws IOException
     */
    public static ByteBuffer readAsByteBuffer(final InputStream source) throws IOException {
        // create channel for input stream
        try (final ReadableByteChannel bc = Channels.newChannel(source)) {
            ByteBuffer bb = ByteBuffer.allocate(READ_BLOCK);
            while (bc.read(bb) != -1) { // read the data while it lasts in chunks defined by READ_BLOCK
                bb = resizeBuffer(bb); //resize the buffer if required to fit the data on the next read
            }
            bb.flip();
            return bb;
        }
    }

    /**
     * A helper method to resize byte buffer upon read.
     *
     * @param in
     * @return
     */
    private static ByteBuffer resizeBuffer(final ByteBuffer in) {
        if (in.remaining() < READ_BLOCK) {
            // create new buffer with double capacity
            final ByteBuffer result = ByteBuffer.allocate(in.capacity() * 2);
            // flip the in buffer in preparation for it to be copied into the newly created larger buffer
            in.flip();
            // copy the in buffer into new buffer
            result.put(in);
            return result;
        }
        return in;
    }
}

Related

  1. read(Reader reader)
  2. read24BitInteger(byte[] threeBytes, ByteOrder order)
  3. read_file(File input)
  4. readAiffHeader(DataInputStream inStrm, FileChannel fc)
  5. readAndTransfer(final String dir)
  6. readBigEndianWord(byte[] buf)
  7. readBinaryFile(File file)
  8. readBinaryFileAsFloats(String fileName)
  9. readByte(String filePath)