Java Code Examples for java.io.PipedOutputStream#close()

The following examples show how to use java.io.PipedOutputStream#close() . 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: ChangeLoader.java    From bireme with Apache License 2.0 6 votes vote down vote up
private void tupleWriter(PipedOutputStream pipeOut, Set<String> tuples) throws BiremeException {
  byte[] data = null;

  try {
    Iterator<String> iterator = tuples.iterator();

    while (iterator.hasNext() && !cxt.stop) {
      data = iterator.next().getBytes("UTF-8");
      pipeOut.write(data);
    }

    pipeOut.flush();
  } catch (IOException e) {
    throw new BiremeException("I/O error occurs while write to pipe.", e);
  } finally {
    try {
      pipeOut.close();
    } catch (IOException ignore) {
    }
  }
}
 
Example 2
Source File: StreamPumperTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test public void shouldKnowIfPumperExpired() throws Exception {
    PipedOutputStream output = new PipedOutputStream();
    InputStream inputStream = new PipedInputStream(output);
    try {
        TestingClock clock = new TestingClock();
        StreamPumper pumper = new StreamPumper(inputStream, new TestConsumer(), "", "utf-8", clock);
        new Thread(pumper).start();

        output.write("line1\n".getBytes());
        output.flush();

        long timeoutDuration = 2L;
        assertThat(pumper.didTimeout(timeoutDuration, TimeUnit.SECONDS), is(false));
        clock.addSeconds(5);
        assertThat(pumper.didTimeout(timeoutDuration, TimeUnit.SECONDS), is(true));
    } finally {
        output.close();
    }
}
 
