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 File: OldPipedWriterTest.java    From j2objc with 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 #2
Source File: Piped.java    From javacore with 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 #3
Source File: WebsocketTtyTestBase.java    From termd with 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 File: OldPipedWriterTest.java    From j2objc with 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 #5
Source File: ReaderBulkReadContract.java    From openjdk-jdk9 with 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 #6
Source File: OldPipedWriterTest.java    From j2objc with 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 #7
Source File: WebsocketTtyTestBase.java    From aesh-readline with 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 #8
Source File: OldPipedWriterTest.java    From j2objc with 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 #9
Source File: OldPipedWriterTest.java    From j2objc with 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 #10
Source File: WebsocketTtyTestBase.java    From termd with 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 #11
Source File: InterruptedStreamTest.java    From j2objc with 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 #12
Source File: SysinfoAPITest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #13
Source File: InterruptedStreamTest.java    From j2objc with 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 #14
Source File: InterruptedStreamTest.java    From j2objc with 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 #15
Source File: SysinfoAPITest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #16
Source File: SysinfoAPITest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public PipedWriter getPipedWriter() {
    return pipeW;
}
 
Example #17
Source File: TreeStreamResult.java    From netbeans with 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 #18
Source File: TreeStreamResult.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public TreeStreamResult (PipedWriter writer) {
    this.writer = new TreeStreamWriter (writer);
}
 
Example #19
Source File: StreamTerm.java    From netbeans with 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 #20
Source File: RdfFileInputFormat.java    From rya with 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");
}