List of usage examples for java.io PipedInputStream PipedInputStream
public PipedInputStream(int pipeSize)
PipedInputStream
so that it is not yet #connect(java.io.PipedOutputStream) connected and uses the specified pipe size for the pipe's buffer. From source file:org.jumpmind.symmetric.transport.internal.InternalTransportManager.java
public IIncomingTransport getRegisterTransport(final Node client, String registrationUrl) throws IOException { final PipedOutputStream respOs = new PipedOutputStream(); final PipedInputStream respIs = new PipedInputStream(respOs); runAtClient(registrationUrl, null, respOs, new IClientRunnable() { public void run(ISymmetricEngine engine, InputStream is, OutputStream os) throws Exception { // This should be basically what the registration servlet does // ... engine.getRegistrationService().registerNode(client, os, false); }// w ww . jav a 2s. com }); return new InternalIncomingTransport(respIs); }
From source file:org.asynchttpclient.async.BodyDeferringAsyncHandlerTest.java
@Test(groups = { "standalone", "default_provider" }) public void deferredInputStreamTrick() throws IOException, ExecutionException, TimeoutException, InterruptedException { AsyncHttpClient client = getAsyncHttpClient(getAsyncHttpClientConfig()); try {/* w ww.j av a 2s . c om*/ BoundRequestBuilder r = client.prepareGet("http://127.0.0.1:" + port1 + "/deferredInputStreamTrick"); PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos); Future<Response> f = r.execute(bdah); BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis); Response resp = is.getAsapResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG)); // "consume" the body, but our code needs input stream CountingOutputStream cos = new CountingOutputStream(); try { copy(is, cos); } finally { is.close(); cos.close(); } // now we don't need to be polite, since consuming and closing // BodyDeferringInputStream does all. // it all should be here now assertEquals(cos.getByteCount(), HALF_GIG); } finally { client.close(); } }
From source file:gov.vha.isaac.rf2.filter.RF2Filter.java
private void handleFile(Path inputFile, Path outputFile) throws IOException { boolean justCopy = true; boolean justSkip = false; if (inputFile.toFile().getName().toLowerCase().endsWith(".txt")) { justCopy = false;//from ww w . ja v a 2s.co m //Filter the file BufferedReader fileReader = new BufferedReader( new InputStreamReader(new BOMInputStream(new FileInputStream(inputFile.toFile())), "UTF-8")); BufferedWriter fileWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outputFile.toFile()), "UTF-8")); PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); CSVReader csvReader = new CSVReader(new InputStreamReader(pis), '\t', CSVParser.NULL_CHARACTER); //don't look for quotes, the data is bad, and has floating instances of '"' all by itself boolean firstLine = true; String line = null; long kept = 0; long skipped = 0; long total = 0; int moduleColumn = -1; while ((line = fileReader.readLine()) != null) { total++; //Write this line into the CSV parser pos.write(line.getBytes()); pos.write("\r\n".getBytes()); pos.flush(); String[] fields = csvReader.readNext(); boolean correctModule = false; for (String ms : moduleStrings_) { if (moduleColumn >= 0 && fields[moduleColumn].equals(ms)) { correctModule = true; break; } } if (firstLine || correctModule) { kept++; fileWriter.write(line); fileWriter.write("\r\n"); } else { //don't write line skipped++; } if (firstLine) { log("Filtering file " + inputDirectory.toPath().relativize(inputFile).toString()); firstLine = false; if (fields.length < 2) { log("txt file doesn't look like a data file - abort and just copy."); justCopy = true; break; } for (int i = 0; i < fields.length; i++) { if (fields[i].equals("moduleId")) { moduleColumn = i; break; } } if (moduleColumn < 0) { log("No moduleId column found - skipping file"); justSkip = true; break; } } } fileReader.close(); csvReader.close(); fileWriter.close(); if (!justCopy) { log("Kept " + kept + " Skipped " + skipped + " out of " + total + " lines in " + inputDirectory.toPath().relativize(inputFile).toString()); } } if (justCopy) { //Just copy the file Files.copy(inputFile, outputFile, StandardCopyOption.REPLACE_EXISTING); log("Copied file " + inputDirectory.toPath().relativize(inputFile).toString()); } if (justSkip) { Files.delete(outputFile); log("Skipped file " + inputDirectory.toPath().relativize(inputFile).toString() + " because it doesn't contain a moduleId"); } }
From source file:org.modelio.vbasic.net.ApacheUriConnection.java
/** * Same as {@link java.net.URLConnection#getOutputStream()}. * <p>//w w w .j av a2s . c o m * This implementation creates a {@link PipedOutputStream} to the Apache entity input stream. * It is strongly advised to <b>write to the returned stream in another thread</b>. * @see PipedOutputStream * @see PipedInputStream * @return an output stream that writes to this connection. * @throws java.io.IOException if an I/O error occurs while creating the output stream. */ @objid("79282b13-7e13-42d5-9917-892c78a155bd") @Override public OutputStream getOutputStream() throws IOException { if (!this.dooutput) throw new IllegalStateException("This is not an output connection"); if (this.req != null && !(this.req instanceof HttpPut)) throw new IllegalStateException("This is not an output connection"); PipedOutputStream outPipe = new PipedOutputStream(); PipedInputStream snk = new PipedInputStream(outPipe); outPipe.connect(snk); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(snk); HttpPut pr = (HttpPut) getRequest(); pr.setEntity(entity); return outPipe; }
From source file:org.opennms.netmgt.eventd.adaptors.tcp.TcpStreamHandler.java
/** * The main execution context for processing a remote XML document. Once the * document is processed and an event receipt is returned to the client the * thread will exit.//from w w w. j a v a2s . c om */ @Override public void run() { // get the context and stop if necessary m_context = Thread.currentThread(); synchronized (m_context) { m_context.notifyAll(); } // check the stop flag if (m_stop) { LOG.debug("The stop flag was set prior to thread entry, closing connection"); try { m_connection.close(); } catch (final IOException e) { LOG.error("An error occured while closing the connection.", e); } LOG.debug("Thread context exiting"); return; } // Log the startup of this stream handler final InetAddress sender = m_connection.getInetAddress(); LOG.debug("Event Log Stream Handler Started for {}", sender); /* * This linked list is used to exchange * instances of PipedOutputStreams. Whenever a * pipe output stream is recovered it must be * signaled to inform the EOT thread of the * ability to write to the pipe. Also, when * the descriptor is close a EOFException is * passed on the list. */ final LinkedList<Object> pipeXchange = new LinkedList<Object>(); final TcpRecordHandler chunker = new TcpRecordHandler(m_connection, pipeXchange); final Thread tchunker = new Thread(chunker, "TCPRecord Chunker[" + InetAddressUtils.str(m_connection.getInetAddress()) + ":" + m_connection.getPort() + "]"); synchronized (tchunker) { tchunker.start(); try { tchunker.wait(); } catch (final InterruptedException e) { LOG.error("The thread was interrupted.", e); } } MAINLOOP: while (!m_stop && m_parent.getStatus() != Fiber.STOP_PENDING && m_parent.getStatus() != Fiber.STOPPED && m_recsPerConn != 0) { // get a new pipe input stream PipedInputStream pipeIn = null; synchronized (pipeXchange) { while (pipeXchange.isEmpty()) { if (chunker.isAlive()) { try { pipeXchange.wait(500); } catch (final InterruptedException e) { LOG.error("The thread was interrupted.", e); break MAINLOOP; } } else { break MAINLOOP; } } // if an exception occured then just exit the BAL (Big Ass Loop) final Object o = pipeXchange.removeFirst(); if (o instanceof Throwable) { break MAINLOOP; } // construct the other end of the pipe try { pipeIn = new PipedInputStream((PipedOutputStream) o); } catch (final IOException e) { LOG.error("An I/O exception occured construction a record reader.", e); break MAINLOOP; } // signal that we got the stream synchronized (o) { o.notify(); } } // decrement the record count if greater than zero m_recsPerConn -= (m_recsPerConn > 0 ? 1 : 0); // convert the pipe input stream into a buffered input stream final InputStream stream = new BufferedInputStream(pipeIn); // Unmarshal the XML document Log eLog = null; boolean doCleanup = false; try { eLog = JaxbUtils.unmarshal(Log.class, new InputSource(stream)); LOG.debug("Event record converted"); } catch (final Exception e) { LOG.error("Could not unmarshall the XML record.", e); doCleanup = true; } finally { if (stream != null) { IOUtils.closeQuietly(stream); } } // clean up the data on the current pipe if necessary if (doCleanup) { /* * Cleanup a failed record. Need to read * the remaining bytes from the other thread * to synchronize up. The other thread might * be blocked writing. */ try { while (stream.read() != -1) { /* do nothing */; } } catch (final IOException e) { // do nothing } // start from the top! continue MAINLOOP; } // Now that we have a list of events, process them final Event[] events = eLog.getEvents().getEvent(); // sort the events by time Arrays.sort(events, new Comparator<Event>() { @Override public int compare(final Event e1, final Event e2) { final boolean e1t = (e1.getTime() != null); final boolean e2t = (e2.getTime() != null); if (e1t && !e2t) { return 1; } else if (!e1t && e2t) { return -1; } else if (!e1t && !e2t) { return 0; } Date de1 = e1.getTime(); Date de2 = e2.getTime(); if (de1 != null && de2 != null) { return (int) (de1.getTime() - de2.getTime()); } else if (de1 == null && de2 != null) { return -1; } else if (de1 != null && de2 == null) { return 1; } else { return 0; } } }); // process the events if (events != null && events.length != 0) { final List<Event> okEvents = new ArrayList<Event>(events.length); /* * This synchronization loop will hold onto the lock * for a while. If the handlers are going to change * often, which is shouldn't then might want to consider * duplicating the handlers into an array before processing * the events. * * Doing the synchronization in the outer loop prevents spending * lots of cycles doing synchronization when it should not * normally be necesary. */ synchronized (m_handlers) { for (final EventHandler hdl : m_handlers) { /* * get the handler and then have it process all * the events in the document before moving to the * next event handler. */ for (final Event event : events) { /* * Process the event and log any errors, * but don't die on these errors */ try { LOG.debug("handling event: {}", event); // shortcut and BOTH parts MUST execute! if (hdl.processEvent(event)) { if (!okEvents.contains(event)) { okEvents.add(event); } } } catch (final Throwable t) { LOG.warn("An exception occured while processing an event.", t); } } } } // Now process the good events and send a receipt message boolean hasReceipt = false; final EventReceipt receipt = new EventReceipt(); for (final Event event : okEvents) { if (event.getUuid() != null) { receipt.addUuid(event.getUuid()); hasReceipt = true; } } if (hasReceipt) { // Transform it to XML and send it to the socket in one call try { final Writer writer = new BufferedWriter( new OutputStreamWriter(m_connection.getOutputStream(), "UTF-8")); JaxbUtils.marshal(receipt, writer); writer.flush(); synchronized (m_handlers) { for (final EventHandler hdl : m_handlers) { /* * Get the handler and then have it process all * the events in the document before moving to * the next event hander. */ try { hdl.receiptSent(receipt); } catch (final Throwable t) { LOG.warn("An exception occured while processing an event receipt.", t); } } } if (LOG.isDebugEnabled()) { try { final StringWriter swriter = new StringWriter(); JaxbUtils.marshal(receipt, swriter); LOG.debug("Sent Event Receipt {"); LOG.debug(swriter.getBuffer().toString()); LOG.debug("}"); } catch (final Throwable e) { LOG.error("An error occured during marshalling of event receipt for the log.", e); } } } catch (final IOException e) { LOG.warn("Failed to send event-receipt XML document.", e); break MAINLOOP; } } } else { LOG.debug("The agent sent an empty event stream"); } } try { LOG.debug("stopping record handler"); chunker.stop(); LOG.debug("record handler stopped"); } catch (final InterruptedException e) { LOG.warn("The thread was interrupted while trying to close the record handler.", e); } // regardless of any errors, be sure to release the socket. try { LOG.debug("closing connnection"); m_connection.close(); LOG.debug("connnection closed "); } catch (final IOException e) { LOG.warn("An I/O exception occured while closing the TCP/IP connection.", e); } LOG.debug("Thread exiting"); }
From source file:org.opennms.systemreport.AbstractSystemReportPlugin.java
protected Set<Integer> getOpenNMSProcesses() { LOG.trace("getOpenNMSProcesses()"); final Set<Integer> processes = new HashSet<Integer>(); final String jps = getResourceLocator().findBinary("jps"); LOG.trace("jps = {}", jps); DataInputStream input = null; PsParser parser = null;//w w w . j a va 2 s . c o m PipedInputStream pis = null; PipedOutputStream output = new PipedOutputStream(); DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(5000)); if (jps != null) { CommandLine command = CommandLine.parse(jps + " -v"); PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err); try { LOG.trace("executing '{}'", command); pis = new PipedInputStream(output); input = new DataInputStream(pis); parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0); parser.start(); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(command); IOUtils.closeQuietly(output); parser.join(); processes.addAll(parser.getProcesses()); LOG.trace("finished '{}'", command); if (exitValue != 0) { LOG.debug("error running '{}': exit value was {}", command, exitValue); } } catch (final Exception e) { LOG.debug("Failed to run '{}'", command, e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(pis); IOUtils.closeQuietly(output); } } LOG.trace("looking for ps"); final String ps = getResourceLocator().findBinary("ps"); if (ps != null) { // try Linux/Mac style CommandLine command = CommandLine.parse(ps + " aww -o pid -o args"); output = new PipedOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err); try { LOG.trace("executing '{}'", command); pis = new PipedInputStream(output); input = new DataInputStream(pis); parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0); parser.start(); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(command); IOUtils.closeQuietly(output); parser.join(MAX_PROCESS_WAIT); processes.addAll(parser.getProcesses()); LOG.trace("finished '{}'", command); if (exitValue != 0) { LOG.debug("error running '{}': exit value was {}", command, exitValue); } } catch (final Exception e) { LOG.debug("error running '{}'", command, e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(pis); IOUtils.closeQuietly(output); } if (processes.size() == 0) { // try Solaris style command = CommandLine.parse(ps + " -ea -o pid -o args"); output = new PipedOutputStream(); streamHandler = new PumpStreamHandler(output, System.err); try { LOG.trace("executing '{}'", command); pis = new PipedInputStream(output); input = new DataInputStream(pis); parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0); parser.start(); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(command); IOUtils.closeQuietly(output); parser.join(MAX_PROCESS_WAIT); processes.addAll(parser.getProcesses()); LOG.trace("finished '{}'", command); if (exitValue != 0) { LOG.debug("error running '{}': exit value was {}", command, exitValue); } } catch (final Exception e) { LOG.debug("error running '{}'", command, e); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(pis); IOUtils.closeQuietly(output); } } } if (processes.size() == 0) { LOG.warn("Unable to find any OpenNMS processes."); } return processes; }
From source file:org.asynchttpclient.handler.BodyDeferringAsyncHandlerTest.java
@Test(groups = "standalone") public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) { BoundRequestBuilder r = client/*from w ww . ja v a 2 s .c om*/ .prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure") .addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString()); PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos); Future<Response> f = r.execute(bdah); BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis); Response resp = is.getAsapResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG)); // "consume" the body, but our code needs input stream CountingOutputStream cos = new CountingOutputStream(); try { try { copy(is, cos); } finally { is.close(); cos.close(); } fail("InputStream consumption should fail with IOException!"); } catch (IOException e) { // good! } } }
From source file:nl.esciencecenter.xenon.adaptors.filesystems.webdav.WebdavFileSystem.java
@Override public OutputStream writeToFile(Path file, long size) throws XenonException { Path absFile = toAbsolutePath(file); assertPathNotExists(absFile);/*from w ww . j a va 2s. c om*/ assertParentDirectoryExists(absFile); try { PipedInputStream in = new PipedInputStream(4096); PipedOutputStream out = new PipedOutputStream(in); // Create a separate thread here to handle the writing new StreamToFileWriter(getFilePath(absFile), in).start(); return out; } catch (Exception e) { throw new XenonException(ADAPTOR_NAME, "Failed to open stream for writing", e); } }
From source file:org.sonar.scanner.report.ReportPublisherTest.java
@Test public void test_ws_parameters() throws Exception { ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); settings.setProperty(CoreProperties.PROJECT_ORGANIZATION_PROPERTY, "MyOrg"); WsResponse response = mock(WsResponse.class); PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out); WsCe.SubmitResponse.newBuilder().build().writeTo(out); out.close();/*from ww w . j a v a 2 s.c o m*/ when(response.failIfNotSuccessful()).thenReturn(response); when(response.contentStream()).thenReturn(in); when(wsClient.call(any(WsRequest.class))).thenReturn(response); underTest.upload(temp.newFile()); ArgumentCaptor<WsRequest> capture = ArgumentCaptor.forClass(WsRequest.class); verify(wsClient).call(capture.capture()); WsRequest wsRequest = capture.getValue(); assertThat(wsRequest.getParams()).containsOnly(entry("organization", "MyOrg"), entry("projectKey", "struts")); }
From source file:org.asynchttpclient.async.BodyDeferringAsyncHandlerTest.java
@Test(groups = { "standalone", "default_provider" }) public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException { AsyncHttpClient client = getAsyncHttpClient(getAsyncHttpClientConfig()); try {/* www. j ava 2s . com*/ BoundRequestBuilder r = client .prepareGet("http://127.0.0.1:" + port1 + "/deferredInputStreamTrickWithFailure") .addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString()); PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos); Future<Response> f = r.execute(bdah); BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis); Response resp = is.getAsapResponse(); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG)); // "consume" the body, but our code needs input stream CountingOutputStream cos = new CountingOutputStream(); try { try { copy(is, cos); } finally { is.close(); cos.close(); } fail("InputStream consumption should fail with IOException!"); } catch (IOException e) { // good! } } finally { client.close(); } }