Search in sources :

Example 11 with PipedOutputStream

use of java.io.PipedOutputStream 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 12 with PipedOutputStream

use of java.io.PipedOutputStream 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 13 with PipedOutputStream

use of java.io.PipedOutputStream 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)

Example 14 with PipedOutputStream

use of java.io.PipedOutputStream in project robovm by robovm.

the class Test method test_readClassDescriptor_1.

/*
     * Testing classDescriptor replacement with the value generated by
     * ObjectStreamClass.lookup() method.
     * Regression test for HARMONY-4638
     */
public void test_readClassDescriptor_1() throws IOException, ClassNotFoundException {
    A a = new A();
    a.name = "It's a test";
    PipedOutputStream pout = new PipedOutputStream();
    PipedInputStream pin = new PipedInputStream(pout);
    ObjectOutputStream out = new ObjectOutputStream(pout);
    ObjectInputStream in = new ObjectIutputStreamWithReadDesc2(pin, A.class);
    // test single object
    out.writeObject(a);
    A a1 = (A) in.readObject();
    assertEquals("Single case: incorrectly read the field of A", a.name, a1.name);
    // test cyclic reference
    HashMap m = new HashMap();
    a = new A();
    a.name = "It's a test 0";
    a1 = new A();
    a1.name = "It's a test 1";
    m.put("0", a);
    m.put("1", a1);
    out.writeObject(m);
    HashMap m1 = (HashMap) in.readObject();
    assertEquals("Incorrectly read the field of A", a.name, ((A) m1.get("0")).name);
    assertEquals("Incorrectly read the field of A1", a1.name, ((A) m1.get("1")).name);
}
Also used : HashMap(java.util.HashMap) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with PipedOutputStream

use of java.io.PipedOutputStream in project jedis by xetorthio.

the class ProtocolTest method buildACommand.

@Test
public void buildACommand() throws IOException {
    PipedInputStream pis = new PipedInputStream();
    BufferedInputStream bis = new BufferedInputStream(pis);
    PipedOutputStream pos = new PipedOutputStream(pis);
    RedisOutputStream ros = new RedisOutputStream(pos);
    Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
    ros.flush();
    pos.close();
    String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
    int b;
    StringBuilder sb = new StringBuilder();
    while ((b = bis.read()) != -1) {
        sb.append((char) b);
    }
    assertEquals(expectedCommand, sb.toString());
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RedisOutputStream(redis.clients.util.RedisOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)227 PipedInputStream (java.io.PipedInputStream)204 IOException (java.io.IOException)91 Test (org.junit.Test)55 InputStream (java.io.InputStream)28 OutputStream (java.io.OutputStream)24 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 PrintStream (java.io.PrintStream)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 InputStreamReader (java.io.InputStreamReader)16 DataInputStream (java.io.DataInputStream)14 DataOutputStream (java.io.DataOutputStream)14 BufferedReader (java.io.BufferedReader)13 Before (org.junit.Before)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ExecutorService (java.util.concurrent.ExecutorService)8 ArrayList (java.util.ArrayList)7