Java tutorial
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; public class Main { private static byte[] readSmallerStream(final InputStream in, final int streamlength) throws IOException { byte[] bytes = new byte[streamlength]; int readed = in.read(bytes); int patchsize = 1; while (patchsize > 0 && readed != streamlength) { patchsize = in.read(bytes, readed, streamlength - readed); if (patchsize > 0) { readed += patchsize; } } if (readed != streamlength) { throw new IOException("InputStream(" + in + ", streamlength=" + streamlength + ", readedlength=" + readed + ") read fail"); } return bytes; } }