Search in sources :

Example 76 with PipedInputStream

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

Example 77 with PipedInputStream

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

Example 78 with PipedInputStream

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();
    }
}
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 79 with PipedInputStream

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

Example 80 with PipedInputStream

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.
}
Also used : Scanner(java.util.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PipedInputStream(java.io.PipedInputStream)

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