Example usage for java.io PipedInputStream PipedInputStream

List of usage examples for java.io PipedInputStream PipedInputStream

Introduction

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

Prototype

public PipedInputStream(PipedOutputStream src, int pipeSize) throws IOException 

Source Link

Document

Creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe's buffer.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    PipedOutputStream out = new PipedOutputStream();

    PipedInputStream in = new PipedInputStream(out, 200);
}

From source file:de.upb.wdqa.wdvd.processors.output.CsvFeatureWriter.java

private static OutputStream getPipedOutputStreamStream(final OutputStream outputStream) throws IOException {
    final int BUFFER_SIZE = 1 * 1024 * 1024;

    final PipedOutputStream pipedOutputStream = new PipedOutputStream();
    final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream, BUFFER_SIZE);

    new Thread("Label Writer Output Stream") {
        @Override//from   w w  w  . ja  va 2s.c  om
        public void run() {
            try {
                IOUtils.copy(pipedInputStream, outputStream);

                pipedInputStream.close();
                outputStream.close();
            } catch (Throwable t) {
                logger.error("", t);
            }
        }
    }.start();

    return pipedOutputStream;
}

From source file:ch.cyberduck.core.cryptomator.features.CryptoChecksumCompute.java

protected Checksum compute(final InputStream in, final long offset, final ByteBuffer header,
        final NonceGenerator nonces) throws ChecksumException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Calculate checksum with header %s", header));
    }/* www.  j a v  a2s  .  co m*/
    try {
        final PipedOutputStream source = new PipedOutputStream();
        final CryptoOutputStream<Void> out = new CryptoOutputStream<Void>(new VoidStatusOutputStream(source),
                cryptomator.getCryptor(), cryptomator.getCryptor().fileHeaderCryptor().decryptHeader(header),
                nonces, cryptomator.numberOfChunks(offset));
        final PipedInputStream sink = new PipedInputStream(source,
                PreferencesFactory.get().getInteger("connection.chunksize"));
        final ThreadPool pool = ThreadPoolFactory.get("checksum", 1);
        try {
            final Future execute = pool.execute(new Callable<TransferStatus>() {
                @Override
                public TransferStatus call() throws Exception {
                    if (offset == 0) {
                        source.write(header.array());
                    }
                    final TransferStatus status = new TransferStatus();
                    new StreamCopier(status, status).transfer(in, out);
                    return status;
                }
            });
            try {
                return delegate.compute(sink, new TransferStatus());
            } finally {
                try {
                    execute.get();
                } catch (InterruptedException e) {
                    throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"),
                            e.getMessage(), e);
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof BackgroundException) {
                        throw (BackgroundException) e.getCause();
                    }
                    throw new DefaultExceptionMappingService().map(e.getCause());
                }
            }
        } finally {
            pool.shutdown(true);
        }
    } catch (ChecksumException e) {
        throw e;
    } catch (IOException | BackgroundException e) {
        throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(),
                e);
    }
}

From source file:org.apache.oozie.cli.TestCLIParser.java

private String readCommandOutput(CLIParser parser, CLIParser.Command c) throws IOException {
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut, 1024 * 10);
    System.setOut(new PrintStream(pipeOut));

    parser.showHelp(c.getCommandLine());
    pipeOut.close();//from   www .  java  2 s.c o  m
    ByteStreams.copy(pipeIn, outBytes);
    pipeIn.close();
    return new String(outBytes.toByteArray());
}

From source file:examples.RdfSerializationExample.java

/**
 * Creates a separate thread for writing into the given output stream and
 * returns a pipe output stream that can be used to pass data to this
 * thread.//from ww  w.  ja  v a2 s  .c  o m
 * <p>
 * This code is inspired by
 * http://stackoverflow.com/questions/12532073/gzipoutputstream
 * -that-does-its-compression-in-a-separate-thread
 *
 * @param outputStream
 *            the stream to write to in the thread
 * @return a new stream that data should be written to
 * @throws IOException
 *             if the pipes could not be created for some reason
 */
