Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.net.SocketTimeoutException; public class Main { public static void readBytes(InputStream in, long count) throws IOException { for (long i = 0; i < count; i++) { try { if (in.read() == -1) { throw new AssertionError("Unexpected end of stream after " + i + " bytes"); } } catch (SocketTimeoutException e) { throw new AssertionError("Timeout while reading " + count + " bytes (read " + i + " bytes)"); } } } }