Search in sources :

Example 11 with PipedReader

use of java.io.PipedReader in project Smack by igniterealtime.

the class XMPPBOSHConnection method initDebugger.

/**
     * Initialize the SmackDebugger which allows to log and debug XML traffic.
     */
@Override
protected void initDebugger() {
    // TODO: Maybe we want to extend the SmackDebugger for simplification
    //       and a performance boost.
    // Initialize a empty writer which discards all data.
    writer = new Writer() {

        @Override
        public void write(char[] cbuf, int off, int len) {
        /* ignore */
        }

        @Override
        public void close() {
        /* ignore */
        }

        @Override
        public void flush() {
        /* ignore */
        }
    };
    // Initialize a pipe for received raw data.
    try {
        readerPipe = new PipedWriter();
        reader = new PipedReader(readerPipe);
    } catch (IOException e) {
    // Ignore
    }
    // Call the method from the parent class which initializes the debugger.
    super.initDebugger();
    // Add listeners for the received and sent raw data.
    client.addBOSHClientResponseListener(new BOSHClientResponseListener() {

        @Override
        public void responseReceived(BOSHMessageEvent event) {
            if (event.getBody() != null) {
                try {
                    readerPipe.write(event.getBody().toXML());
                    readerPipe.flush();
                } catch (Exception e) {
                // Ignore
                }
            }
        }
    });
    client.addBOSHClientRequestListener(new BOSHClientRequestListener() {

        @Override
        public void requestSent(BOSHMessageEvent event) {
            if (event.getBody() != null) {
                try {
                    writer.write(event.getBody().toXML());
                } catch (Exception e) {
                // Ignore
                }
            }
        }
    });
    // Create and start a thread which discards all read data.
    readerConsumer = new Thread() {

        private Thread thread = this;

        private int bufferLength = 1024;

        @Override
        public void run() {
            try {
                char[] cbuf = new char[bufferLength];
                while (readerConsumer == thread && !done) {
                    reader.read(cbuf, 0, bufferLength);
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    };
    readerConsumer.setDaemon(true);
    readerConsumer.start();
}
Also used : PipedWriter(java.io.PipedWriter) BOSHClientRequestListener(org.igniterealtime.jbosh.BOSHClientRequestListener) BOSHClientResponseListener(org.igniterealtime.jbosh.BOSHClientResponseListener) IOException(java.io.IOException) BOSHMessageEvent(org.igniterealtime.jbosh.BOSHMessageEvent) PipedWriter(java.io.PipedWriter) Writer(java.io.Writer) PipedReader(java.io.PipedReader) SmackException(org.jivesoftware.smack.SmackException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) BOSHException(org.igniterealtime.jbosh.BOSHException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 12 with PipedReader

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

the class OldBufferedReaderTest method test_read$CII_Exception.

public void test_read$CII_Exception() throws Exception {
    br = new BufferedReader(new Support_StringReader(testString));
    try {
        br.read(new char[10], -1, 1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        br.read(new char[10], 0, -1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        br.read(new char[10], 10, 1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    //regression for HARMONY-831
    try {
        new BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : BufferedReader(java.io.BufferedReader) PipedReader(java.io.PipedReader) Support_StringReader(tests.support.Support_StringReader)

Example 13 with PipedReader

use of java.io.PipedReader in project j2objc by google.

the class OldBufferedReaderTest method test_read$CII_Exception.

public void test_read$CII_Exception() throws Exception {
    br = new BufferedReader(new Support_StringReader(testString));
    try {
        br.read(new char[10], -1, 1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        br.read(new char[10], 0, -1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        br.read(new char[10], 10, 1);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    //regression for HARMONY-831
    try {
        new BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : BufferedReader(java.io.BufferedReader) PipedReader(java.io.PipedReader) Support_StringReader(tests.support.Support_StringReader)

Example 14 with PipedReader

use of java.io.PipedReader in project j2objc by google.

the class OldPipedWriterTest method test_connectLjava_io_PipedReader.

public void test_connectLjava_io_PipedReader() throws Exception {
    PipedReader rd = new PipedReader();
    pw = new PipedWriter();
    try {
        pw.connect(rd);
    } catch (Exception e) {
        fail("Test 1: Unexpected exception when connecting: " + e.getLocalizedMessage());
    }
    readerThread = new Thread(reader = new PReader(rd), "connect");
    readerThread.start();
    try {
        pw.write(testBuf);
    } catch (IOException e) {
        fail("Test 2: Unexpected IOException when writing after connecting.");
    }
    assertEquals("Test 3: Incorrect character string received.", testString, reader.read(testLength));
    try {
        pw.connect(new PipedReader());
        fail("Test 4: IOException expected when reconnecting the writer.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : PipedWriter(java.io.PipedWriter) IOException(java.io.IOException) PipedReader(java.io.PipedReader) IOException(java.io.IOException)

Example 15 with PipedReader

use of java.io.PipedReader in project j2objc by google.

the class OldPipedWriterTest method test_write$CII.

public void test_write$CII() throws Exception {
    pw = new PipedWriter();
    try {
        pw.write(testBuf, 0, 5);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    pw = new PipedWriter(new PipedReader());
    try {
        pw.write(testBuf, -1, 1);
        fail("Test 2: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        pw.write(testBuf, 0, -1);
        fail("Test 3: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        pw.write(testBuf, 5, testString.length());
        fail("Test 4: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    pw.close();
    pw = new PipedWriter();
    try {
        readerThread = new Thread(reader = new PReader(pw), "writeCII");
        readerThread.start();
        pw.write(testBuf, 0, testLength);
        pw.close();
        reader.read(testLength);
        assertTrue("Test 5: Characters read do not match the characters written.", Arrays.equals(testBuf, reader.buf));
    } catch (IOException e) {
        fail("Test 5: Unexpected IOException: " + e.getMessage());
    }
    readerThread.interrupt();
    try {
        pw.write(testBuf, 0, 5);
        fail("Test 6: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    reader.pr.close();
    try {
        pw.write(testBuf, 0, 5);
        fail("Test 7: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : PipedWriter(java.io.PipedWriter) 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