Example 3
Source File: TestTerminalConnection.java    From aesh-readline with Apache License 2.0 6 votes vote down vote up
@Test
public void testRead() throws IOException, InterruptedException {
    PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(outputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    TerminalConnection connection = new TerminalConnection(Charset.defaultCharset(), pipedInputStream, byteArrayOutputStream);

    final ArrayList<int[]> result = new ArrayList<>();
    connection.setStdinHandler(result::add);

    outputStream.write(("FOO").getBytes());
    outputStream.flush();
    outputStream.close();
    Thread.sleep(150);
    connection.openBlocking();

    assertArrayEquals(result.get(0), new int[] {70,79,79});
}
 
Example 4
Source File: AnyProcessTest.java    From ipc-eventbus with Apache License 2.0 6 votes vote down vote up
@Test
public void pipe_stdin() throws InterruptedException, TimeoutException, ExecutionException, IOException {
  PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream(in);
  AnyProcess proc = AnyProcess.newBuilder()
      .command("bash", "-c", "read input && echo $input")
      .pipeStdout()
      .pipeStderr()
      .pipeStdin(in)
      .recordStderr()
      .recordStdout()
      .build();
  out.write("Hello World!\n".getBytes());
  out.close();
  assertEquals(0, proc.waitFor());
  assertEquals("Hello World!\n", proc.getRecordedStdoutText());
}
 
Example 5
Source File: ProtocolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void buildACommand() throws IOException {
  PipedInputStream pis = new PipedInputStream();
  BufferedInputStream bis = new BufferedInputStream(pis);
  PipedOutputStream pos = new PipedOutputStream(pis);
  RedisOutputStream ros = new RedisOutputStream(pos);

  Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
  ros.flush();
  pos.close();
  String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";

  int b;
  StringBuilder sb = new StringBuilder();
  while ((b = bis.read()) != -1) {
    sb.append((char) b);
  }

  assertEquals(expectedCommand, sb.toString());
}
 
Example 6
Source File: TestJMXGet.java    From big-c with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 7
Source File: AbstractSshSupport.java    From sshd-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
void sshCall(String username, String password, SshExecutor executor, String channelType) {
    try {
        Session session = openSession(username, password);
        Channel channel = session.openChannel(channelType);
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream();
        channel.setInputStream(new PipedInputStream(pos));
        channel.setOutputStream(new PipedOutputStream(pis));
        channel.connect();
        try {
            executor.execute(pis, pos);
        } finally {
            pis.close();
            pos.close();
            channel.disconnect();
            session.disconnect();
        }
    } catch (JSchException | IOException ex) {
        fail(ex.toString());
    }
}
 
Example 8
Source File: OutputCollectorImpl.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
private <T> T cloneObj(T t) throws IOException
{
  Serializer<T> keySerializer;
  Class<T> keyClass;
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  keyClass = (Class<T>)t.getClass();
  keySerializer = serializationFactory.getSerializer(keyClass);
  keySerializer.open(pos);
  keySerializer.serialize(t);
  Deserializer<T> keyDesiralizer = serializationFactory.getDeserializer(keyClass);
  keyDesiralizer.open(pis);
  T clonedArg0 = keyDesiralizer.deserialize(null);
  pos.close();
  pis.close();
  keySerializer.close();
  keyDesiralizer.close();
  return clonedArg0;

}
 
Example 9
Source File: TestJMXGet.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 10
Source File: StreamPumperTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test public void shouldNotHaveExpiredTimeoutWhenCompleted() throws Exception {
    PipedOutputStream output = new PipedOutputStream();
    InputStream inputStream = new PipedInputStream(output);
    TestingClock clock = new TestingClock();
    StreamPumper pumper = new StreamPumper(inputStream, new TestConsumer(), "", "utf-8", clock);
    new Thread(pumper).start();

    output.write("line1\n".getBytes());
    output.flush();
    output.close();
    pumper.readToEnd();
    clock.addSeconds(2);
    assertThat(pumper.didTimeout(1L, TimeUnit.SECONDS), is(false));
}
 
Example 11
Source File: StreamGobblerTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testGobbleMultiLineBlockingStream() throws Exception {
    PipedOutputStream pipedOutputStream = new PipedOutputStream();
    PipedInputStream stream = new PipedInputStream(pipedOutputStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamGobbler gobbler = new StreamGobbler(stream, out, null);
    gobbler.start();
    try {
        pipedOutputStream.write("line1\n".getBytes());
        pipedOutputStream.flush();
        assertEqualsEventually(out, "line1" + NL);

        pipedOutputStream.write("line2\n".getBytes());
        pipedOutputStream.flush();
        assertEqualsEventually(out, "line1" + NL + "line2" + NL);

        pipedOutputStream.write("line".getBytes());
        pipedOutputStream.write("3\n".getBytes());
        pipedOutputStream.flush();
        assertEqualsEventually(out, "line1" + NL + "line2" + NL + "line3" + NL);

        pipedOutputStream.close();
        
        gobbler.join(10*1000);
        assertFalse(gobbler.isAlive());
        assertEquals(new String(out.toByteArray()), "line1" + NL + "line2" + NL + "line3" + NL);
    } finally {
        gobbler.close();
        gobbler.interrupt();
    }
}
 
Example 12
Source File: TestTools.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void checkOutput(String[] args, String pattern, PrintStream out,
    Class<?> clazz) {       
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  try {
    PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
    if (out == System.out) {
      System.setOut(new PrintStream(pipeOut));
    } else if (out == System.err) {
      System.setErr(new PrintStream(pipeOut));
    }

    if (clazz == DelegationTokenFetcher.class) {
      expectDelegationTokenFetcherExit(args);
    } else if (clazz == JMXGet.class) {
      expectJMXGetExit(args);
    } else if (clazz == DFSAdmin.class) {
      expectDfsAdminPrint(args);
    }
    pipeOut.close();
    ByteStreams.copy(pipeIn, outBytes);      
    pipeIn.close();
    assertTrue(new String(outBytes.toByteArray()).contains(pattern));            
  } catch (Exception ex) {
    fail("checkOutput error " + ex);
  }
}
 
Example 13
Source File: DownloadSchema.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@ApiResponses({@ApiResponse(code = 200, response = File.class, message = "")})
@GetMapping(path = "/slowInputStream")
public ResponseEntity<InputStream> slowInputStream() throws IOException {
  PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream();
  in.connect(out);

  slowInputStreamThread = new Thread(() -> {
    Thread.currentThread().setName("download thread");
    byte[] bytes = "1".getBytes();
    for (; ; ) {
      try {
        out.write(bytes);
        out.flush();
        Thread.sleep(1000);
      } catch (Throwable e) {
        break;
      }
    }
    try {
      out.close();
    } catch (final IOException ioe) {
      // ignore
    }
  });
  slowInputStreamThread.start();

  ResponseEntity<InputStream> responseEntity = ResponseEntity
      .ok()
      .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
      .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=slowInputStream.txt")
      .body(in);
  return responseEntity;
}
 
Example 14
Source File: ClientConnectionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void bigChunkTest() throws IOException, InterruptedException {
    final boolean[] success = new boolean[] { true };
    PipedOutputStream pos = createPipedConnection(success, new DummyListener());
    for (int nch = 1; nch < 10; nch++) {
        System.out.println("Num chunks: "+nch);
        for (int chs = MIN_CHUNK_LENGTH; chs < 20000; chs++) {
            pos.write(generateChunk(nch, chs));
        }
    }
    Thread.sleep(200);
    boolean retSuccess = success[0];
    pos.close();
    Assert.assertTrue(retSuccess);
}
 
Example 15
Source File: OdfPackage.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the InputStream containing whole OdfPackage.
 *
 * @return the ODF package as input stream
 * @throws java.lang.Exception
 *             - if the package could not be read
 */
public InputStream getInputStream() throws Exception {
	final PipedOutputStream os = new PipedOutputStream();
	final PipedInputStream is = new PipedInputStream();

	is.connect(os);

	Thread thread1 = new Thread() {
		@Override
		public void run() {
			try {
				save(os, mBaseURI);
			} catch (Exception e) {
			}
		}
	};

	Thread thread2 = new Thread() {
		@Override
		public void run() {
			try {
				BufferedInputStream bis = new BufferedInputStream(is,
						PAGE_SIZE);
				BufferedOutputStream bos = new BufferedOutputStream(os,
						PAGE_SIZE);
				StreamHelper.stream(bis, bos);
				is.close();
				os.close();
			} catch (Exception ie) {
			}
		}
	};

	thread1.start();
	thread2.start();

	return is;
}
 
Example 16
Source File: OldPipedOutputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_close() {
    out = new PipedOutputStream();
    rt = new Thread(reader = new PReader(out));
    rt.start();
    try {
        out.close();
    } catch (IOException e) {
        fail("Test 1: Unexpected IOException: " + e.getMessage());
    }
}
 
Example 17
Source File: OldPipedOutputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_Constructor() {
    out = new PipedOutputStream();
    assertNotNull(out);
    try {
        out.close();
    } catch (IOException e) {
        fail("Unexpeceted IOException.");
    }
}
 
Example 18
Source File: TarGenerator.java    From evosql with Apache License 2.0 4 votes vote down vote up
/**
 * After instantiating a TarEntrySupplicant, the user must either invoke
 * write() or close(), to release system resources on the input
 * File/Stream.
 * <P>
 * <B>WARNING:</B>
 * Do not use this method unless the quantity of available RAM is
 * sufficient to accommodate the specified maxBytes all at one time.
 * This constructor loads all input from the specified InputStream into
 * RAM before anything is written to disk.
 * </P>
 *
 * @param maxBytes This method will fail if more than maxBytes bytes
 *                 are supplied on the specified InputStream.
 *                 As the type of this parameter enforces, the max
 *                 size you can request is 2GB.
 */
public TarEntrySupplicant(String path, InputStream origStream,
                          int maxBytes, char typeFlag,
                          TarFileOutputStream tarStream)
        throws IOException, TarMalformatException {

    /*
     * If you modify this, make sure to not intermix reading/writing of
     * the PipedInputStream and the PipedOutputStream, or you could
     * cause dead-lock.  Everything is safe if you close the
     * PipedOutputStream before reading the PipedInputStream.
     */
    this(path, typeFlag, tarStream, 0100000000000L);

    if (maxBytes < 1) {
        throw new IllegalArgumentException(RB.read_lt_1.getString());
    }

    int               i;
    PipedOutputStream outPipe = new PipedOutputStream();

    /*
     *  This constructor not available until Java 1.6:
     * inputStream = new PipedInputStream(outPipe, maxBytes);
     */
    try {
        inputStream =
            new InputStreamWrapper(new PipedInputStream(outPipe));

        while ((i =
                origStream.read(tarStream.writeBuffer, 0,
                                tarStream.writeBuffer.length)) > 0) {
            outPipe.write(tarStream.writeBuffer, 0, i);
        }

        outPipe.flush();    // Do any good on a pipe?
        dataSize = inputStream.available();

        if (TarFileOutputStream.debug) {
            System.out.println(
                RB.stream_buffer_report.getString(
                    Long.toString(dataSize)));
        }
    } catch (IOException ioe) {
        close();

        throw ioe;
    } finally {
        try {
            outPipe.close();
        } finally {
            outPipe = null;    // Encourage buffer GC
        }
    }

    modTime = new java.util.Date().getTime() / 1000L;
}
 
Example 19
Source File: SSHShellUtil.java    From mcg-helper with Apache License 2.0 4 votes vote down vote up
public static String execute(String ip, int port, String userName, String password, String secretKey, String shell) throws JSchException, IOException {
		String response = null;
		JSch.setLogger(new ShellLogger());
		JSch jsch = new JSch();
		Session session = jsch.getSession(userName, ip, port);
		UserInfo ui = null;
		if(StringUtils.isEmpty(secretKey)) {
			ui = new SSHUserInfo(password);
		} else {
			ui = new SSHGoogleAuthUserInfo(secretKey, password);
		}
		session.setUserInfo(ui);
		session.connect(6000);

		Channel channel = session.openChannel("shell");
		PipedInputStream pipedInputStream = new PipedInputStream();
		PipedOutputStream pipedOutputStream = new PipedOutputStream();
		pipedOutputStream.connect(pipedInputStream);
		
		Thread thread = new Thread(new MonitorShellUser(channel, shell, pipedOutputStream));
		thread.start();
		
		channel.setInputStream(pipedInputStream);
		
		PipedOutputStream shellPipedOutputStream = new PipedOutputStream();
		PipedInputStream receiveStream = new PipedInputStream(); 
		shellPipedOutputStream.connect(receiveStream);
		
		channel.setOutputStream(shellPipedOutputStream);
		((ChannelShell)channel).setPtyType("vt100", 160, 24, 1000, 480);   // dumb
		//((ChannelShell)channel).setTerminalMode("binary".getBytes(Constants.CHARSET));
	//	((ChannelShell)channel).setEnv("LANG", "zh_CN.UTF-8");
		try {
			channel.connect();
			response = IOUtils.toString(receiveStream, "UTF-8");
		}finally {
//			if(channel.isClosed()) {
				pipedOutputStream.close();
				pipedInputStream.close();
				shellPipedOutputStream.close();
				receiveStream.close();
				channel.disconnect();
				session.disconnect();
			}
//		}
			
		return response;
	}
 
Example 20
Source File: AsyncHttpClientTest.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void doTest(Request request) throws InterruptedException, ExecutionException, IOException {
	final PipedOutputStream pipedOutputStream = new PipedOutputStream();
	final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);

	AsyncHandler<Response> asyncHandler = new AsyncHandler<Response>() {
		private final Response.ResponseBuilder builder = new Response.ResponseBuilder();

		@Override
		public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
			content.writeTo(pipedOutputStream);
			return STATE.CONTINUE;
		}

		@Override
		public STATE onStatusReceived(final HttpResponseStatus status) throws Exception {
			builder.accumulate(status);
			return STATE.CONTINUE;
		}

		@Override
		public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
			builder.accumulate(headers);
			return STATE.CONTINUE;
		}

		@Override
		public Response onCompleted() throws Exception {

			LOGGER.info("On complete called!");

			pipedOutputStream.flush();
			pipedOutputStream.close();

			return builder.build();

		}

		@Override
		public void onThrowable(Throwable arg0) {
			// TODO Auto-generated method stub
			LOGGER.error("Error: {}", arg0);
			onTestFailed();
		}

	};

	Future<Void> readingThreadFuture = Executors.newCachedThreadPool().submit(new Callable<Void>() {

		@Override
		public Void call() throws Exception {
			BufferedReader reader = new BufferedReader(new InputStreamReader(pipedInputStream));

			String readPrediction;

			int numPredictionsRead = 0;

			while ((readPrediction = reader.readLine()) != null) {
				//LOGGER.info("Got prediction: {}", readPrediction);
				numPredictionsRead++;
			}

			LOGGER.info("Read a total of {} predictions", numPredictionsRead);
			Assert.assertEquals(roundsOfDataToSubmit * 272274, numPredictionsRead);

			return null;
		}
	});

	Builder config = new AsyncHttpClientConfig.Builder();

	config.setRequestTimeoutInMs(-1); //need to set this to -1, to indicate wait forever. setting to 0 actually means a 0 ms timeout!

	AsyncHttpClient client = new AsyncHttpClient(config.build());

	client.executeRequest(request, asyncHandler).get();

	readingThreadFuture.get(); //verify no exceptions occurred when reading predictions

	client.close();

	Assert.assertFalse(getTestFailed());
}