java.io.PipedWriter Java Examples

The following examples show how to use java.io.PipedWriter. 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 Project: javacore   Author: dunwu   File: Piped.java    License: Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    PipedWriter out = new PipedWriter();
    PipedReader in = new PipedReader();
    // 将输出流和输入流进行连接,否则在使用时会抛出IOException
    out.connect(in);
    Thread printThread = new Thread(new Print(in), "PrintThread");
    printThread.start();
    int receive = 0;
    try {
        while ((receive = System.in.read()) != -1) {
            out.write(receive);
        }
    } finally {
        out.close();
    }
}
 
Example #2
Source Project: j2objc   Author: google   File: OldPipedWriterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test_writeI() throws Exception {
    // Test for method void java.io.PipedWriter.write(int)

    pw = new PipedWriter();

    try {
        pw.write(42);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
        // Expected.
    }

    readerThread = new Thread(reader = new PReader(pw), "writeI");
    readerThread.start();
    pw.write(1);
    pw.write(2);
    pw.write(3);
    pw.close();
    reader.read(3);
    assertTrue("Test 2: The charaacters read do not match the characters written: " +
            (int) reader.buf[0] + " " + (int) reader.buf[1] + " " + (int) reader.buf[2],
            reader.buf[0] == 1 && reader.buf[1] == 2 && reader.buf[2] == 3);
}
 
Example #3
Source Project: termd   Author: alibaba   File: WebsocketTtyTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void assertConnect(String term) throws Exception {
  if (endpoint != null) {
    throw failure("Already a session");
  }
  final CountDownLatch latch = new CountDownLatch(1);
  final PipedWriter out = new PipedWriter();
  in = new PipedReader(out);
  endpoint = new Endpoint() {
    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
      session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override
        public void onMessage(String message) {
          try {
            out.write(message);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      });
      latch.countDown();
    }
    @Override
    public void onClose(Session sess, CloseReason closeReason) {
      session = null;
      endpoint = null;
      in = null;
    }
    @Override
    public void onError(Session session, Throwable thr) {
    }
  };
  ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
  WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
  session = webSocketContainer.connectToServer(endpoint, clientEndpointConfig, new URI("http://localhost:8080/ws"));
  latch.await();
}
 
Example #4
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ReaderBulkReadContract.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static PipedReader newPipedReader(String contents) {
    try (PipedWriter w = new PipedWriter()) {
        PipedReader r = new PipedReader(w);
        w.write(contents);
        return r;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example #5
Source Project: aesh-readline   Author: aeshell   File: WebsocketTtyTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void assertConnect(String term) throws Exception {
  if (endpoint != null) {
    throw failure("Already a session");
  }
  CountDownLatch latch = new CountDownLatch(1);
  PipedWriter out = new PipedWriter();
  in = new PipedReader(out);
  endpoint = new Endpoint() {
    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
      session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override
        public void onMessage(String message) {
          try {
            out.write(message);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      });
      latch.countDown();
    }
    @Override
    public void onClose(Session sess, CloseReason closeReason) {
      session = null;
      endpoint = null;
      in = null;
    }
    @Override
    public void onError(Session session, Throwable thr) {
    }
  };
  ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
  WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
  session = webSocketContainer.connectToServer(endpoint, clientEndpointConfig, new URI("http://localhost:8080/ws"));
  latch.await();
}
 
Example #6
Source Project: termd   Author: termd   File: WebsocketTtyTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void assertConnect(String term) throws Exception {
  if (endpoint != null) {
    throw failure("Already a session");
  }
  CountDownLatch latch = new CountDownLatch(1);
  PipedWriter out = new PipedWriter();
  in = new PipedReader(out);
  endpoint = new Endpoint() {
    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
      session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override
        public void onMessage(String message) {
          try {
            out.write(message);
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      });
      latch.countDown();
    }
    @Override
    public void onClose(Session sess, CloseReason closeReason) {
      session = null;
      endpoint = null;
      in = null;
    }
    @Override
    public void onError(Session session, Throwable thr) {
    }
  };
  ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
  WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
  session = webSocketContainer.connectToServer(endpoint, clientEndpointConfig, new URI("http://localhost:8080/ws"));
  latch.await();
}
 
Example #7
Source Project: j2objc   Author: google   File: InterruptedStreamTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testInterruptWriter(final PipedWriter writer) throws Exception {
    Thread thread = interruptMeLater();
    try {
        // this will block when the receiving buffer fills up
        while (true) {
            writer.write(new char[BUFFER_SIZE]);
        }
    } catch (InterruptedIOException expected) {
    } finally {
        confirmInterrupted(thread);
    }
}
 
