Example usage for java.io PipedInputStream available

List of usage examples for java.io PipedInputStream available

Introduction

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

Prototype

public synchronized int available() throws IOException 

Source Link

Document

Returns the number of bytes that can be read from this input stream without blocking.

Usage

From source file:Main.java

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

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

    // connect input and output
    in.connect(out);// w w w  .  ja  v a  2 s  . c  o m

    // write something
    out.write(70);
    out.write(71);

    // print how many bytes are available
    System.out.println(in.available());

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

From source file:ee.ioc.cs.vsle.util.Console.java

public String readLine(PipedInputStream in) throws IOException {
    String input = "";
    do {/* ww  w .j  a v a2 s  .c  o  m*/
        int available = in.available();
        if (available == 0)
            break;
        byte b[] = new byte[available];
        in.read(b);
        input = input + new String(b, 0, b.length);
    } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
    return input;
}

From source file:org.wikidata.wdtk.client.DumpProcessingOutputAction.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 w w w .  ja v  a  2 s.co 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
 */
protected OutputStream getAsynchronousOutputStream(final OutputStream outputStream) throws IOException {
    final int SIZE = 1024 * 1024 * 10;
    final PipedOutputStream pos = new PipedOutputStream();
    final PipedInputStream pis = new PipedInputStream(pos, SIZE);

    final FinishableRunnable run = new FinishableRunnable() {

        volatile boolean finish = false;
        volatile boolean hasFinished = false;

        @Override
        public void finish() {
            this.finish = true;
            while (!this.hasFinished) {
                // loop until thread is really finished
            }
        }

        @Override
        public void run() {
            try {
                byte[] bytes = new byte[SIZE];
                // Note that we finish really gently here, writing all data
                // that is still in the input first (in theory, new data
                // could arrive asynchronously, so that the thread never
                // finishes, but this is not the intended mode of
                // operation).
                for (int len; (!this.finish || pis.available() > 0) && (len = pis.read(bytes)) > 0;) {
                    outputStream.write(bytes, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                close(pis);
                close(outputStream);
                this.hasFinished = true;
            }
        }
    };

    new Thread(run, "async-output-stream").start();

    this.outputStreams.add(new Closeable() {
        @Override
        public void close() throws IOException {
            run.finish();
        }
    });

    return pos;
}