public static InputStream newInputStream()

in surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/Channels.java [116:160]


    public static InputStream newInputStream(final AsynchronousByteChannel channel) {
        return new InputStream() {
            @Override
            public synchronized int read(byte[] b, int off, int len) throws IOException {
                if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) {
                    throw new IndexOutOfBoundsException("b.length = " + b.length + ", off = " + off + ", len = " + len);
                } else if (len == 0) {
                    return 0;
                }
                ByteBuffer bb = ByteBuffer.wrap(b, off, len);
                try {
                    return channel.read(bb).get();
                } catch (ExecutionException e) {
                    Throwable t = e.getCause();
                    throw t instanceof IOException
                            ? (IOException) t
                            : new IOException((t == null ? e : t).getLocalizedMessage(), t);
                } catch (Exception e) {
                    throw new IOException(e.getLocalizedMessage(), e);
                }
            }

            @Override
            public int read() throws IOException {
                int count;
                byte[] b = new byte[1];
                do {
                    count = read(b, 0, 1);
                } while (count == 0);

                return count == -1 ? -1 : b[0];
            }

            @Override
            public synchronized void close() throws IOException {
                if (channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (ClosedChannelException e) {
                        // closed channel anyway
                    }
                }
            }
        };
    }