public static OutputStream asynchronousOutputStream(final OutputStream outputStream) throws IOException {
    final int SIZE = 1024 * 1024 * 10;
    final PipedOutputStream pos = new PipedOutputStream();
    final PipedInputStream pis = new PipedInputStream(pos, SIZE);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                byte[] bytes = new byte[SIZE];
                for (int len; (len = pis.read(bytes)) > 0;) {
                    outputStream.write(bytes, 0, len);
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            } finally {
                close(pis);
                close(outputStream);
            }
        }
    }, "async-output-stream").start();
    return pos;
}

From source file:de.tu_dresden.psy.fca.ConexpCljBridge.java

public ConexpCljBridge() {

    this.b = new byte[1];

    /**//from w  ww. j  a  v a2  s.co  m
     * build the command line (see conexp-clj/bin/conexp-clj)
     */

    String java_bin = Launcher.getJavaCommand();

    CommandLine conexp_cmd = new CommandLine(java_bin);
    conexp_cmd.addArgument("-server");
    conexp_cmd.addArgument("-cp");
    conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj-0.0.7-alpha-SNAPSHOT-standalone.jar");
    conexp_cmd.addArgument("clojure.main");
    conexp_cmd.addArgument("-e");
    conexp_cmd.addArgument("");
    conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj.clj");

    /**
     * open the pipes
     */

    this.to_conexp = new PipedOutputStream();
    try {
        this.stream_to_conexp = new PipedInputStream(this.to_conexp, 2048);
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    this.stream_error_conexp = new PipedOutputStream();
    this.stream_from_conexp = new PipedOutputStream();

    try {
        this.from_conexp = new PipedInputStream(this.stream_from_conexp, 2048);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        this.error_conexp = new PipedInputStream(this.stream_error_conexp, 2048);
    } catch (IOException e1) {

        e1.printStackTrace();
    }

    /**
     * setup apache commons exec
     */

    this.result = new DefaultExecuteResultHandler();

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setStreamHandler(
            new PumpStreamHandler(this.stream_from_conexp, this.stream_error_conexp, this.stream_to_conexp));

    /**
     * run in non-blocking mode
     */

    try {
        executor.execute(conexp_cmd, this.result);
    } catch (ExecuteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.output_buffer = "";

}

From source file:org.apache.tez.http.async.netty.AsyncHttpConnection.java

public AsyncHttpConnection(URL url, HttpConnectionParams connParams, String logIdentifier,
        JobTokenSecretManager jobTokenSecretManager) throws IOException {
    this.jobTokenSecretMgr = jobTokenSecretManager;
    this.httpConnParams = connParams;
    this.url = url;
    this.stopWatch = new StopWatch();
    if (LOG.isDebugEnabled()) {
        LOG.debug("MapOutput URL :" + url.toString());
    }//w  ww .  j a  v a 2 s  .  c  o m

    initClient(httpConnParams);
    pos = new PipedOutputStream();
    pis = new PipedInputStream(pos, httpConnParams.getBufferSize());
    handler = new TezBodyDeferringAsyncHandler(pos, url, UNIT_CONNECT_TIMEOUT);
}

From source file:net.pms.io.WindowsNamedPipe.java

public WindowsNamedPipe(String basename, boolean forceReconnect, boolean in, OutputParams params) {
    this.path = "\\\\.\\pipe\\" + basename;
    this.in = in;
    this.forceReconnect = forceReconnect;
    LOGGER.debug("Creating pipe " + this.path);

    try {/* w w  w  .  j  ava  2  s  .  c o  m*/
        if (Platform.isWindows()) {
            handle1 = Kernel32.INSTANCE.CreateNamedPipeA(this.path, 3, 0, 255, BUFSIZE, BUFSIZE, 0, null);

            if (forceReconnect) {
                handle2 = Kernel32.INSTANCE.CreateNamedPipeA(this.path, 3, 0, 255, BUFSIZE, BUFSIZE, 0, null);
            }

            if (params != null) {
                directBuffer = new BufferedOutputFileImpl(params);
            } else {
                writable = new PipedOutputStream();
                readable = new PipedInputStream((PipedOutputStream) writable, BUFSIZE);
            }

            start();

            if (forceReconnect) {
                forced = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        b2 = Kernel32.INSTANCE.ConnectNamedPipe(handle2, null);
                    }
                }, "Forced Reconnector");

                forced.start();
            }
        }
    } catch (Exception e1) {
        LOGGER.warn("Error creating Windows named pipe: {}", e1.getMessage());
        LOGGER.trace("", e1);
    }
}

