use of java.io.PipedWriter 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();
}
use of java.io.PipedWriter in project robovm by robovm.
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.
}
}
use of java.io.PipedWriter in project robovm by robovm.
the class OldPipedWriterTest method test_ConstructorLjava_io_PipedReader.
public void test_ConstructorLjava_io_PipedReader() throws Exception {
PipedReader rd = new PipedReader();
try {
pw = new PipedWriter(rd);
} catch (Exception e) {
fail("Test 1: Construtor failed:" + e.getMessage());
}
readerThread = new Thread(reader = new PReader(rd), "Constructor(Reader)");
readerThread.start();
try {
pw.write(testBuf);
} catch (Exception e) {
fail("Test 2: Could not write to the constructed writer: " + e.getMessage());
}
pw.close();
assertEquals("Test 3: Incorrect character string received.", testString, reader.read(testLength));
rd = new PipedReader(new PipedWriter());
try {
pw = new PipedWriter(rd);
fail("Test 4: IOException expected because the reader is already connected.");
} catch (IOException e) {
// Expected.
}
}
use of java.io.PipedWriter in project robovm by robovm.
the class OldPipedWriterTest method test_Constructor.
public void test_Constructor() {
pw = new PipedWriter();
assertNotNull(pw);
try {
pw.close();
} catch (IOException e) {
fail("Unexpeceted IOException.");
}
}
use of java.io.PipedWriter in project robovm by robovm.
the class OldPipedWriterTest method test_close.
public void test_close() throws Exception {
PipedReader rd = new PipedReader();
pw = new PipedWriter(rd);
reader = new PReader(rd);
try {
pw.close();
} catch (IOException e) {
fail("Test 1: Unexpected IOException: " + e.getMessage());
}
}
Aggregations