Example usage for java.io PipedOutputStream connect

List of usage examples for java.io PipedOutputStream connect

Introduction

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

Prototype

public synchronized void connect(PipedInputStream snk) throws IOException 

Source Link

Document

Connects this piped output stream to a receiver.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    PipedInputStream pis = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream();
    pos.connect(pis);

    Runnable producer = () -> produceData(pos);
    Runnable consumer = () -> consumeData(pis);
    new Thread(producer).start();
    new Thread(consumer).start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    PipedOutputStream out = new PipedOutputStream();
    Main in = new Main();

    out.connect(in);

    out.write(70);/*from www. j  a v  a2 s.  co m*/
    out.write(71);
    out.write(72);

    out.flush();

    for (int i = 0; i < 3; i++) {
        System.out.println((char) in.read());
    }
    out.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    PipedOutputStream out = new PipedOutputStream();
    Main in = new Main();

    out.connect(in);

    out.write(70);/* w  ww .j a v a  2  s  . c  o m*/
    out.write(71);

    out.flush();

    // print what we wrote
    for (int i = 0; i < 2; i++) {
        System.out.println((char) in.read());
    }
    out.close();
}

From source file:Main.java

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

    byte[] b = { 'h', 'e', 'l', 'l', 'o' };

    PipedOutputStream out = new PipedOutputStream();
    Main in = new Main();

    out.connect(in);

    // write something
    out.write(b, 0, 3);//from  w  ww.jav a 2s.c o m

    // flush the stream
    out.flush();

    // print what we wrote
    for (int i = 0; i < 3; i++) {
        System.out.print((char) in.read());
    }
    out.close();
}

From source file:Main.java

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

    PipedOutputStream out = new PipedOutputStream();
    Main in = new Main();

    // connect input and output
    out.connect(in);

    // write something
    out.write(70);//w ww  .  j  a  v  a 2s.  co  m
    out.write(71);

    // print what we wrote
    for (int i = 0; i < 2; i++) {
        System.out.println("" + (char) in.read());
    }
    out.close();
}

From source file:Main.java

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

    // create a new Piped input and Output Stream
    PipedOutputStream out = new PipedOutputStream();
    Main in = new Main();

    // connect input and output
    out.connect(in);

    // write something
    out.write(70);/*from  w ww.  j  ava  2 s  . co m*/
    out.write(71);

    out.close();

    // print what we wrote
    for (int i = 0; i < 2; i++) {
        System.out.println("" + (char) in.read());
    }

}

From source file:org.talend.dataprofiler.chart.util.ChartUtils.java

public static ImageDescriptor bufferedToDescriptorOptimized(final BufferedImage image) throws IOException {
    final PipedOutputStream output = new PipedOutputStream();
    final PipedInputStream pipedInputStream = new PipedInputStream();
    output.connect(pipedInputStream);

    try {/*from   w ww. j a va2s . c o m*/
        new Thread() {

            @Override
            public void run() {
                try {
                    ChartUtilities.writeBufferedImageAsPNG(output, image, false, 0);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }.start();
    } catch (RuntimeException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        }
    }

    ImageData img = new ImageData(pipedInputStream);
    ImageDescriptor descriptor = ImageDescriptor.createFromImageData(img);
    return descriptor;
}

From source file:org.hawkular.rest.ResponseUtil.java

private static <T> InputStream pageToStream(Page<T> page, ObjectMapper mapper, Runnable callback)
        throws IOException {
    final PipedOutputStream outs = new PipedOutputStream();
    final PipedInputStream ins = new PipedInputStream() {
        @Override// w ww .j a  v a  2s .com
        public void close() throws IOException {
            outs.close();
            super.close();
        }
    };
    outs.connect(ins);
    mapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);

    PageToStreamThreadPool.getInstance().submit(() -> {
        try (Page<T> closeablePage = page;
                PipedOutputStream out = outs;
                SequenceWriter sequenceWriter = mapper.writer().writeValuesAsArray(out)) {
            for (T element : closeablePage) {
                sequenceWriter.write(element);
                sequenceWriter.flush();
            }
        } catch (IOException e) {
            throw new IllegalStateException("Unable to convert page to input stream.", e);
        } finally {
            callback.run();
        }
    });
    return ins;
}

From source file:com.ibm.stocator.fs.swift.SwiftOutputStream.java

/**
 * Default constructor//from  w  w  w . j av a  2s . c  o m
 *
 * @param account JOSS account object
 * @param url URL connection
 * @param contentType content type
 * @param metadata input metadata
 * @param connectionManager SwiftConnectionManager
 * @throws IOException if error
 */
public SwiftOutputStream(JossAccount account, URL url, final String contentType, Map<String, String> metadata,
        SwiftConnectionManager connectionManager) throws IOException {
    mUrl = url;
    totalWritten = 0;
    mAccount = account;
    client = connectionManager.createHttpConnection();
    request = new HttpPut(mUrl.toString());
    request.addHeader("X-Auth-Token", account.getAuthToken());
    if (metadata != null && !metadata.isEmpty()) {
        for (Map.Entry<String, String> entry : metadata.entrySet()) {
            request.addHeader("X-Object-Meta-" + entry.getKey(), entry.getValue());
        }
    }

    PipedOutputStream out = new PipedOutputStream();
    final PipedInputStream in = new PipedInputStream();
    out.connect(in);
    execService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    mOutputStream = out;
    Callable<Void> task = new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            InputStreamEntity entity = new InputStreamEntity(in, -1);
            entity.setChunked(true);
            entity.setContentType(contentType);
            request.setEntity(entity);

            LOG.debug("HTTP PUT request {}", mUrl.toString());
            HttpResponse response = client.execute(request);
            int responseCode = response.getStatusLine().getStatusCode();
            LOG.debug("HTTP PUT response {}. Response code {}", mUrl.toString(), responseCode);
            if (responseCode == 401) { // Unauthorized error
                mAccount.authenticate();
                request.removeHeaders("X-Auth-Token");
                request.addHeader("X-Auth-Token", mAccount.getAuthToken());
                LOG.warn("Token recreated for {}.  Retry request", mUrl.toString());
                response = client.execute(request);
                responseCode = response.getStatusLine().getStatusCode();
            }
            if (responseCode >= 400) { // Code may have changed from retrying
                throw new IOException("HTTP Error: " + responseCode + " Reason: "
                        + response.getStatusLine().getReasonPhrase());
            }

            return null;
        }
    };
    futureTask = execService.submit(task);
}

From source file:Console.java

public ConsoleTextArea() {
    super();//from   ww  w .  ja v  a2s . c om
    history = new java.util.Vector<String>();
    console1 = new ConsoleWriter(this);
    ConsoleWriter console2 = new ConsoleWriter(this);
    out = new PrintStream(console1);
    err = new PrintStream(console2);
    PipedOutputStream outPipe = new PipedOutputStream();
    inPipe = new PrintWriter(outPipe);
    in = new PipedInputStream();
    try {
        outPipe.connect(in);
    } catch (IOException exc) {
        exc.printStackTrace();
    }
    getDocument().addDocumentListener(this);
    addKeyListener(this);
    setLineWrap(true);
    setFont(new Font("Monospaced", 0, 12));
}