Search in sources :

Example 16 with PipedReader

use of java.io.PipedReader in project robovm by robovm.

the class BufferedReaderTest method test_read$CII.

/**
	 * @tests java.io.BufferedReader#read(char[], int, int)
	 */
public void test_read$CII() throws Exception {
    char[] ca = new char[2];
    BufferedReader toRet = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[0])));
    /* Null buffer should throw NPE even when len == 0 */
    try {
        toRet.read(null, 1, 0);
        fail("null buffer reading zero bytes should throw NPE");
    } catch (NullPointerException e) {
    //expected
    }
    try {
        toRet.close();
    } catch (IOException e) {
        fail("unexpected 1: " + e);
    }
    try {
        toRet.read(null, 1, 0);
        fail("null buffer reading zero bytes on closed stream should throw IOException");
    } catch (IOException e) {
    //expected
    }
    /* Closed reader should throw IOException reading zero bytes */
    try {
        toRet.read(ca, 0, 0);
        fail("Reading zero bytes on a closed reader should not work");
    } catch (IOException e) {
    // expected
    }
    /*
		 * Closed reader should throw IOException in preference to index out of
		 * bounds
		 */
    try {
        // Read should throw IOException before
        // ArrayIndexOutOfBoundException
        toRet.read(ca, 1, 5);
        fail("IOException should have been thrown");
    } catch (IOException e) {
    // expected
    }
    // Test to ensure that a drained stream returns 0 at EOF
    toRet = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[2])));
    try {
        assertEquals("Emptying the reader should return two bytes", 2, toRet.read(ca, 0, 2));
        assertEquals("EOF on a reader should be -1", -1, toRet.read(ca, 0, 2));
        assertEquals("Reading zero bytes at EOF should work", 0, toRet.read(ca, 0, 0));
    } catch (IOException ex) {
        fail("Unexpected IOException : " + ex.getLocalizedMessage());
    }
    // Test for method int java.io.BufferedReader.read(char [], int, int)
    try {
        char[] buf = new char[testString.length()];
        br = new BufferedReader(new Support_StringReader(testString));
        br.read(buf, 50, 500);
        assertTrue("Chars read improperly", new String(buf, 50, 500).equals(testString.substring(0, 500)));
    } catch (java.io.IOException e) {
        fail("Exception during read test");
    }
    BufferedReader bufin = new BufferedReader(new Reader() {

        int size = 2, pos = 0;

        char[] contents = new char[size];

        public int read() throws IOException {
            if (pos >= size)
                throw new IOException("Read past end of data");
            return contents[pos++];
        }

        public int read(char[] buf, int off, int len) throws IOException {
            if (pos >= size)
                throw new IOException("Read past end of data");
            int toRead = len;
            if (toRead > (size - pos))
                toRead = size - pos;
            System.arraycopy(contents, pos, buf, off, toRead);
            pos += toRead;
            return toRead;
        }

        public boolean ready() throws IOException {
            return size - pos > 0;
        }

        public void close() throws IOException {
        }
    });
    try {
        bufin.read();
        int result = bufin.read(new char[2], 0, 2);
        assertTrue("Incorrect result: " + result, result == 1);
    } catch (IOException e) {
        fail("Unexpected: " + e);
    }
    //regression for HARMONY-831
    try {
        new BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    // Regression for HARMONY-54
    char[] ch = {};
    BufferedReader reader = new BufferedReader(new CharArrayReader(ch));
    try {
        // Check exception thrown when the reader is open.
        reader.read(null, 1, 0);
        fail("Assert 0: NullPointerException expected");
    } catch (NullPointerException e) {
    // Expected
    }
    // Now check IOException is thrown in preference to
    // NullPointerexception when the reader is closed.
    reader.close();
    try {
        reader.read(null, 1, 0);
        fail("Assert 1: IOException expected");
    } catch (IOException e) {
    // Expected
    }
    try {
        // And check that the IOException is thrown before
        // ArrayIndexOutOfBoundException
        reader.read(ch, 0, 42);
        fail("Assert 2: IOException expected");
    } catch (IOException e) {
    // expected
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PipedReader(java.io.PipedReader) Support_StringReader(tests.support.Support_StringReader) CharArrayReader(java.io.CharArrayReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) PipedReader(java.io.PipedReader) CharArrayReader(java.io.CharArrayReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) Support_StringReader(tests.support.Support_StringReader)

Example 17 with PipedReader

use of java.io.PipedReader in project robovm by robovm.

the class InterruptedStreamTest method testInterruptPipedReader.

public void testInterruptPipedReader() throws Exception {
    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader(writer);
    testInterruptReader(reader);
}
Also used : PipedWriter(java.io.PipedWriter) PipedReader(java.io.PipedReader)

Example 18 with PipedReader

use of java.io.PipedReader in project robovm by robovm.

the class InterruptedStreamTest method testInterruptPipedWriter.

public void testInterruptPipedWriter() throws Exception {
    final PipedWriter writer = new PipedWriter();
    new PipedReader(writer);
    testInterruptWriter(writer);
}
Also used : PipedWriter(java.io.PipedWriter) PipedReader(java.io.PipedReader)

Example 19 with PipedReader

use of java.io.PipedReader in project JMRI by JMRI.

the class ScriptOutput method getOutputArea.

/**
     * Provide access to the JTextArea containing all ScriptEngine output.
     * <P>
     * The output JTextArea is not created until this is invoked, so that code
     * that doesn't use this feature can run on GUI-less machines.
     *
     * @return component containing script output
     */
public JTextArea getOutputArea() {
    if (output == null) {
        try {
            // create the output area
            output = new JTextArea();
            // Add the I/O pipes
            PipedWriter pw = new PipedWriter();
            ScriptContext context = JmriScriptEngineManager.getDefault().getDefaultContext();
            context.setErrorWriter(pw);
            context.setWriter(pw);
            // ensure the output pipe is read and stored into a
            // Swing TextArea data model
            PipedReader pr = new PipedReader(pw);
            PipeListener pl = new PipeListener(pr, output);
            pl.start();
        } catch (IOException e) {
            log.error("Exception creating script output area", e);
            return null;
        }
    }
    return output;
}
Also used : PipedWriter(java.io.PipedWriter) JTextArea(javax.swing.JTextArea) ScriptContext(javax.script.ScriptContext) PipeListener(jmri.util.PipeListener) IOException(java.io.IOException) PipedReader(java.io.PipedReader)

Aggregations

PipedReader (java.io.PipedReader)19 PipedWriter (java.io.PipedWriter)16 IOException (java.io.IOException)11 BufferedReader (java.io.BufferedReader)3 Support_StringReader (tests.support.Support_StringReader)3 Templates (javax.xml.transform.Templates)2 Transformer (javax.xml.transform.Transformer)2 StreamResult (javax.xml.transform.stream.StreamResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 CharArrayReader (java.io.CharArrayReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 ScriptContext (javax.script.ScriptContext)1 JTextArea (javax.swing.JTextArea)1 PipeListener (jmri.util.PipeListener)1 BOSHClientRequestListener (org.igniterealtime.jbosh.BOSHClientRequestListener)1 BOSHClientResponseListener (org.igniterealtime.jbosh.BOSHClientResponseListener)1 BOSHException (org.igniterealtime.jbosh.BOSHException)1