Example usage for java.io PipedWriter write

List of usage examples for java.io PipedWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes the specified char to the piped output stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader();

    try {//w w w . jav a 2s.  c o m
        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // read into a char array
        char[] b = new char[2];
        reader.read(b, 0, 2);

        // print the char array
        for (int i = 0; i < 2; i++) {
            System.out.println(b[i]);
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    try {//from  w w w . j  a  v a  2  s . c  o m
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader(writer, 100);

        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // check if reader is ready to read
        System.out.println(reader.ready());

        // print the char array
        for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
        }
        reader.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader(100);

    try {/*  w w w  .j  a  v a2 s  .co m*/
        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // check if reader is ready to read
        System.out.println(reader.ready());

        // print the char array
        for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader();

    try {//from w ww .j av  a 2  s.  c om
        // connect the reader and the writer
        writer.connect(reader);

        writer.write(70);
        writer.write(71);

        // flush the writer
        writer.flush();

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

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

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

public void testReadCharShortRead() throws Exception {
    char[] snd1 = { '0', '1', 0, '3' };
    final int len = 12;
    final char[] buf = new char[len];
    PipedWriter outs = new PipedWriter();
    final Reader ins = new PipedReader(outs);
    final Exception[] ex = { null };
    final int[] res = { 0 };
    Thread th = new Thread() {
        public void run() {
            try {
                res[0] = StreamUtil.readChars(ins, buf, len);
                StreamUtil.readChars(ins, buf, len);
            } catch (IOException e) {
                ex[0] = e;/* w ww . j av a2  s. com*/
            }
        }
    };
    th.start();
    outs.write(snd1);
    outs.close();
    th.join();

    assertEquals(snd1.length, res[0]);
    assertEquals(null, ex[0]);
}

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

public void testReadCharMultipleRead() throws Exception {
    char[] snd1 = { '0', '1', 0, '3' };
    char[] snd2 = { '4', '5', '6', '7', '8', '9', 'a', 'b' };
    char[] exp = { '0', '1', 0, '3', '4', '5', '6', '7', '8', '9', 'a', 'b' };
    final int len = exp.length;
    final char[] buf = new char[len];
    PipedWriter outs = new PipedWriter();
    final Reader ins = new PipedReader(outs);
    final Exception[] ex = { null };
    final int[] res = { 0 };
    Thread th = new Thread() {
        public void run() {
            try {
                res[0] = StreamUtil.readChars(ins, buf, len);
            } catch (IOException e) {
                ex[0] = e;/*from w  w w .  j  av a  2s .  com*/
            }
        }
    };
    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();
}