java.io.PipedReader Java Examples

The following examples show how to use java.io.PipedReader. 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: 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 #2
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 #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: 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 #5
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 #6
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 #7
Source File: InterruptedStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void testInterruptReader(final PipedReader reader) throws Exception {
    Thread thread = interruptMeLater();
    try {
        reader.read();
        fail();
    } catch (InterruptedIOException expected) {
    } finally {
        confirmInterrupted(thread);
    }
}
 
Example #8
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 #9
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 #10
Source File: Piped.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
Print(PipedReader in) {
    this.in = in;
}
 
Example #11
Source File: PipedHostTelService.java    From hasor with Apache License 2.0 4 votes vote down vote up
public PipedHostTelService(AppContext appContext, Writer outDataWriter) throws IOException {
    super(appContext);
    super.initConstructor(new PipedReader(this.inDataWriter), outDataWriter);
}
 
Example #12
Source File: OldPipedWriterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public PReader(PipedReader pr) {
    this.pr = pr;
}
 
Example #13
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 #14
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 #15
Source File: SysinfoAPITest.java    From spliceengine with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Test sysinfo.getInfo()
 *
 * Currently only tests getInfo() by comparing the first line with the
 * expected first line in English. Because so much of sysinfo changes from
 * machine-to-machine, writing a better test may be difficult.
 *
 * Test spawns a separate thread in which to call sysinfo and feed the
 * PipedWriter. Using PipedWriter and PipedReader from the same thread
 * can cause a deadlock.
 */
public void testGetInfo() throws IOException {
    sysinfo_api_helper sah = new sysinfo_api_helper();
    sah.start();
    PipedReader pipeR = new PipedReader(sah.getPipedWriter());
    BufferedReader br = new BufferedReader(pipeR);
    assertEquals("------------------ Java Information ------------------",
                 br.readLine());
    br.close();
    pipeR.close();
}
 
Example #16
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");
}
 
Example #17
Source File: SysinfoAPITest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Test sysinfo.getInfo()
 *
 * Currently only tests getInfo() by comparing the first line with the
 * expected first line in English. Because so much of sysinfo changes from
 * machine-to-machine, writing a better test may be difficult.
 *
 * Test spawns a separate thread in which to call sysinfo and feed the
 * PipedWriter. Using PipedWriter and PipedReader from the same thread
 * can cause a deadlock.
 */
public void testGetInfo() throws IOException {
    sysinfo_api_helper sah = new sysinfo_api_helper();
    sah.start();
    PipedReader pipeR = new PipedReader(sah.getPipedWriter());
    BufferedReader br = new BufferedReader(pipeR);
    assertEquals("------------------ Java Information ------------------",
                 br.readLine());
    br.close();
    pipeR.close();
}
 
Example #18
Source File: SysinfoAPITest.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Test sysinfo.getInfo()
 *
 * Currently only tests getInfo() by comparing the first line with the
 * expected first line in English. Because so much of sysinfo changes from
 * machine-to-machine, writing a better test may be difficult.
 *
 * Test spawns a separate thread in which to call sysinfo and feed the
 * PipedWriter. Using PipedWriter and PipedReader from the same thread
 * can cause a deadlock.
 */
public void testGetInfo() throws IOException {
    sysinfo_api_helper sah = new sysinfo_api_helper();
    sah.start();
    PipedReader pipeR = new PipedReader(sah.getPipedWriter());
    BufferedReader br = new BufferedReader(pipeR);
    assertEquals("------------------ Java Information ------------------",
                 br.readLine());
    br.close();
    pipeR.close();
}