Example #8
Source Project: j2objc   Author: google   File: OldPipedWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
public PReader(PipedWriter pw) {
    try {
        pr = new PipedReader(pw);
    } catch (IOException e) {
        System.out.println("Exception setting up reader: "
                + e.toString());
    }
}
 
Example #9
Source Project: j2objc   Author: google   File: OldPipedWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_Constructor() {
    pw = new PipedWriter();
    assertNotNull(pw);
    try {
        pw.close();
    } catch (IOException e) {
        fail("Unexpeceted IOException.");
    }
}
 
Example #10
Source Project: j2objc   Author: google   File: OldPipedWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_close() throws Exception {
    PipedReader rd = new PipedReader();
    pw = new PipedWriter(rd);
    reader = new PReader(rd);
    try {
        pw.close();
    } catch (IOException e) {
        fail("Test 1: Unexpected IOException: " + e.getMessage());
    }
}
 
Example #11
Source Project: j2objc   Author: google   File: OldPipedWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_flush() throws Exception {
    // Test for method void java.io.PipedWriter.flush()
    pw = new PipedWriter();
    readerThread = new Thread(reader = new PReader(pw), "flush");
    readerThread.start();
    pw.write(testBuf);
    pw.flush();
    assertEquals("Test 1: Flush failed. ", testString,
            reader.read(testLength));
}
 
Example #12
Source Project: netbeans   Author: apache   File: StreamTerm.java    License: Apache License 2.0 4 votes vote down vote up
Pipe(Term term) throws IOException {
    pipedReader = new PipedReader();
    pipedWriter = new PipedWriter(pipedReader);

    term.addInputListener(new TermListener());
}
 
Example #13
Source Project: netbeans   Author: apache   File: TreeStreamResult.java    License: Apache License 2.0 4 votes vote down vote up
public TreeStreamResult (PipedWriter writer) {
    this.writer = new TreeStreamWriter (writer);
}
 
Example #14
Source Project: netbeans   Author: apache   File: TreeStreamResult.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create it writing result in pipe (convertable to Reader).
 */
public TreeStreamWriter (PipedWriter writer) {
    this.writer = writer;
}
 
Example #15
Source Project: gemfirexd-oss   Author: gemxd   File: SysinfoAPITest.java    License: Apache License 2.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #16
Source Project: gemfirexd-oss   Author: gemxd   File: SysinfoAPITest.java    License: Apache License 2.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #17
Source Project: j2objc   Author: google   File: InterruptedStreamTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testInterruptPipedReader() throws Exception {
    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader(writer);
    testInterruptReader(reader);
}
 
Example #18
Source Project: j2objc   Author: google   File: InterruptedStreamTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testInterruptPipedWriter() throws Exception {
    final PipedWriter writer = new PipedWriter();
    new PipedReader(writer);
    testInterruptWriter(writer);
}
 
Example #19
Source Project: spliceengine   Author: splicemachine   File: SysinfoAPITest.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #20
Source Project: rya   Author: apache   File: RdfFileInputFormat.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Instantiates the RecordReader.
 * @param format    RDF serialization format to parse.
 * @param charBufferSize    Number of input characters to hold in
 *                          memory; if exceeded, wait until the parser
 *                          thread consumes some text before proceeding
 *                          with reading input.
 * @param statementBufferSize   Number of output statements to hold in
 *                              memory; if exceeded, wait until the
 *                              client consumes data before proceeding
 *                              with parsing.
 * @param timeoutSeconds    Number of seconds to wait for the parser
 *                          thread to provide the next statement (or
 *                          state that there are none). If exceeded,
 *                          abort.
 */
RdfFileRecordReader(RDFFormat format, int charBufferSize, int statementBufferSize, int timeoutSeconds) {
    rdfParser = Rio.createParser(format);
    rdfParser.setRDFHandler(this);
    statementCache = new LinkedBlockingQueue<RyaStatementWritable>(statementBufferSize);
    pipeOut = new PipedWriter();
    pipeIn = new PipedReader(charBufferSize);
    this.timeoutSeconds = timeoutSeconds;
    logger.info("Initializing RecordReader with parameters:");
    logger.info("\tRDF serialization format = " + format.getName());
    logger.info("\tinput buffer size = " + charBufferSize + " characters");
    logger.info("\tstatement cache size = " + statementBufferSize);
    logger.info("\tparser timeout = " + timeoutSeconds + " seconds");
}