Example usage for java.io PipedOutputStream flush

List of usage examples for java.io PipedOutputStream flush

Introduction

In this page you can find the example usage for java.io PipedOutputStream flush.

Prototype

public synchronized void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:com.kdmanalytics.toif.assimilator.Assimilator.java

private void processKdmXmlFile(final List<File> kdmFiles)
        throws FileNotFoundException, IOException, RepositoryException, ToifException {
    if (debug) {//from  w w  w. ja  va  2s .c o m
        LOG.debug("processing kdm file...");
        //System.err.println("processing kdm file...");
    }

    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    final ThreadStatus status = new ThreadStatus();

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            KdmXmlHandler handler = null;
            try {
                if (kdmFiles.size() > 1) {
                    final String msg = "There should only be one .kdm file.";
                    LOG.error(msg);
                    throw new ToifException(msg);
                } else if (kdmFiles.size() == 1) {
                    File kdmFile = kdmFiles.get(0); // get the head of
                                                    // thelist.
                    handler = load(kdmFile, out);
                }
                out.flush();
                out.close();

                if (handler == null) {
                    return;
                }
                setNextId(handler.getNextId());
                setSmallestBigNumber(handler.getSmallestBigNumber());
                // increase
            } catch (IOException e) {
                final String msg = "IO exception whilst processing kdm file. "
                        + ". Possibly an existing kdm file is in your input path!";

                LOG.error(msg, e);
                status.exception = new ToifException(msg, e);
            } catch (RepositoryException e) {
                final String msg = "Repository Exception whilst processing kdm file. "
                        + ". Possibly an existing kdm file is in your input path!";

                LOG.error(msg, e);
                status.exception = new ToifException(msg, e);
            } catch (ToifException e) {
                // RJF final String msg =
                // "Processing Exception whilst processing kdm file. "
                // + ". Possibly that input file is invalid XML!";

                // LOG.error(msg, e);
                status.exception = e;
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException e) {
                        // Just leave it alone
                        LOG.error("unable to close stream");
                    }
            }
        }
    });

    // ---------------------------------------------------------
    // Unable to change logic within the short time frame given so
    // adding a means to catch unknown exceptions in thread
    // ----------------------------------------------------------
    Thread.UncaughtExceptionHandler tueh = new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread th, Throwable ex) {
            LOG.error("Uncaught exception: " + ex);
            status.exception = (Exception) ex;
        }
    };

    t.setUncaughtExceptionHandler(tueh);
    t.start();

    streamStatementsToRepo(in);
    try {
        t.join();

        // Check if we enoutered exception during processing and
        // proxy throw if we have one
        if (status.exception != null) {
            // Leave alone if already a ToifException
            if (status.exception instanceof ToifException)
                throw (ToifException) status.exception;
            else
                throw new ToifException(status.exception);

        }
    } catch (InterruptedException e) {
        LOG.error("Interrupted");
        throw new ToifException("Interrupted");
    }
}

From source file:com.dtolabs.rundeck.core.execution.impl.jsch.JschNodeExecutor.java

