Search in sources :

Example 31 with PipedInputStream

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);
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 32 with PipedInputStream

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);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 33 with PipedInputStream

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());
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PipedOutputStream(java.io.PipedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PipedInputStream(java.io.PipedInputStream) PrintWriter(java.io.PrintWriter)

Example 34 with PipedInputStream

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();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InflaterInputStream(java.util.zip.InflaterInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedOutputStream(java.io.PipedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ExecutorService(java.util.concurrent.ExecutorService) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Callable(java.util.concurrent.Callable)

Example 35 with PipedInputStream

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();
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

PipedInputStream (java.io.PipedInputStream)128 PipedOutputStream (java.io.PipedOutputStream)121 IOException (java.io.IOException)42 Test (org.junit.Test)33 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 DataInputStream (java.io.DataInputStream)13 DataOutputStream (java.io.DataOutputStream)13 InputStream (java.io.InputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Before (org.junit.Before)10 OutputStream (java.io.OutputStream)9 PrintStream (java.io.PrintStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5