Here you can find the source of readFully(InputStream in)
public static byte[] readFully(InputStream in) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; public class Main { public static final int BUFFER_SIZE = 8192; /**//from ww w.j a v a2 s. c om * Reads until the end of the input stream, and returns the contents as a byte[] */ public static byte[] readFully(InputStream in) throws IOException { List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(4); while (true) { ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); int count = in.read(buffer.array(), 0, BUFFER_SIZE); if (count > 0) { buffer.limit(count); buffers.add(buffer); } if (count < BUFFER_SIZE) break; } return getAsBytes(buffers); } /** * Copies the contents of the buffers into a single byte[] */ //TODO: not tested public static byte[] getAsBytes(List<ByteBuffer> buffers) { //find total size int size = 0; for (ByteBuffer buffer : buffers) { size += buffer.remaining(); } byte[] arr = new byte[size]; int offset = 0; for (ByteBuffer buffer : buffers) { int len = buffer.remaining(); buffer.get(arr, offset, len); offset += len; } return arr; } }