Search in sources :

Example 26 with PipedInputStream

use of java.io.PipedInputStream in project j2objc by google.

the class OldPipedOutputStreamTest method test_write$BII.

public void test_write$BII() throws IOException {
    out = new PipedOutputStream();
    try {
        out.write(testString.getBytes(), 0, 5);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    out = new PipedOutputStream(new PipedInputStream());
    try {
        out.write(testString.getBytes(), -1, 10);
        fail("Test 2: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        out.write(testString.getBytes(), 0, -1);
        fail("Test 3: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    try {
        out.write(testString.getBytes(), 5, testString.length());
        fail("Test 4: IndexOutOfBoundsException expected.");
    } catch (IndexOutOfBoundsException e) {
    // Expected.
    }
    out.close();
    out = new PipedOutputStream();
    try {
        rt = new Thread(reader = new PReader(out));
        rt.start();
        out.write(testString.getBytes(), 0, testString.length());
        out.flush();
        assertEquals("Test 5: Bytes read do not match the bytes written. ", testString, reader.read(testString.length()));
    } catch (IOException e) {
        fail("Test 5: Unexpected IOException: " + e.getMessage());
    }
    reader.getReader().close();
    try {
        out.write(testString.getBytes(), 0, 5);
        fail("Test 7: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) PipedInputStream(java.io.PipedInputStream)

Example 27 with PipedInputStream

use of java.io.PipedInputStream in project j2objc by google.

the class OldPipedOutputStreamTest method test_ConstructorLjava_io_PipedInputStream.

public void test_ConstructorLjava_io_PipedInputStream() throws IOException {
    try {
        out = new PipedOutputStream(new PipedInputStream());
        out.write('b');
    } catch (Exception e) {
        fail("Test 1: Constructor failed: " + e.getMessage());
    }
    out.close();
    PipedInputStream pis = new PipedInputStream(new PipedOutputStream());
    try {
        out = new PipedOutputStream(pis);
        fail("Test 2: IOException expected because the input stream is already connected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 28 with PipedInputStream

use of java.io.PipedInputStream in project j2objc by google.

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 29 with PipedInputStream

use of java.io.PipedInputStream in project j2objc by google.

the class InputStreamReaderTest method testReadDoesNotBlockUnnecessarily.

/**
     * This bug claims that InputStreamReader blocks unnecessarily:
     * http://code.google.com/p/android/issues/detail?id=10252
     */
public void testReadDoesNotBlockUnnecessarily() throws IOException {
    PipedInputStream pin = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream(pin);
    pos.write("hello".getBytes("UTF-8"));
    InputStreamReader reader = new InputStreamReader(pin);
    char[] buffer = new char[1024];
    int count = reader.read(buffer);
    assertEquals(5, count);
}
Also used : InputStreamReader(java.io.InputStreamReader) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 30 with PipedInputStream

use of java.io.PipedInputStream in project j2objc by google.

the class InterruptedStreamTest method testInterruptPipedOutputStream.

public void testInterruptPipedOutputStream() throws Exception {
    PipedOutputStream out = new PipedOutputStream();
    new PipedInputStream(out);
    testInterruptOutputStream(out);
}
Also used : PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Aggregations

PipedInputStream (java.io.PipedInputStream)122 PipedOutputStream (java.io.PipedOutputStream)118 IOException (java.io.IOException)38 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 ByteArrayInputStream (java.io.ByteArrayInputStream)7 PrintStream (java.io.PrintStream)7 InputStreamReader (java.io.InputStreamReader)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5