TimeoutInputStream extends FilterInputStream { @Override public int read() { waitFor(1); return super.read(); } TimeoutInputStream(InputStream in, long timeout);  TimeoutInputStream(InputStream in); long getSleep(); void setSleep(long sleep); long getTimeout(); void setTimeout(long timeout); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len); @Override long skip(long n); @Override int available(); boolean readyFor(int bytes); void waitFor(int bytes);  }
@Test public void testOk() throws Exception { ByteArrayInputStream bin = new ByteArrayInputStream( "this is my test".getBytes()); TimeoutInputStream in = new TimeoutInputStream(bin, 500l); DataInputStream data = new DataInputStream(in); byte[] buffer = new byte["this is my test".length()]; data.readFully(buffer); assertEquals("this is my test", new String(buffer)); try { data.read(); fail("IllegalStateException expected"); } catch (IllegalStateException e) { assertEquals("timeout", e.getMessage()); } data.close(); }