### Question:
SocketIOWithTimeout { SocketIOWithTimeout(SelectableChannel channel, long timeout) throws IOException { checkChannelValidity(channel); this.channel = channel; this.timeout = timeout; channel.configureBlocking(false); } SocketIOWithTimeout(SelectableChannel channel, long timeout); void setTimeout(long timeoutMs);  }### Answer:
@Test public void testSocketIOWithTimeout() throws Exception { Pipe pipe = Pipe.open(); Pipe.SourceChannel source = pipe.source(); Pipe.SinkChannel sink = pipe.sink(); try { final InputStream in = new SocketInputStream(source, TIMEOUT); OutputStream out = new SocketOutputStream(sink, TIMEOUT); byte[] writeBytes = TEST_STRING.getBytes(); byte[] readBytes = new byte[writeBytes.length]; byte byteWithHighBit = (byte)0x80; out.write(writeBytes); out.write(byteWithHighBit); doIO(null, out, TIMEOUT); in.read(readBytes); assertTrue(Arrays.equals(writeBytes, readBytes)); assertEquals(byteWithHighBit & 0xff, in.read()); doIO(in, null, TIMEOUT); ((SocketInputStream)in).setTimeout(TIMEOUT * 2); doIO(in, null, TIMEOUT * 2); ((SocketInputStream)in).setTimeout(0); TestingThread thread = new TestingThread(ctx) { @Override public void doWork() throws Exception { try { in.read(); fail("Did not fail with interrupt"); } catch (InterruptedIOException ste) { LOG.info("Got expection while reading as expected : " + ste.getMessage()); } } }; ctx.addThread(thread); ctx.startThreads(); Thread.sleep(1000); thread.interrupt(); ctx.stop(); assertTrue(source.isOpen()); assertTrue(sink.isOpen()); try { out.write(1); fail("Did not throw"); } catch (IOException ioe) { GenericTestUtils.assertExceptionContains( "stream is closed", ioe); } out.close(); assertFalse(sink.isOpen()); assertEquals(-1, in.read()); in.close(); assertFalse(source.isOpen()); } finally { if (source != null) { source.close(); } if (sink != null) { sink.close(); } } }
@Test public void testSocketIOWithTimeout() throws Exception { Pipe pipe = Pipe.open(); Pipe.SourceChannel source = pipe.source(); Pipe.SinkChannel sink = pipe.sink(); try { final InputStream in = new SocketInputStream(source, TIMEOUT); OutputStream out = new SocketOutputStream(sink, TIMEOUT); byte[] writeBytes = TEST_STRING.getBytes(); byte[] readBytes = new byte[writeBytes.length]; byte byteWithHighBit = (byte)0x80; out.write(writeBytes); out.write(byteWithHighBit); doIO(null, out, TIMEOUT); in.read(readBytes); assertTrue(Arrays.equals(writeBytes, readBytes)); assertEquals(byteWithHighBit & 0xff, in.read()); doIO(in, null, TIMEOUT); ((SocketInputStream)in).setTimeout(TIMEOUT * 2); doIO(in, null, TIMEOUT * 2); ((SocketInputStream)in).setTimeout(0); TestingThread thread = new TestingThread(ctx) { @Override public void doWork() throws Exception { try { in.read(); fail("Did not fail with interrupt"); } catch (InterruptedIOException ste) { LOG.info("Got expection while reading as expected : " + ste.getMessage()); } } }; ctx.addThread(thread); ctx.startThreads(); Thread.sleep(1000); thread.interrupt(); ctx.stop(); assertTrue(source.isOpen()); assertTrue(sink.isOpen()); if (!Shell.WINDOWS && !Shell.PPC_64) { try { out.write(1); fail("Did not throw"); } catch (IOException ioe) { GenericTestUtils.assertExceptionContains( "stream is closed", ioe); } } out.close(); assertFalse(sink.isOpen()); assertEquals(-1, in.read()); in.close(); assertFalse(source.isOpen()); } finally { if (source != null) { source.close(); } if (sink != null) { sink.close(); } } }