From source file:org.mule.service.http.impl.service.client.async.ResponseBodyDeferringAsyncHandler.java

@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
    // body arrived, can handle the partial response
    if (!input.isPresent()) {
        if (bodyPart.isLast()) {
            // no need to stream response, we already have it all
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Single part (size = {}bytes).", bodyPart.getBodyByteBuffer().remaining());
            }//from w  w  w .  j  a va2  s .c om
            responseBuilder.accumulate(bodyPart);
            handleIfNecessary();
            return CONTINUE;
        } else {
            output = new PipedOutputStream();
            input = of(new PipedInputStream(output, bufferSize));
        }
    }
    if (LOGGER.isDebugEnabled()) {
        int bodyLength = bodyPart.getBodyByteBuffer().remaining();
        LOGGER.debug("Multiple parts (part size = {} bytes, PipedInputStream buffer size = {} bytes).",
                bodyLength, bufferSize);
        if (bufferSize - input.get().available() < bodyLength) {
            //TODO - MULE-10550: Process to detect blocking of non-io threads should take care of this
            LOGGER.debug(
                    "SELECTOR BLOCKED! No room in piped stream to write {} bytes immediately. There are still has {} bytes unread",
                    LOGGER, input.get().available());
        }
    }
    handleIfNecessary();
    try {
        bodyPart.writeTo(output);
    } catch (IOException e) {
        this.onThrowable(e);
        return ABORT;
    }
    return CONTINUE;
}

From source file:org.grails.plugin.platform.events.push.EventsPushHandler.java

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    InputStream stream = req.getInputStream();
    final String topic = readPacket(stream);
    if (topic == null || topic.isEmpty()) {
        return;/*  w w  w . j  a  v  a  2 s. c  o m*/
    }

    final String type = readPacket(stream);
    if (type == null || type.isEmpty()) {
        return;
    }

    if (type.equals(TYPE_STRING)) {
        grailsEvents.event(topic, readPacket(stream), EventsPushConstants.FROM_BROWSERS, null, null, null);
    } else if (type.equals(TYPE_JSON)) {
        grailsEvents.event(topic, new JsonSlurper().parse(new BufferedReader(new InputStreamReader(stream))),
                EventsPushConstants.FROM_BROWSERS, null, null, null);
    } else if (type.equals(TYPE_BINARY)) {
        PipedOutputStream pipeOut = new PipedOutputStream();
        PipedInputStream pipeIn = new PipedInputStream(pipeOut, 4096);
        grailsEvents.event(topic, pipeIn, EventsPushConstants.FROM_BROWSERS, null, null, null);
        IOUtils.copy(stream, pipeOut);
        pipeOut.close();

    } else if (type.equals(TYPE_REGISTER)) {
        AtmosphereResource targetResource = null;
        for (AtmosphereResource r : broadcasterFactory.lookup(EventsPushConstants.GLOBAL_TOPIC)
                .getAtmosphereResources()) {
            if (r.uuid().equalsIgnoreCase(((AtmosphereRequest) req).resource().uuid())) {
                targetResource = r;
                break;
            }
        }
        if (targetResource != null) {
            targetResource.addEventListener(
                    new AtmosphereRegistrationsHandler(registerTopics(topic, targetResource)));
        }
    } else if (type.equals(TYPE_UNREGISTER)) {
    }
}