public NodeExecutorResult executeCommand(final ExecutionContext context, final String[] command,
        final INodeEntry node) {
    if (null == node.getHostname() || null == node.extractHostname()) {
        return NodeExecutorResultImpl.createFailure(StepFailureReason.ConfigurationFailure,
                "Hostname must be set to connect to remote node '" + node.getNodename() + "'", node);
    }//from  www  .  java 2  s. c o  m

    final ExecutionListener listener = context.getExecutionListener();
    final Project project = new Project();
    AntSupport.addAntBuildListener(listener, project);

    boolean success = false;
    final ExtSSHExec sshexec;
    //perform jsch sssh command
    final NodeSSHConnectionInfo nodeAuthentication = new NodeSSHConnectionInfo(node, framework, context);
    final int timeout = nodeAuthentication.getSSHTimeout();
    try {

        sshexec = SSHTaskBuilder.build(node, command, project, context.getDataContext(), nodeAuthentication,
                context.getLoglevel(), listener);
    } catch (SSHTaskBuilder.BuilderException e) {
        return NodeExecutorResultImpl.createFailure(StepFailureReason.ConfigurationFailure, e.getMessage(),
                node);
    }

    //Sudo support

    final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
        public Thread newThread(Runnable r) {
            return new Thread(null, r,
                    "SudoResponder " + node.getNodename() + ": " + System.currentTimeMillis());
        }
    });

    final Future<ResponderTask.ResponderResult> responderFuture;
    final SudoResponder sudoResponder = SudoResponder.create(node, framework, context);
    Runnable responderCleanup = null;
    if (sudoResponder.isSudoEnabled() && sudoResponder.matchesCommandPattern(command[0])) {
        final DisconnectResultHandler resultHandler = new DisconnectResultHandler();

        //configure two piped i/o stream pairs, to connect to the input/output of the SSH connection
        final PipedInputStream responderInput = new PipedInputStream();
        final PipedOutputStream responderOutput = new PipedOutputStream();
        final PipedInputStream jschInput = new PipedInputStream();
        //lead pipe allows connected inputstream to close and not hang the writer to this stream
        final PipedOutputStream jschOutput = new LeadPipeOutputStream();
        try {
            responderInput.connect(jschOutput);
            jschInput.connect(responderOutput);
        } catch (IOException e) {
            return NodeExecutorResultImpl.createFailure(StepFailureReason.IOFailure, e.getMessage(), node);
        }

        //first sudo prompt responder
        ResponderTask responder = new ResponderTask(sudoResponder, responderInput, responderOutput,
                resultHandler);

        /**
         * Callable will be executed by the ExecutorService
         */
        final Callable<ResponderTask.ResponderResult> responderResultCallable;

        //if 2nd responder
        final SudoResponder sudoResponder2 = SudoResponder.create(node, framework, context, SUDO2_OPT_PREFIX,
                DEFAULT_SUDO2_PASSWORD_OPTION, DEFAULT_SUDO2_COMMAND_PATTERN);
        if (sudoResponder2.isSudoEnabled()
                && sudoResponder2.matchesCommandPattern(CLIUtils.generateArgline(null, command, false))) {
            logger.debug("Enable second sudo responder");

            sudoResponder2.setDescription("Second " + SudoResponder.DEFAULT_DESCRIPTION);
            sudoResponder.setDescription("First " + SudoResponder.DEFAULT_DESCRIPTION);

            //sequence of the first then the second sudo responder
            responderResultCallable = responder.createSequence(sudoResponder2);
        } else {
            responderResultCallable = responder;
        }

        //set up SSH execution
        sshexec.setAllocatePty(true);
        sshexec.setInputStream(jschInput);
        sshexec.setSecondaryStream(jschOutput);
        sshexec.setDisconnectHolder(resultHandler);

        responderFuture = executor.submit(responderResultCallable);
        //close streams after responder is finished
        responderCleanup = new Runnable() {
            public void run() {
                logger.debug("SudoResponder shutting down...");
                try {
                    responderInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    responderOutput.flush();
                    responderOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //executor pool shutdown
                executor.shutdownNow();
            }
        };
        executor.submit(responderCleanup);
    } else {
        responderFuture = null;
    }
    if (null != context.getExecutionListener()) {
        context.getExecutionListener().log(3, "Starting SSH Connection: " + nodeAuthentication.getUsername()
                + "@" + node.getHostname() + " (" + node.getNodename() + ")");
    }
    String errormsg = null;
    FailureReason failureReason = null;
    try {
        sshexec.execute();
        success = true;
    } catch (BuildException e) {
        final ExtractFailure extractJschFailure = extractFailure(e, node, timeout, framework);
        errormsg = extractJschFailure.getErrormsg();
        failureReason = extractJschFailure.getReason();
        context.getExecutionListener().log(0, errormsg);
    }
    if (null != responderCleanup) {
        responderCleanup.run();
    }
    shutdownAndAwaitTermination(executor);
    if (null != responderFuture) {
        try {
            logger.debug("Waiting 5 seconds for responder future result");
            final ResponderTask.ResponderResult result = responderFuture.get(5, TimeUnit.SECONDS);
            logger.debug("Responder result: " + result);
            if (!result.isSuccess() && !result.isInterrupted()) {
                context.getExecutionListener().log(0,
                        result.getResponder().toString() + " failed: " + result.getFailureReason());
            }
        } catch (InterruptedException e) {
            //ignore
        } catch (java.util.concurrent.ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            //ignore
        }
    }
    final int resultCode = sshexec.getExitStatus();

    if (success) {
        return NodeExecutorResultImpl.createSuccess(node);
    } else {
        return NodeExecutorResultImpl.createFailure(failureReason, errormsg, node, resultCode);
    }
}

