Java Code Examples for java.io.PipedInputStream#read()

The following examples show how to use java.io.PipedInputStream#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJMXGet.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 2
Source File: TestJMXGet.java    From big-c with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 3
Source File: InputStreamPump.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
public InputStreamPump(InputStream source, final Pump pump) throws IOException {
    this.source = source;

    out = new PipedOutputStream();
    in = new PipedInputStream(out, 8192);

    pumpThread = new Thread(new Runnable() {
        public void run() {
            try {
                pump.run(new CloseShieldInputStream(in));
                // ensure that input stream is pumping in case it didn't read to the end
                byte[] buffer = new byte[8192];
                while (in.read(buffer) >= 0);
            } catch (Exception e) {
                error = e;
                log.error("Error while processing input stream", e);
            }
        }
    });
    pumpThread.start();
}
 
Example 4
Source File: EditorConsolePane.java    From SikuliX1 with MIT License 5 votes vote down vote up
public synchronized String readLine(PipedInputStream in) throws IOException {
  String input = "";
  do {
    int available = in.available();
    if (available == 0) {
      break;
    }
    byte b[] = new byte[available];
    in.read(b);
    input = input + new String(b, 0, b.length);
  } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
  return input;
}
 
Example 5
Source File: OldAndroidPipedStreamTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
 
Example 6
Source File: OldAndroidPipedStreamTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 7
Source File: OldAndroidPipedStreamTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
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());
    }
}