public static OutputStream newOutputStream()

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


    public static OutputStream newOutputStream(final AsynchronousByteChannel channel) {
        return new OutputStream() {
            @Override
            public synchronized void write(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) {
                    ByteBuffer bb = ByteBuffer.wrap(b, off, len);
                    while (bb.hasRemaining()) {
                        try {
                            channel.write(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 void write(int b) throws IOException {
                write(new byte[] {(byte) b});
            }

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