Search in sources :

Example 1 with ProxyOutputStream

use of org.apache.commons.io.output.ProxyOutputStream in project xwiki-platform by xwiki.

the class XWikiAttachmentContent method getContentOutputStream.

/**
 * Set the content of the attachment by writing to a provided OutputStream. Content is *not* appended, this method
 * clears the content and creates new content. If you want to append content, you can call
 * {@link #getContentInputStream()} and copy the content of that into the provided OutputStream. Before closing this
 * OutputStream the content will remain the old content prior to the change.
 *
 * @return an OutputStream into which the caller can set the content of the attachments.
 * @since 4.2M3
 */
public OutputStream getContentOutputStream() {
    final FileItem fi = getNewFileItem();
    final XWikiAttachmentContent xac = this;
    final OutputStream fios;
    try {
        fios = fi.getOutputStream();
    } catch (IOException e) {
        // so unless it is modified, this should not happen.
        throw new RuntimeException("Exception getting attachment OutputStream.", e);
    }
    return (new ProxyOutputStream(fios) {

        @Override
        public void close() throws IOException {
            super.close();
            xac.file = fi;
            xac.setContentDirty(true);
            if (xac.attachment != null) {
                xac.attachment.setLongSize(xac.getLongSize());
            }
        }
    });
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) OutputStream(java.io.OutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) IOException(java.io.IOException)

Example 2 with ProxyOutputStream

use of org.apache.commons.io.output.ProxyOutputStream in project gocd by gocd.

the class ConsoleLogSender method sendLogs.

private long sendLogs(final SocketEndpoint webSocket, final ConsoleConsumer console, final JobIdentifier jobIdentifier) throws IOException {
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(BUF_SIZE);
    final OutputStream proxyOutputStream = new AutoFlushingStream(buffer, webSocket, BUF_SIZE);
    long linesProcessed = console.stream(line -> {
        try {
            byte[] bytes = line.getBytes(charset);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(bytes.length);
            byteArrayOutputStream.write(bytes);
            byteArrayOutputStream.write('\n');
            proxyOutputStream.write(byteArrayOutputStream.toByteArray());
        } catch (IOException e) {
            LOGGER.error("Failed to send log line {} for {}", console.totalLinesConsumed(), jobIdentifier, e);
        }
    });
    flushBuffer(buffer, webSocket);
    return linesProcessed;
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with ProxyOutputStream

use of org.apache.commons.io.output.ProxyOutputStream in project gradle by gradle.

the class AnsiConsoleUtil method wrapOutputStream.

/**
 * @see <a href="https://github.com/fusesource/jansi/blob/eeda18cb05122abe48b284dca969e2c060a0c009/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java#L64-L119">Method copied over from AnsiConsole.wrapOutputStream</a>
 */
private static OutputStream wrapOutputStream(final OutputStream stream, int fileno) {
    // any of the ansi sequences.
    if (Boolean.getBoolean("jansi.passthrough")) {
        return stream;
    }
    // the ansi escapes.
    if (Boolean.getBoolean("jansi.strip")) {
        return new AnsiOutputStream(stream);
    }
    if (OperatingSystem.current().isWindows() && !IS_MINGW_XTERM) {
        final long stdOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        final int[] mode = new int[1];
        if (stdOutputHandle != INVALID_HANDLE_VALUE && 0 != GetConsoleMode(stdOutputHandle, mode) && 0 != SetConsoleMode(stdOutputHandle, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN)) {
            return new ProxyOutputStream(stream) {

                @Override
                public void close() throws IOException {
                    write(AnsiOutputStream.RESET_CODE);
                    flush();
                    // Reset console mode
                    SetConsoleMode(stdOutputHandle, mode[0]);
                    super.close();
                }
            };
        }
        // On windows we know the console does not interpret ANSI codes..
        try {
            return new WindowsAnsiPrintStream(new PrintStream(stream));
        } catch (Throwable ignore) {
        // this happens when JNA is not in the path.. or
        // this happens when the stdout is being redirected to a file.
        }
        // Use the ANSIOutputStream to strip out the ANSI escape sequences.
        return new AnsiOutputStream(stream);
    }
    // We must be on some Unix variant, including MSYS(2) on Windows...
    try {
        // If the jansi.force property is set, then we force to output
        // the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
        boolean forceColored = Boolean.getBoolean("jansi.force");
        // to strip the ANSI sequences..
        if (!IS_XTERM && !forceColored && isatty(fileno) == 0) {
            return new AnsiOutputStream(stream);
        }
    } catch (Throwable ignore) {
    // These errors happen if the JNI lib is not available for your platform.
    // But since we are on ANSI friendly platform, assume the user is on the console.
    }
    // attributes.
    return new ProxyOutputStream(stream) {

        @Override
        public void close() throws IOException {
            write(AnsiOutputStream.RESET_CODE);
            flush();
            super.close();
        }
    };
}
Also used : PrintStream(java.io.PrintStream) WindowsAnsiPrintStream(org.fusesource.jansi.WindowsAnsiPrintStream) AnsiOutputStream(org.fusesource.jansi.AnsiOutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) WindowsAnsiPrintStream(org.fusesource.jansi.WindowsAnsiPrintStream)

Aggregations

ProxyOutputStream (org.apache.commons.io.output.ProxyOutputStream)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1 AnsiOutputStream (org.fusesource.jansi.AnsiOutputStream)1 WindowsAnsiPrintStream (org.fusesource.jansi.WindowsAnsiPrintStream)1