NonBlocking { public static NonBlockingInputStream nonBlocking(String name, InputStream inputStream) { if (inputStream instanceof NonBlockingInputStream) { return (NonBlockingInputStream) inputStream; } return new NonBlockingInputStreamImpl(name, inputStream); }  static NonBlockingPumpReader nonBlockingPumpReader(); static NonBlockingPumpReader nonBlockingPumpReader(int size); static NonBlockingPumpInputStream nonBlockingPumpInputStream(); static NonBlockingPumpInputStream nonBlockingPumpInputStream(int size); static NonBlockingInputStream nonBlockingStream(NonBlockingReader reader, Charset encoding); static NonBlockingInputStream nonBlocking(String name, InputStream inputStream); static NonBlockingReader nonBlocking(String name, Reader reader); static NonBlockingReader nonBlocking(String name, InputStream inputStream, Charset encoding);  }
@Test public void testNonBlockingReaderOnNonBlockingStream() throws IOException { NonBlockingInputStream nbis = new NonBlockingInputStream() { int call = 0; int idx = 0; byte[] input = "中英字典".getBytes(StandardCharsets.UTF_8); @Override public int read(long timeout, boolean isPeek) throws IOException { if (call++ % 3 == 0) { try { Thread.sleep(timeout); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } return -2; } else if (idx < input.length) { return input[idx++] & 0x00FF; } else { return -1; } } }; NonBlockingReader nbr = NonBlocking.nonBlocking("name", nbis, StandardCharsets.UTF_8); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals('中', nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals('英', nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals('字', nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals('典', nbr.read(100)); assertEquals(NonBlockingReader.READ_EXPIRED, nbr.read(100)); assertEquals(-1, nbr.read(100)); }