From source file:org.lockss.util.TestStreamUtil.java

public void testReadBufMultipleRead() throws Exception {
    byte[] snd1 = { '0', '1', 0, '3' };
    byte[] snd2 = { '4', '5', '6', '7', '8', '9', 'a', 'b' };
    byte[] exp = { '0', '1', 0, '3', '4', '5', '6', '7', '8', '9', 'a', 'b' };
    final int len = exp.length;
    final byte[] buf = new byte[len];
    PipedOutputStream outs = new PipedOutputStream();
    final InputStream ins = new PipedInputStream(outs);
    final Exception[] ex = { null };
    final int[] res = { 0 };
    Thread th = new Thread() {
        public void run() {
            try {
                res[0] = StreamUtil.readBytes(ins, buf, len);
            } catch (IOException e) {
                ex[0] = e;//from  w w  w .ja v a 2s . c  om
            }
        }
    };
    th.start();
    outs.write(snd1);
    TimerUtil.guaranteedSleep(100);
    outs.write(snd2);
    outs.flush();
    th.join();

    assertEquals(exp, buf);
    assertEquals(len, res[0]);
    assertNull(ex[0]);
    outs.close();
}

From source file:org.waarp.openr66.context.task.ExecMoveTask.java

@Override
public void run() {
    /*//from w w w  .  ja  va  2s .c  o  m
     * First apply all replacements and format to argRule from context and argTransfer. Will
     * call exec (from first element of resulting string) with arguments as the following value
     * from the replacements. Return 0 if OK, else 1 for a warning else as an error. The last
     * line of stdout will be the new name given to the R66File in case of status 0. The
     * previous file should be deleted by the script or will be deleted in case of status 0. If
     * the status is 1, no change is made to the file.
     */
    logger.info("ExecMove with " + argRule + ":" + argTransfer + " and {}", session);
    String finalname = argRule;
    finalname = getReplacedValue(finalname, argTransfer.split(" "));
    // Force the WaitForValidation
    waitForValidation = true;
    if (Configuration.configuration.isUseLocalExec() && useLocalExec) {
        LocalExecClient localExecClient = new LocalExecClient();
        if (localExecClient.connect()) {
            localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion);
            LocalExecResult result = localExecClient.getLocalExecResult();
            move(result.getStatus(), result.getResult(), finalname);
            localExecClient.disconnect();
            return;
        } // else continue
    }
    String[] args = finalname.split(" ");
    File exec = new File(args[0]);
    if (exec.isAbsolute()) {
        if (!exec.canExecute()) {
            logger.error("Exec command is not executable: " + finalname);
            R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner());
            futureCompletion.setResult(result);
            futureCompletion.cancel();
            return;
        }
    }
    CommandLine commandLine = new CommandLine(args[0]);
    for (int i = 1; i < args.length; i++) {
        commandLine.addArgument(args[i]);
    }
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    PipedInputStream inputStream = new PipedInputStream();
    PipedOutputStream outputStream = null;
    try {
        outputStream = new PipedOutputStream(inputStream);
    } catch (IOException e1) {
        try {
            inputStream.close();
        } catch (IOException e) {
        }
        logger.error("Exception: " + e1.getMessage() + " Exec in error with " + commandLine.toString(), e1);
        futureCompletion.setFailure(e1);
        return;
    }
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, null);
    defaultExecutor.setStreamHandler(pumpStreamHandler);
    int[] correctValues = { 0, 1 };
    defaultExecutor.setExitValues(correctValues);
    ExecuteWatchdog watchdog = null;

    if (delay > 0) {
        watchdog = new ExecuteWatchdog(delay);
        defaultExecutor.setWatchdog(watchdog);
    }
    LastLineReader lastLineReader = new LastLineReader(inputStream);
    Thread thread = new Thread(lastLineReader, "ExecRename" + session.getRunner().getSpecialId());
    thread.setDaemon(true);
    Configuration.configuration.getExecutorService().execute(thread);
    int status = -1;
    try {
        status = defaultExecutor.execute(commandLine);
    } catch (ExecuteException e) {
        if (e.getExitValue() == -559038737) {
            // Cannot run immediately so retry once
            try {
                Thread.sleep(Configuration.RETRYINMS);
            } catch (InterruptedException e1) {
            }
            try {
                status = defaultExecutor.execute(commandLine);
            } catch (ExecuteException e1) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                thread.interrupt();
                try {
                    inputStream.close();
                } catch (IOException e2) {
                }
                try {
                    pumpStreamHandler.stop();
                } catch (IOException e2) {
                }
                logger.error("ExecuteException: " + e.getMessage() + " . Exec in error with "
                        + commandLine.toString());
                futureCompletion.setFailure(e);
                return;
            } catch (IOException e1) {
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                thread.interrupt();
                try {
                    inputStream.close();
                } catch (IOException e2) {
                }
                try {
                    pumpStreamHandler.stop();
                } catch (IOException e2) {
                }
                logger.error(
                        "IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
                futureCompletion.setFailure(e);
                return;
            }
        } else {
            try {
                outputStream.close();
            } catch (IOException e1) {
            }
            thread.interrupt();
            try {
                inputStream.close();
            } catch (IOException e1) {
            }
            try {
                pumpStreamHandler.stop();
            } catch (IOException e2) {
            }
            logger.error(
                    "ExecuteException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
            futureCompletion.setFailure(e);
            return;
        }
    } catch (IOException e) {
        try {
            outputStream.close();
        } catch (IOException e1) {
        }
        thread.interrupt();
        try {
            inputStream.close();
        } catch (IOException e1) {
        }
        try {
            pumpStreamHandler.stop();
        } catch (IOException e2) {
        }
        logger.error("IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
        futureCompletion.setFailure(e);
        return;
    }
    try {
        outputStream.flush();
    } catch (IOException e) {
    }
    try {
        outputStream.close();
    } catch (IOException e) {
    }
    try {
        pumpStreamHandler.stop();
    } catch (IOException e2) {
    }
    try {
        if (delay > 0) {
            thread.join(delay);
        } else {
            thread.join();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    try {
        inputStream.close();
    } catch (IOException e1) {
    }
    String newname = null;
    if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) {
        // kill by the watchdoc (time out)
        status = -1;
        newname = "TimeOut";
    } else {
        newname = lastLineReader.getLastLine();
        if (status == 0 && (newname == null || newname.isEmpty())) {
            status = 1;
        }
    }
    move(status, newname, commandLine.toString());
}

From source file:org.waarp.openr66.context.task.ExecOutputTask.java

@Override
public void run() {
    /*/*from  ww  w  .ja  va 2  s  . com*/
     * First apply all replacements and format to argRule from context and argTransfer. Will
     * call exec (from first element of resulting string) with arguments as the following value
     * from the replacements. Return 0 if OK, else 1 for a warning else as an error. In case of
     * an error (> 0), all the line from output will be send back to the partner with the Error
     * code. No change is made to the file.
     */
    logger.info("ExecOutput with " + argRule + ":" + argTransfer + " and {}", session);
    String finalname = argRule;
    finalname = getReplacedValue(finalname, argTransfer.split(" "));
    // Force the WaitForValidation
    waitForValidation = true;
    if (Configuration.configuration.isUseLocalExec() && useLocalExec) {
        LocalExecClient localExecClient = new LocalExecClient();
        if (localExecClient.connect()) {
            localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion);
            LocalExecResult result = localExecClient.getLocalExecResult();
            finalize(result.getStatus(), result.getResult(), finalname);
            localExecClient.disconnect();
            return;
        } // else continue
    }
    String[] args = finalname.split(" ");
    File exec = new File(args[0]);
    if (exec.isAbsolute()) {
        if (!exec.canExecute()) {
            logger.error("Exec command is not executable: " + finalname);
            R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner());
            futureCompletion.setResult(result);
            futureCompletion.cancel();
            return;
        }
    }
    CommandLine commandLine = new CommandLine(args[0]);
    for (int i = 1; i < args.length; i++) {
        commandLine.addArgument(args[i]);
    }
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    PipedInputStream inputStream = new PipedInputStream();
    PipedOutputStream outputStream = null;
    try {
        outputStream = new PipedOutputStream(inputStream);
    } catch (IOException e1) {
        try {
            inputStream.close();
        } catch (IOException e) {
        }
        logger.error("Exception: " + e1.getMessage() + " Exec in error with " + commandLine.toString(), e1);
        futureCompletion.setFailure(e1);
        return;
    }
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, null);
    defaultExecutor.setStreamHandler(pumpStreamHandler);
    int[] correctValues = { 0, 1 };
    defaultExecutor.setExitValues(correctValues);
    ExecuteWatchdog watchdog = null;
    if (delay > 0) {
        watchdog = new ExecuteWatchdog(delay);
        defaultExecutor.setWatchdog(watchdog);
    }
    AllLineReader allLineReader = new AllLineReader(inputStream);
    Thread thread = new Thread(allLineReader, "ExecRename" + session.getRunner().getSpecialId());
    thread.setDaemon(true);
    Configuration.configuration.getExecutorService().execute(thread);
    int status = -1;
    try {
        status = defaultExecutor.execute(commandLine);
    } catch (ExecuteException e) {
        if (e.getExitValue() == -559038737) {
            // Cannot run immediately so retry once
            try {
                Thread.sleep(Configuration.RETRYINMS);
            } catch (InterruptedException e1) {
            }
            try {
                status = defaultExecutor.execute(commandLine);
            } catch (ExecuteException e1) {
                finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status,
                        commandLine);
                return;
            } catch (IOException e1) {
                try {
                    outputStream.flush();
                } catch (IOException e2) {
                }
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                thread.interrupt();
                try {
                    inputStream.close();
                } catch (IOException e2) {
                }
                try {
                    pumpStreamHandler.stop();
                } catch (IOException e2) {
                }
                logger.error(
                        "IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
                futureCompletion.setFailure(e);
                return;
            }
        } else {
            finalizeFromError(outputStream, pumpStreamHandler, inputStream, allLineReader, thread, status,
                    commandLine);
            return;
        }
    } catch (IOException e) {
        try {
            outputStream.close();
        } catch (IOException e1) {
        }
        thread.interrupt();
        try {
            inputStream.close();
        } catch (IOException e1) {
        }
        try {
            pumpStreamHandler.stop();
        } catch (IOException e2) {
        }
        logger.error("IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString());
        futureCompletion.setFailure(e);
        return;
    }
    try {
        outputStream.flush();
    } catch (IOException e) {
    }
    try {
        outputStream.close();
    } catch (IOException e) {
    }
    try {
        pumpStreamHandler.stop();
    } catch (IOException e2) {
    }
    try {
        if (delay > 0) {
            thread.join(delay);
        } else {
            thread.join();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    try {
        inputStream.close();
    } catch (IOException e1) {
    }
    String newname = null;
    if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) {
        // kill by the watchdoc (time out)
        status = -1;
        newname = "TimeOut";
    } else {
        newname = allLineReader.getLastLine().toString();
    }
    finalize(status, newname, commandLine.toString());
}

From source file:org.waarp.openr66.context.task.ExecOutputTask.java

private void finalizeFromError(PipedOutputStream outputStream, PumpStreamHandler pumpStreamHandler,
        PipedInputStream inputStream, AllLineReader allLineReader, Thread thread, int status,
        CommandLine commandLine) {/*  ww w.j ava 2 s . co  m*/
    try {
        Thread.sleep(Configuration.RETRYINMS);
    } catch (InterruptedException e) {
    }
    try {
        outputStream.flush();
    } catch (IOException e2) {
    }
    try {
        Thread.sleep(Configuration.RETRYINMS);
    } catch (InterruptedException e) {
    }
    try {
        outputStream.close();
    } catch (IOException e1) {
    }
    thread.interrupt();
    try {
        inputStream.close();
    } catch (IOException e1) {
    }
    try {
        Thread.sleep(Configuration.RETRYINMS);
    } catch (InterruptedException e) {
    }
    try {
        pumpStreamHandler.stop();
    } catch (IOException e2) {
    }
    try {
        Thread.sleep(Configuration.RETRYINMS);
    } catch (InterruptedException e) {
    }
    String result = allLineReader.getLastLine().toString();
    logger.error("Status: " + status + " Exec in error with " + commandLine + " returns " + result);
    OpenR66RunnerErrorException exc = new OpenR66RunnerErrorException(
            "<STATUS>" + status + "</STATUS><ERROR>" + result + "</ERROR>");
    futureCompletion.setFailure(exc);
}

From source file:ubicrypt.core.Utils.java

public static InputStream readIs(final Path path) {
    final PipedInputStream pis = new PipedInputStream();
    final AtomicLong pos = new AtomicLong(0);
    try {/*  w w  w  .  ja  v a2 s .  c o  m*/
        final PipedOutputStream ostream = new PipedOutputStream(pis);
        final AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        final ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
        channel.read(buffer, pos.get(), buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(final Integer result, final ByteBuffer buf) {
                try {
                    if (result == -1) {
                        ostream.close();
                        return;
                    }
                    final byte[] bytes = new byte[result];
                    System.arraycopy(buf.array(), 0, bytes, 0, result);
                    ostream.write(bytes);
                    ostream.flush();
                    if (result < 1 << 16) {
                        ostream.close();
                        return;
                    }
                    pos.addAndGet(result);
                    final ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
                    channel.read(buffer, pos.get(), buffer, this);
                } catch (final IOException e) {
                    Throwables.propagate(e);
                }
            }

            @Override
            public void failed(final Throwable exc, final ByteBuffer attachment) {
                log.error(exc.getMessage(), exc);
            }
        });
    } catch (final IOException e) {
        if (e instanceof NoSuchFileException) {
            throw new NotFoundException(path);
        }
        Throwables.propagate(e);
    }
    return pis;
}

From source file:ubicrypt.core.Utils.java

public static InputStream convert(final Observable<byte[]> source) {
    final PipedOutputStream pos = new PipedOutputStream();
    try {//w w  w  .ja  v  a  2 s. c  o  m
        final PipedInputStream pis = new PipedInputStream(pos);
        source.subscribe(bytes -> {
            try {
                pos.write(bytes);
                pos.flush();
            } catch (final IOException e) {
                Throwables.propagate(e);
            }
        }, err -> {
            log.error(err.getMessage(), err);
            try {
                pis.close();
            } catch (final IOException e) {
            }
        });
        return pis;
    } catch (final IOException e) {
        Throwables.propagate(e);
    }
    return null;
}