List of usage examples for java.io PipedOutputStream PipedOutputStream
public PipedOutputStream(PipedInputStream snk) throws IOException
From source file:org.apache.hadoop.mapred.TestJobClient.java
private void verifyJobPriority(String jobId, String priority) throws Exception { PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(pis); int exitCode = runTool(createJobConf(), new JobClient(), new String[] { "-list", "all" }, pos); assertEquals("Exit code", 0, exitCode); BufferedReader br = new BufferedReader(new InputStreamReader(pis)); String line = null;//from www.jav a2s. c o m while ((line = br.readLine()) != null) { LOG.info("line = " + line); if (!line.startsWith(jobId)) { continue; } assertTrue(line.contains(priority)); break; } pis.close(); }
From source file:io.fabric8.kubernetes.pipeline.BuildImageStepExecution.java
@Override protected ImageInspect run() throws Exception { return workspace.getChannel().call(new MasterToSlaveCallable<ImageInspect, Exception>() { @Override/*w w w .j a v a 2 s .co m*/ public ImageInspect call() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(2); try { Future<Boolean> createTarFuture; Future<ImageInspect> buildImageFuture; try (PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(pin)) { createTarFuture = executorService.submit(new CreateTarTask(pout)); buildImageFuture = executorService.submit(new BuildImageTask(pin)); } //Wait for the two tasks to complete. if (!createTarFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS)) { listener.getLogger().println("Failed to create docker image tarball."); } ImageInspect imageInspect = buildImageFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS); if (imageInspect == null) { throw new RuntimeException("Failed to build docker image."); } else { return imageInspect; } } finally { executorService.shutdown(); if (executorService.awaitTermination(30, TimeUnit.SECONDS)) { executorService.shutdownNow(); } } } }); }
From source file:org.jaqpot.core.data.serialize.JacksonJSONSerializerTest.java
@Before public void before() throws IOException { PipedInputStream pipeInput = new PipedInputStream(); reader = new BufferedReader(new InputStreamReader(pipeInput)); out = new BufferedOutputStream(new PipedOutputStream(pipeInput)); }
From source file:de.jsurf.http.HttpServerHandler.java
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { HttpRequest request = (HttpRequest) e.getMessage(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.setHeader(CONTENT_TYPE, "video/mpts"); /*response.setChunked(true); response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED);*/ Channel c = e.getChannel();/* w ww . j a va 2 s .c o m*/ // create a media reader String inputStream = HttpServerConfiguration.getConfiguration().getChannelInput(request.getUri()); if (inputStream == null) { response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND); ChannelFuture future = c.write(response); future.addListener(ChannelFutureListener.CLOSE); return; } String path = new java.io.File(".").getCanonicalPath(); log.debug("Current execution path: " + path); String[] parameters = new String[] { "-loglevel", "error", "-i", inputStream, "-vcodec", "copy", "-acodec", "copy", "-vbsf", "h264_mp4toannexb", "-f", "mpegts", "pipe:1" }; CommandLine cmdLine = CommandLine.parse("ffmpeg.exe"); cmdLine.addArguments(parameters); DefaultExecutor executor = new DefaultExecutor(); final ExecuteWatchdog watchDog = new ExecuteWatchdog(86400000); // One day timeout executor.setWatchdog(watchDog); PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(pin); PumpStreamHandler streamHandler = new PumpStreamHandler(pout, System.err); executor.setStreamHandler(streamHandler); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(cmdLine, resultHandler); c.write(response); InputStream in = new BufferedInputStream(pin); ChannelFuture future = c.write(new ChunkedStream(in)); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { try { log.debug("operationComplete: closeChannel"); future.getChannel().close(); } catch (Exception e) { } log.debug("operationComplete: Destroy ffmpeg process"); watchDog.destroyProcess(); } }); }
From source file:org.taverna.server.master.soap.WrappedWorkflow.java
@Override public OutputStream getOutputStream() throws IOException { final PipedInputStream is = new PipedInputStream(); OutputStream os = new PipedOutputStream(is); new Worker() { @Override//w w w .ja v a 2 s . c o m public void doWork() throws IOException, ReaderException { wf = new Workflow(io.readBundle(is, null)); } @Override public void doneWork() { closeQuietly(is); } }; return os; }
From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java
/** * Constructs a new exporting input stream with the default buffer size of 64KB. * /*from w w w . j ava2 s .c om*/ * @param synchronizers the synchronizers to invoke when producing the export file * @param messagesPerExporter a reference to a map of messages that the exporters will use to produce additional info about the export * @param size the size in bytes of the intermediate buffer * @param zip whether to zip the export data * @throws IOException on failure */ public ExportingInputStream(Set<Synchronizer<?, ?>> synchronizers, Map<String, ExporterMessages> messagesPerExporter, int size, boolean zip) throws IOException { this.synchronizers = synchronizers; this.messagesPerExporter = messagesPerExporter; inputStream = new PipedInputStream(size); exportOutput = new PipedOutputStream(inputStream); zipOutput = zip; try { JAXBContext context = JAXBContext.newInstance(DefaultImportConfigurationDescriptor.class); configurationMarshaller = context.createMarshaller(); configurationMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); configurationMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); configurationMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); } catch (JAXBException e) { throw new IOException(e); } }
From source file:org.onebusaway.nyc.webapp.actions.admin.ReportingAction.java
public String submit() throws Exception { Session session = null;//from w w w. j ava2s.c o m Connection connection = null; Statement statement = null; ResultSet rs = null; try { session = sessionFactory.openSession(); connection = getConnectionFromSession(session); connection.setReadOnly(true); statement = connection.createStatement(); rs = statement.executeQuery(query); } catch (Exception e) { // make sure everything is closed if an exception was thrown try { rs.close(); } catch (Exception ex) { } try { statement.close(); } catch (Exception ex) { } try { connection.close(); } catch (Exception ex) { } try { session.close(); } catch (Exception ex) { } reportError = e.getMessage(); // not really "success", but we'll use the same template with the error displayed return SUCCESS; } // final so the output generator thread can close it final Session finalSession = session; final Connection finalConnection = connection; final Statement finalStatement = statement; final ResultSet finalRS = rs; final PipedInputStream pipedInputStream = new PipedInputStream(); final PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream); executorService.execute(new Runnable() { @Override public void run() { try { // column labels ResultSetMetaData metaData = finalRS.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 0; i < columnCount; i++) { String columnName = metaData.getColumnName(i + 1); byte[] bytes = columnName.getBytes(); if (i > 0) pipedOutputStream.write(columnDelimiter); pipedOutputStream.write(bytes); } pipedOutputStream.write(newline); // column values while (finalRS.next()) { for (int i = 0; i < columnCount; i++) { String value = finalRS.getString(i + 1); if (value == null) value = "null"; else { // remove returns value = value.replaceAll("\n|\r", ""); } byte[] valueBytes = value.getBytes(); if (i > 0) pipedOutputStream.write(columnDelimiter); pipedOutputStream.write(valueBytes); } pipedOutputStream.write(newline); } } catch (Exception e) { } finally { try { pipedOutputStream.close(); } catch (IOException e) { } try { finalRS.close(); } catch (SQLException e) { } try { finalStatement.close(); } catch (SQLException e) { } try { finalConnection.close(); } catch (SQLException e) { } try { finalSession.close(); } catch (Exception e) { } } } }); // the input stream will get populated by the piped output stream inputStream = pipedInputStream; return "download"; }
From source file:com.xebialabs.overthere.cifs.telnet.CifsTelnetConnection.java
@Override public OverthereProcess startProcess(final CmdLine cmd) { checkNotNull(cmd, "Cannot execute null command line"); checkArgument(cmd.getArguments().size() > 0, "Cannot execute empty command line"); final String obfuscatedCmd = cmd.toCommandLine(os, true); logger.info("Starting command [{}] on [{}]", obfuscatedCmd, this); try {/*w ww .ja v a 2 s. c o m*/ final TelnetClient tc = new TelnetClient(); tc.setConnectTimeout(connectionTimeoutMillis); tc.addOptionHandler(new WindowSizeOptionHandler(299, 25, true, false, true, false)); logger.info("Connecting to telnet://{}@{}", username, address); tc.connect(address, port); final InputStream stdout = tc.getInputStream(); final OutputStream stdin = tc.getOutputStream(); final PipedInputStream callersStdout = new PipedInputStream(); final PipedOutputStream toCallersStdout = new PipedOutputStream(callersStdout); final ByteArrayOutputStream outputBuf = new ByteArrayOutputStream(); final int[] exitValue = new int[1]; exitValue[0] = -1; final Thread outputReaderThread = new Thread("Telnet output reader") { @Override public void run() { try { receive(stdout, outputBuf, toCallersStdout, "ogin:"); send(stdin, username); receive(stdout, outputBuf, toCallersStdout, "assword:"); send(stdin, password); receive(stdout, outputBuf, toCallersStdout, ">", "ogon failure"); send(stdin, "PROMPT " + DETECTABLE_WINDOWS_PROMPT); // We must wait for the prompt twice; the first time is an echo of the PROMPT command, // the second is the actual prompt receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT); receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT); if (workingDirectory != null) { send(stdin, "CD /D " + workingDirectory.getPath()); receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT); } send(stdin, cmd.toCommandLine(getHostOperatingSystem(), false)); receive(stdout, outputBuf, toCallersStdout, DETECTABLE_WINDOWS_PROMPT); send(stdin, "ECHO \"" + ERRORLEVEL_PREAMBLE + "%errorlevel%" + ERRORLEVEL_POSTAMBLE); receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE); receive(stdout, outputBuf, toCallersStdout, ERRORLEVEL_POSTAMBLE); String outputBufStr = outputBuf.toString(); int preamblePos = outputBufStr.indexOf(ERRORLEVEL_PREAMBLE); int postamblePos = outputBufStr.indexOf(ERRORLEVEL_POSTAMBLE); if (preamblePos >= 0 && postamblePos >= 0) { String errorlevelString = outputBufStr .substring(preamblePos + ERRORLEVEL_PREAMBLE.length(), postamblePos); logger.debug("Errorlevel string found: {}", errorlevelString); try { synchronized (exitValue) { exitValue[0] = Integer.parseInt(errorlevelString); } } catch (NumberFormatException exc) { logger.error("Cannot parse errorlevel in Windows output: " + outputBuf); } } else { logger.error("Cannot find errorlevel in Windows output: " + outputBuf); } } catch (IOException exc) { throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc); } finally { closeQuietly(toCallersStdout); } } }; outputReaderThread.setDaemon(true); outputReaderThread.start(); return new OverthereProcess() { @Override public synchronized OutputStream getStdin() { return stdin; } @Override public synchronized InputStream getStdout() { return callersStdout; } @Override public synchronized InputStream getStderr() { return new ByteArrayInputStream(new byte[0]); } @Override public synchronized int waitFor() { if (!tc.isConnected()) { return exitValue[0]; } try { try { outputReaderThread.join(); } finally { disconnect(); } return exitValue[0]; } catch (InterruptedException exc) { throw new RuntimeIOException(format("Cannot start command [%s] on [%s]", obfuscatedCmd, CifsTelnetConnection.this), exc); } } @Override public synchronized void destroy() { if (!tc.isConnected()) { return; } disconnect(); } private synchronized void disconnect() { try { tc.disconnect(); logger.info("Disconnected from {}", CifsTelnetConnection.this); closeQuietly(toCallersStdout); } catch (IOException exc) { throw new RuntimeIOException(format("Cannot disconnect from %s", CifsTelnetConnection.this), exc); } } @Override public synchronized int exitValue() { if (tc.isConnected()) { throw new IllegalThreadStateException( format("Process for command [%s] on %s is still running", obfuscatedCmd, CifsTelnetConnection.this)); } synchronized (exitValue) { return exitValue[0]; } } }; } catch (InvalidTelnetOptionException exc) { throw new RuntimeIOException( "Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc); } catch (IOException exc) { throw new RuntimeIOException( "Cannot execute command " + cmd + " at telnet://" + username + "@" + address, exc); } }
From source file:org.waarp.openr66.context.task.ExecMoveTask.java
@Override public void run() { /*/* w w w. j a v a 2 s. c om*/ * First apply all replacements and format to argRule from context and argTransfer. Will * call exec (from first element of resulting string) with arguments as the following value * from the replacements. Return 0 if OK, else 1 for a warning else as an error. The last * line of stdout will be the new name given to the R66File in case of status 0. The * previous file should be deleted by the script or will be deleted in case of status 0. If * the status is 1, no change is made to the file. */ logger.info("ExecMove with " + argRule + ":" + argTransfer + " and {}", session); String finalname = argRule; finalname = getReplacedValue(finalname, argTransfer.split(" ")); // Force the WaitForValidation waitForValidation = true; if (Configuration.configuration.isUseLocalExec() && useLocalExec) { LocalExecClient localExecClient = new LocalExecClient(); if (localExecClient.connect()) { localExecClient.runOneCommand(finalname, delay, waitForValidation, futureCompletion); LocalExecResult result = localExecClient.getLocalExecResult(); move(result.getStatus(), result.getResult(), finalname); localExecClient.disconnect(); return; } // else continue } String[] args = finalname.split(" "); File exec = new File(args[0]); if (exec.isAbsolute()) { if (!exec.canExecute()) { logger.error("Exec command is not executable: " + finalname); R66Result result = new R66Result(session, false, ErrorCode.CommandNotFound, session.getRunner()); futureCompletion.setResult(result); futureCompletion.cancel(); return; } } CommandLine commandLine = new CommandLine(args[0]); for (int i = 1; i < args.length; i++) { commandLine.addArgument(args[i]); } DefaultExecutor defaultExecutor = new DefaultExecutor(); PipedInputStream inputStream = new PipedInputStream(); PipedOutputStream outputStream = null; try { outputStream = new PipedOutputStream(inputStream); } catch (IOException e1) { try { inputStream.close(); } catch (IOException e) { } logger.error("Exception: " + e1.getMessage() + " Exec in error with " + commandLine.toString(), e1); futureCompletion.setFailure(e1); return; } PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, null); defaultExecutor.setStreamHandler(pumpStreamHandler); int[] correctValues = { 0, 1 }; defaultExecutor.setExitValues(correctValues); ExecuteWatchdog watchdog = null; if (delay > 0) { watchdog = new ExecuteWatchdog(delay); defaultExecutor.setWatchdog(watchdog); } LastLineReader lastLineReader = new LastLineReader(inputStream); Thread thread = new Thread(lastLineReader, "ExecRename" + session.getRunner().getSpecialId()); thread.setDaemon(true); Configuration.configuration.getExecutorService().execute(thread); int status = -1; try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e) { if (e.getExitValue() == -559038737) { // Cannot run immediately so retry once try { Thread.sleep(Configuration.RETRYINMS); } catch (InterruptedException e1) { } try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e1) { try { outputStream.close(); } catch (IOException e2) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e2) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("ExecuteException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } catch (IOException e1) { try { outputStream.close(); } catch (IOException e2) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e2) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error( "IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } } else { try { outputStream.close(); } catch (IOException e1) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e1) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error( "ExecuteException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } } catch (IOException e) { try { outputStream.close(); } catch (IOException e1) { } thread.interrupt(); try { inputStream.close(); } catch (IOException e1) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } logger.error("IOException: " + e.getMessage() + " . Exec in error with " + commandLine.toString()); futureCompletion.setFailure(e); return; } try { outputStream.flush(); } catch (IOException e) { } try { outputStream.close(); } catch (IOException e) { } try { pumpStreamHandler.stop(); } catch (IOException e2) { } try { if (delay > 0) { thread.join(delay); } else { thread.join(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } try { inputStream.close(); } catch (IOException e1) { } String newname = null; if (defaultExecutor.isFailure(status) && watchdog != null && watchdog.killedProcess()) { // kill by the watchdoc (time out) status = -1; newname = "TimeOut"; } else { newname = lastLineReader.getLastLine(); if (status == 0 && (newname == null || newname.isEmpty())) { status = 1; } } move(status, newname, commandLine.toString()); }
From source file:org.mule.providers.jcr.JcrConnector.java
/** * Will get the output stream for this type of transport. Typically this * will be called only when Streaming is being used on an outbound endpoint. *///from w w w . j a v a 2s . co m public OutputStream getOutputStream(final UMOImmutableEndpoint endpoint, UMOMessage message) throws UMOException { final PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream; try { pipedOutputStream = new PipedOutputStream(pipedInputStream); } catch (IOException ioe) { throw new ConnectorException(CoreMessages.streamingFailedForEndpoint(endpoint.toString()), this, ioe); } final Map properties = new HashMap(); for (Iterator i = message.getPropertyNames().iterator(); i.hasNext();) { String propertyName = (String) i.next(); properties.put(propertyName, message.getProperty(propertyName)); } final UMOEvent event = RequestContext.getEvent(); final UMOSession session = event != null ? event.getSession() : null; // It is essential to use a different thread for reading the piped input // stream. Doing a dispatch and relying on Mule's work manager to do so // might be a better option but this would require to force threading // and never execute in the same thread even if the pool is expired. final Thread thread = new Thread(new Runnable() { public void run() { try { send(endpoint, new MuleEvent(new MuleMessage(pipedInputStream, properties), endpoint, session, true)); } catch (DispatchException de) { logger.error("Can not send streaming message!", de); } } }); thread.start(); return new CallbackOutputStream(pipedOutputStream, new JoinThreadCallback(thread)); }