use of java.io.PipedInputStream in project j2objc by google.
the class OldAndroidPipedStreamTest method testC.
public void testC() throws Exception {
final PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
final byte[] readBytes = new byte[1024 * 2];
assertEquals(0, in.available());
TestThread reader, writer;
reader = new TestThread() {
@Override
public void runTest() throws Exception {
int ret;
for (; ; ) {
int nread = 0;
while (nread < readBytes.length) {
ret = in.read(readBytes, nread, readBytes.length - nread);
if (ret == -1) {
return;
}
nread += ret;
}
}
}
};
reader.start();
writer = new TestThread() {
Fibonacci fib = new Fibonacci();
@Override
public void runTest() throws Exception {
byte[] writeBytes = new byte[1024 * 2];
for (int i = 0; i < (writeBytes.length - 4); i += 4) {
int toWrite = fib.next();
writeBytes[i] = (byte) (toWrite >> 24);
writeBytes[i + 1] = (byte) (toWrite >> 16);
writeBytes[i + 2] = (byte) (toWrite >> 8);
writeBytes[i + 3] = (byte) (toWrite);
}
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);
}
Fibonacci fib = new Fibonacci();
for (int i = 0; i < (readBytes.length - 4); i += 4) {
int readInt = (((int) readBytes[i] & 0xff) << 24) | (((int) readBytes[i + 1] & 0xff) << 16) | (((int) readBytes[i + 2] & 0xff) << 8) | (((int) readBytes[i + 3] & 0xff));
assertEquals("Error at " + i, readInt, fib.next());
}
}
use of java.io.PipedInputStream in project j2objc by google.
the class OldPipedOutputStreamTest method test_connectLjava_io_PipedInputStream.
public void test_connectLjava_io_PipedInputStream() throws IOException {
out = new PipedOutputStream();
try {
out.connect(new PipedInputStream());
} catch (Exception e) {
fail("Test 1: Unexpected exception when connecting: " + e.getLocalizedMessage());
}
try {
out.write('B');
} catch (IOException e) {
fail("Test 2: Unexpected IOException when writing after connecting.");
}
try {
out.connect(new PipedInputStream());
fail("Test 3: IOException expected when reconnecting the stream.");
} catch (IOException e) {
// Expected.
}
try {
out.connect(null);
fail("Test 4: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected.
}
}
use of java.io.PipedInputStream in project j2objc by google.
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 ScannerTest method test_hasNextLine_sequence.
public void test_hasNextLine_sequence() throws IOException {
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream();
final Scanner scanner = new Scanner(pis);
pis.connect(pos);
final List<String> result = new ArrayList<String>();
Thread thread = new Thread(new Runnable() {
public void run() {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.add(line);
}
}
});
thread.start();
for (int index = 0; index < 5; index++) {
String line = "line" + index + "\n";
pos.write(line.getBytes());
pos.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
assertEquals(index + 1, result.size());
}
pis.close();
pos.close();
try {
thread.join(1000);
} catch (InterruptedException ignored) {
}
assertFalse(scanner.hasNextLine());
}
use of java.io.PipedInputStream in project robovm by robovm.
the class ScannerTest method test_ConstructorLjava_io_InputStream.
/**
* @tests java.util.Scanner#Scanner(InputStream)
*/
public void test_ConstructorLjava_io_InputStream() {
s = new Scanner(new PipedInputStream());
assertNotNull(s);
s.close();
// Scanner(InputStream)
try {
s = new Scanner((InputStream) null);
fail();
} catch (NullPointerException expected) {
}
// TODO: test if the default charset is used.
}
Aggregations