Here you can find the source of readBytes(DataInputStream input, int size)
public static byte[] readBytes(DataInputStream input, int size) throws IOException
//package com.java2s; /*//from w w w . j a v a 2s . c o m * Copyright 2006-2011 The Virtual Laboratory for e-Science (VL-e) * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * For details, see the LICENCE.txt file location in the root directory of this * distribution or obtain the Apache Licence at the following location: * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * See: http://www.vl-e.nl/ * See: LICENCE.txt (located in the root folder of this distribution). * --- * $Id: IOUtil.java,v 1.2 2011-04-18 12:30:40 ptdeboer Exp $ * $Date: 2011-04-18 12:30:40 $ */ import java.io.DataInputStream; import java.io.IOException; public class Main { /** Reads exact amount of bytes */ public static byte[] readBytes(DataInputStream input, int size) throws IOException { if (input == null) return null; byte bytes[] = new byte[size]; int numRead = 0; // read loop: while (numRead < size) { int ret = input.read(bytes, numRead, size - numRead); if (ret < 0) throw new IOException("EOF"); numRead += ret; } return bytes; } }