Here you can find the source of readFully(InputStream in, ByteBuffer out)
public static void readFully(InputStream in, ByteBuffer out) throws IOException
//package com.java2s; /* JKTX//w w w. j a va 2s.c o m * * Copyright (c) 2011 Timon Bijlsma * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; public class Main { public static void readFully(InputStream in, ByteBuffer out) throws IOException { out.mark(); if (out.hasArray()) { while (out.hasRemaining()) { int r = in .read(out.array(), out.arrayOffset() + out.position(), out.remaining()); if (r < 0) throw new EOFException(); out.position(out.position() + r); } } else { byte[] temp = new byte[Math.min(out.remaining(), 8192)]; while (out.hasRemaining()) { int r = in.read(temp, 0, Math.min(temp.length, out.remaining())); if (r < 0) throw new EOFException("" + out.position()); out.put(temp, 0, r); } } out.reset(); } }