use of java.io.PipedInputStream in project robovm by robovm.
the class OldAndroidPipedStreamTest method testA.
public void testA() throws Exception {
final PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
assertEquals(0, in.available());
TestThread reader, writer;
reader = new TestThread() {
Fibonacci fib = new Fibonacci();
@Override
public void runTest() throws Exception {
int readInt;
byte readByte;
for (; ; ) {
readInt = in.read();
if (readInt == -1) {
return;
}
readByte = (byte) readInt;
assertEquals(readByte, (byte) fib.next());
countRead++;
}
}
};
reader.start();
writer = new TestThread() {
Fibonacci fib = new Fibonacci();
@Override
public void runTest() throws Exception {
for (int i = 0; i < 2000; i++) {
int toWrite = fib.next();
out.write(toWrite);
}
out.close();
}
};
writer.start();
for (; ; ) {
try {
reader.join(60 * 1000);
writer.join(1000);
break;
} catch (InterruptedException ex) {
}
}
assertEquals(2000, reader.countRead);
if (writer.exception != null) {
throw new Exception(writer.exception);
}
if (reader.exception != null) {
throw new Exception(reader.exception);
}
}
use of java.io.PipedInputStream in project robovm by robovm.
the class OldAndroidPipedStreamTest method testB.
public void testB() throws Exception {
final PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
assertEquals(0, in.available());
TestThread reader, writer;
reader = new TestThread() {
Fibonacci fib = new Fibonacci();
@Override
public void runTest() throws Exception {
byte[] readBytes = new byte[5];
int ret;
for (; ; ) {
int nread = 0;
while (nread < 5) {
ret = in.read(readBytes, nread, readBytes.length - nread);
if (ret == -1) {
return;
}
nread += ret;
}
assertEquals(5, nread);
int readInt = (((int) readBytes[0] & 0xff) << 24) | (((int) readBytes[1] & 0xff) << 16) | (((int) readBytes[2] & 0xff) << 8) | (((int) readBytes[3] & 0xff));
assertEquals("Error at " + countRead, fib.next(), readInt);
assertEquals("Error at " + countRead, 0, readBytes[4]);
countRead++;
}
}
};
reader.start();
writer = new TestThread() {
Fibonacci fib = new Fibonacci();
@Override
public void runTest() throws Exception {
byte[] writeBytes = new byte[5];
for (int i = 0; i < 2000; i++) {
int toWrite = fib.next();
writeBytes[0] = (byte) (toWrite >> 24);
writeBytes[1] = (byte) (toWrite >> 16);
writeBytes[2] = (byte) (toWrite >> 8);
writeBytes[3] = (byte) (toWrite);
writeBytes[4] = 0;
out.write(writeBytes, 0, writeBytes.length);
}
out.close();
}
};
writer.start();
for (; ; ) {
try {
reader.join(60 * 1000);
writer.join(1000);
break;
} catch (InterruptedException ex) {
}
}
if (reader.exception != null) {
throw new Exception(reader.exception);
}
if (writer.exception != null) {
throw new Exception(writer.exception);
}
assertEquals(2000, reader.countRead);
}
use of java.io.PipedInputStream in project robovm by robovm.
the class OldBufferedReaderTest method test_8778372.
public void test_8778372() throws Exception {
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);
final Thread t = new Thread() {
@Override
public void run() {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(pos));
pw.print("hello, world\r");
pw.flush();
try {
Thread.sleep(2 * 60 * 1000);
} catch (InterruptedException ex) {
fail();
}
}
};
t.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pis));
assertEquals("hello, world", br.readLine());
}
use of java.io.PipedInputStream in project robovm by robovm.
the class DeflaterOutputStreamTest method createInflaterStream.
/**
* Creates an optionally-flushing deflater stream, writes some bytes to it,
* and flushes it. Returns an inflater stream that reads this deflater's
* output.
*
* <p>These bytes are written on a separate thread so that when the inflater
* stream is read, that read will fail when no bytes are available. Failing
* takes 3 seconds, co-ordinated by PipedInputStream's 'broken pipe'
* timeout. The 3 second delay is unfortunate but seems to be the easiest
* way demonstrate that data is unavailable. Ie. other techniques will cause
* the dry read to block indefinitely.
*/
static InputStream createInflaterStream(final Class<?> c, final boolean flushing) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
final PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
executor.submit(new Callable<Void>() {
public Void call() throws Exception {
OutputStream out;
if (c == DeflaterOutputStream.class) {
out = new DeflaterOutputStream(pout, flushing);
} else if (c == GZIPOutputStream.class) {
out = new GZIPOutputStream(pout, flushing);
} else {
throw new AssertionError();
}
out.write(1);
out.write(2);
out.write(3);
out.flush();
return null;
}
}).get();
executor.shutdown();
if (c == DeflaterOutputStream.class) {
return new InflaterInputStream(pin);
} else if (c == GZIPOutputStream.class) {
return new GZIPInputStream(pin);
} else {
throw new AssertionError();
}
}
use of java.io.PipedInputStream in project robovm by robovm.
the class Test method test_readObject_replacedClassDescriptor.
// Regression test for HARMONY-4996
public void test_readObject_replacedClassDescriptor() throws Exception {
ObjectStreamClass[] objs = new ObjectStreamClass[1000];
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
ObjectOutputStream oout = new TestObjectOutputStream(pout, objs);
oout.writeObject(new TestExtObject());
oout.writeObject("test");
oout.close();
ObjectInputStream oin = new TestObjectInputStream(pin, objs);
oin.readObject();
oin.readObject();
}
Aggregations