Code example for PipedWriter

Methods: sleep

0
    static class PWriter implements Runnable {
        public PipedWriter pw;
 
        public PWriter(PipedReader reader) {
            try { 
                pw = new PipedWriter(reader);
            } catch (Exception e) {
                System.out.println("Couldn't create writer");
            } 
        } 
 
        public PWriter() { 
            pw = new PipedWriter();
        } 
 
        public void run() { 
            try { 
                char[] c = new char[11];
                "Hello World".getChars(0, 11, c, 0);
                pw.write(c);
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            } catch (Exception e) {
                System.out.println("Exception occurred: " + e.toString());
            } 
        } 
    } 
 
    PipedReader preader;
 
    PWriter pwriter;
 
    Thread t;
 
    /** 
     * @tests java.io.PipedReader#PipedReader() 
     */ 
    public void test_Constructor() { 
        // Used in test 
    } 
 
    /** 
     * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter) 
     */ 
    public void test_ConstructorLjava_io_PipedWriter() throws IOException { 
        preader = new PipedReader(new PipedWriter());
    } 
 
    /** 
     * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter, 
     *        int) 
     * @since 1.6 
     */ 
    public void test_Constructor_LPipedWriter_I() throws Exception { 
        // Test for method java.io.PipedReader(java.io.PipedWriter, 
        // int) 
        try { 
            preader = new PipedReader(null, -1);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected 
        } 
 
        try { 
            preader = new PipedReader(null, 0);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.PipedReader#PipedReader(int) 
     * @since 1.6 
     */ 
    public void test_Constructor_I() throws Exception { 
        // Test for method java.io.PipedReader(int) 
        try { 
            preader = new PipedReader(-1);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected 
        } 
 
        try { 
            preader = new PipedReader(0);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected 
        } 
    } 
 
    /** 
     * @tests java.io.PipedReader#close() 
     */ 
    public void test_close() throws Exception { 
        char[] c = null;
        preader = new PipedReader();
        t = new Thread(new PWriter(preader), "");
        t.start();
        Thread.sleep(500); // Allow writer to start
        c = new char[11];
        preader.read(c, 0, 11);
        preader.close();
        assertEquals("Read incorrect chars", "Hello World", new String(c));
    }