Here you can find the source of getBytes(InputStream in, int length, int BUF_SIZE)
public static byte[] getBytes(InputStream in, int length, int BUF_SIZE) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2005, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:// w w w . j a va 2 s. c om * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.*; public class Main { public static byte[] getBytes(InputStream in, int length, int BUF_SIZE) throws IOException { byte[] classbytes; int bytesread = 0; int readcount; try { if (length > 0) { classbytes = new byte[length]; for (; bytesread < length; bytesread += readcount) { readcount = in.read(classbytes, bytesread, length - bytesread); if (readcount <= 0) /* if we didn't read anything */ break; /* leave the loop */ } } else /* does not know its own length! */ { length = BUF_SIZE; classbytes = new byte[length]; readloop: while (true) { for (; bytesread < length; bytesread += readcount) { readcount = in.read(classbytes, bytesread, length - bytesread); if (readcount <= 0) /* if we didn't read anything */ break readloop; /* leave the loop */ } byte[] oldbytes = classbytes; length += BUF_SIZE; classbytes = new byte[length]; System.arraycopy(oldbytes, 0, classbytes, 0, bytesread); } } if (classbytes.length > bytesread) { byte[] oldbytes = classbytes; classbytes = new byte[bytesread]; System.arraycopy(oldbytes, 0, classbytes, 0, bytesread); } } finally { try { in.close(); } catch (IOException ee) { // nothing to do here } } return classbytes; } }