List of usage examples for java.io PipedInputStream read
public int read(byte b[]) throws IOException
b
. From source file:examples.RdfSerializationExample.java
/** * Creates a separate thread for writing into the given output stream and * returns a pipe output stream that can be used to pass data to this * thread./*w w w.j a va 2 s.c o m*/ * <p> * This code is inspired by * http://stackoverflow.com/questions/12532073/gzipoutputstream * -that-does-its-compression-in-a-separate-thread * * @param outputStream * the stream to write to in the thread * @return a new stream that data should be written to * @throws IOException * if the pipes could not be created for some reason */ public static OutputStream asynchronousOutputStream(final OutputStream outputStream) throws IOException { final int SIZE = 1024 * 1024 * 10; final PipedOutputStream pos = new PipedOutputStream(); final PipedInputStream pis = new PipedInputStream(pos, SIZE); new Thread(new Runnable() { @Override public void run() { try { byte[] bytes = new byte[SIZE]; for (int len; (len = pis.read(bytes)) > 0;) { outputStream.write(bytes, 0, len); } } catch (IOException ioException) { ioException.printStackTrace(); } finally { close(pis); close(outputStream); } } }, "async-output-stream").start(); return pos; }
From source file:de.resol.vbus.LiveOutputStreamTest.java
@Test public void testWriteHeader() throws Exception { Datagram refDgram1 = new Datagram(0, 0, 0x2336, 0x3335, 0x4334, 0x5333, 0x63328330); PipedInputStream refIs1 = new PipedInputStream(2048); PipedOutputStream refOs1 = new PipedOutputStream(refIs1); LiveOutputStream testOs1 = new LiveOutputStream(refOs1); testOs1.writeHeader(refDgram1);// w w w.ja v a 2 s.co m byte[] testBuffer1 = new byte[32]; int testLength1 = refIs1.read(testBuffer1); assertEquals(16, testLength1); assertEquals("aa362335332034433353300332630851", Hex.encodeHexString(testBuffer1).substring(0, 32)); }
From source file:ee.ioc.cs.vsle.util.Console.java
public String readLine(PipedInputStream in) throws IOException { String input = ""; do {// ww w .j a v a 2s.c o m 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; }
From source file:org.whitesource.agent.utils.ZipUtils.java
private static void consumeCompressData(PipedInputStream pipedInputStream, OutputStream exportByteArrayOutputStream) { try (OutputStream out = new GZIPOutputStream(new BufferedOutputStream(exportByteArrayOutputStream))) { try {//from ww w . j a va2 s . c om byte[] bytes = new byte[BYTES_BUFFER_SIZE]; int len; while ((len = pipedInputStream.read(bytes)) > 0) { out.write(bytes, 0, len); } out.flush(); } catch (IOException e) { // logger.error("Failed to consume data to compress:", e); } } catch (IOException e) { // logger.error("Failed to consume data to compress:", e); } }
From source file:org.wikidata.wdtk.client.DumpProcessingOutputAction.java
/** * Creates a separate thread for writing into the given output stream and * returns a pipe output stream that can be used to pass data to this * thread.//from ww w . j av a 2 s .co m * <p> * This code is inspired by * http://stackoverflow.com/questions/12532073/gzipoutputstream * -that-does-its-compression-in-a-separate-thread * * @param outputStream * the stream to write to in the thread * @return a new stream that data should be written to * @throws IOException * if the pipes could not be created for some reason */ protected OutputStream getAsynchronousOutputStream(final OutputStream outputStream) throws IOException { final int SIZE = 1024 * 1024 * 10; final PipedOutputStream pos = new PipedOutputStream(); final PipedInputStream pis = new PipedInputStream(pos, SIZE); final FinishableRunnable run = new FinishableRunnable() { volatile boolean finish = false; volatile boolean hasFinished = false; @Override public void finish() { this.finish = true; while (!this.hasFinished) { // loop until thread is really finished } } @Override public void run() { try { byte[] bytes = new byte[SIZE]; // Note that we finish really gently here, writing all data // that is still in the input first (in theory, new data // could arrive asynchronously, so that the thread never // finishes, but this is not the intended mode of // operation). for (int len; (!this.finish || pis.available() > 0) && (len = pis.read(bytes)) > 0;) { outputStream.write(bytes, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { close(pis); close(outputStream); this.hasFinished = true; } } }; new Thread(run, "async-output-stream").start(); this.outputStreams.add(new Closeable() { @Override public void close() throws IOException { run.finish(); } }); return pos; }