List of usage examples for java.io PipedOutputStream write
public void write(int b) throws IOException
byte
to the piped output stream. From source file:org.duracloud.audit.reader.impl.AuditLogReaderImpl.java
@Override public InputStream getAuditLog(final String account, final String storeId, final String spaceId) throws AuditLogReaderException { checkEnabled();/*ww w . j a v a 2 s. c o m*/ this.storageProvider = getStorageProvider(); final String auditBucket = auditConfig.getAuditLogSpaceId(); String prefix = MessageFormat.format("{0}/{1}/{2}/", account, storeId, spaceId); final PipedInputStream is = new PipedInputStream(10 * 1024); final PipedOutputStream os; try { os = new PipedOutputStream(is); } catch (IOException e) { throw new AuditLogReaderException(e); } try { final Iterator<String> it = this.storageProvider.getSpaceContents(auditBucket, prefix); if (!it.hasNext()) { os.write((AuditLogUtil.getHeader() + "\n").getBytes()); os.close(); } new Thread(new Runnable() { @Override public void run() { try { int count = 0; while (it.hasNext()) { String contentId = it.next(); writeToOutputStream(auditBucket, storageProvider, os, count, contentId); count++; } os.close(); } catch (ContentStoreException | IOException ex) { log.error(MessageFormat.format("failed to complete audit log read routine " + "for space: storeId={0}, spaceId={1}", storeId, spaceId), ex); } } }).start(); } catch (StorageException | IOException e) { throw new AuditLogReaderException(e); } return is; }
From source file:org.gradle.launcher.daemon.server.exec.ForwardClientInput.java
public void execute(final DaemonCommandExecution execution) { final PipedOutputStream inputSource = new PipedOutputStream(); final PipedInputStream replacementStdin; try {//from w ww . j a va 2s . com replacementStdin = new PipedInputStream(inputSource); } catch (IOException e) { throw UncheckedException.throwAsUncheckedException(e); } execution.getConnection().onStdin(new StdinHandler() { public void onInput(ForwardInput input) { LOGGER.debug("Writing forwarded input on daemon's stdin."); try { inputSource.write(input.getBytes()); } catch (IOException e) { LOGGER.warn("Received exception trying to forward client input.", e); } } public void onEndOfInput() { LOGGER.info("Closing daemon's stdin at end of input."); try { inputSource.close(); } catch (IOException e) { LOGGER.warn("Problem closing output stream connected to replacement stdin", e); } finally { LOGGER.info("The daemon will no longer process any standard input."); } } }); try { try { new StdinSwapper().swap(replacementStdin, new Callable<Void>() { public Void call() { execution.proceed(); return null; } }); } finally { execution.getConnection().onStdin(null); IOUtils.closeQuietly(replacementStdin); IOUtils.closeQuietly(inputSource); } } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } }
From source file:org.lockss.util.TestStreamUtil.java
public void testReadBufShortRead() throws Exception { byte[] snd1 = { '0', '1', 0, '3' }; final int len = 12; final byte[] buf = new byte[len]; PipedOutputStream outs = new PipedOutputStream(); final InputStream ins = new PipedInputStream(outs); final Exception[] ex = { null }; final int[] res = { 0 }; Thread th = new Thread() { public void run() { try { res[0] = StreamUtil.readBytes(ins, buf, len); StreamUtil.readBytes(ins, buf, len); } catch (IOException e) { ex[0] = e;/*w w w .ja v a2 s . com*/ } } }; th.start(); outs.write(snd1); outs.close(); th.join(); assertEquals(snd1.length, res[0]); assertEquals(null, ex[0]); }
From source file:org.lockss.util.TestStreamUtil.java
public void testReadBufMultipleRead() throws Exception { byte[] snd1 = { '0', '1', 0, '3' }; byte[] snd2 = { '4', '5', '6', '7', '8', '9', 'a', 'b' }; byte[] exp = { '0', '1', 0, '3', '4', '5', '6', '7', '8', '9', 'a', 'b' }; final int len = exp.length; final byte[] buf = new byte[len]; PipedOutputStream outs = new PipedOutputStream(); final InputStream ins = new PipedInputStream(outs); final Exception[] ex = { null }; final int[] res = { 0 }; Thread th = new Thread() { public void run() { try { res[0] = StreamUtil.readBytes(ins, buf, len); } catch (IOException e) { ex[0] = e;// w w w . j a v a2s. c o m } } }; th.start(); outs.write(snd1); TimerUtil.guaranteedSleep(100); outs.write(snd2); outs.flush(); th.join(); assertEquals(exp, buf); assertEquals(len, res[0]); assertNull(ex[0]); outs.close(); }
From source file:org.springframework.integration.ip.tcp.connection.TcpNetConnectionTests.java
@Test public void transferHeaders() throws Exception { Socket inSocket = mock(Socket.class); PipedInputStream pipe = new PipedInputStream(); when(inSocket.getInputStream()).thenReturn(pipe); TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, nullPublisher, null); inboundConnection.setDeserializer(new MapJsonSerializer()); MapMessageConverter inConverter = new MapMessageConverter(); MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter); inboundConnection.setMapper(inMapper); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Socket outSocket = mock(Socket.class); TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, nullPublisher, null); when(outSocket.getOutputStream()).thenReturn(baos); MapMessageConverter outConverter = new MapMessageConverter(); outConverter.setHeaderNames("bar"); MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter); outboundConnection.setMapper(outMapper); outboundConnection.setSerializer(new MapJsonSerializer()); Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build(); outboundConnection.send(message);/*from w ww. j a va 2s. co m*/ PipedOutputStream out = new PipedOutputStream(pipe); out.write(baos.toByteArray()); out.close(); final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>(); TcpListener listener = new TcpListener() { public boolean onMessage(Message<?> message) { if (!(message instanceof ErrorMessage)) { inboundMessage.set(message); } return false; } }; inboundConnection.registerListener(listener); inboundConnection.run(); assertNotNull(inboundMessage.get()); assertEquals("foo", inboundMessage.get().getPayload()); assertEquals("baz", inboundMessage.get().getHeaders().get("bar")); }
From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java
@Test public void testInt3047ConcurrentSharedSession() throws Exception { final Session<?> session1 = this.sessionFactory.getSession(); final Session<?> session2 = this.sessionFactory.getSession(); final PipedInputStream pipe1 = new PipedInputStream(); PipedOutputStream out1 = new PipedOutputStream(pipe1); final PipedInputStream pipe2 = new PipedInputStream(); PipedOutputStream out2 = new PipedOutputStream(pipe2); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(() -> { try {//from w ww. j a va 2 s . c om session1.write(pipe1, "foo.txt"); } catch (IOException e) { e.printStackTrace(); } latch1.countDown(); }); Executors.newSingleThreadExecutor().execute(() -> { try { session2.write(pipe2, "bar.txt"); } catch (IOException e) { e.printStackTrace(); } latch2.countDown(); }); out1.write('a'); out2.write('b'); out1.write('c'); out2.write('d'); out1.write('e'); out2.write('f'); out1.close(); out2.close(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); assertTrue(latch2.await(10, TimeUnit.SECONDS)); ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); session1.read("foo.txt", bos1); session2.read("bar.txt", bos2); assertEquals("ace", new String(bos1.toByteArray())); assertEquals("bdf", new String(bos2.toByteArray())); session1.remove("foo.txt"); session2.remove("bar.txt"); session1.close(); session2.close(); }
From source file:org.whitesource.agent.utils.ZipUtils.java
private static void produceCompressDataFromText(String text, PipedOutputStream pipedOutputStream) { int start_String = 0; int chunk = text.length(); if (text.length() > STRING_MAX_SIZE) { chunk = text.length() / STRING_MAX_SIZE; }/*from ww w . j a va 2s. com*/ try { while (start_String < text.length()) { int end = start_String + chunk; if (end > text.length()) { end = text.length(); } byte[] bytes = text.substring(start_String, end).getBytes(StandardCharsets.UTF_8); pipedOutputStream.write(bytes); start_String = end; } pipedOutputStream.close(); } catch (IOException e) { // logger.error("Failed to produce data to compress : ", e); } }
From source file:org.whitesource.agent.utils.ZipUtils.java
private static void produceDecompressData(String text, PipedOutputStream pipedOutputStream) { try {// ww w . j a v a2s . c o m byte[] bytes = getStringFromDecode(text); pipedOutputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } }
From source file:ro.kuberam.libs.java.crypto.CryptoModuleTests.java
public InputStream openStream() throws IOException { final PipedOutputStream out = new PipedOutputStream(); final PipedInputStream in = new PipedInputStream(out); final Runnable exporter = () -> { try {//from w ww. j a v a 2s . c o m out.write("message".getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } IOUtils.closeQuietly(out); }; // executor.submit(exporter); return in; }
From source file:ubicrypt.core.Utils.java
public static InputStream readIs(final Path path) { final PipedInputStream pis = new PipedInputStream(); final AtomicLong pos = new AtomicLong(0); try {/*w w w . j a v a 2 s . c om*/ final PipedOutputStream ostream = new PipedOutputStream(pis); final AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); final ByteBuffer buffer = ByteBuffer.allocate(1 << 16); channel.read(buffer, pos.get(), buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(final Integer result, final ByteBuffer buf) { try { if (result == -1) { ostream.close(); return; } final byte[] bytes = new byte[result]; System.arraycopy(buf.array(), 0, bytes, 0, result); ostream.write(bytes); ostream.flush(); if (result < 1 << 16) { ostream.close(); return; } pos.addAndGet(result); final ByteBuffer buffer = ByteBuffer.allocate(1 << 16); channel.read(buffer, pos.get(), buffer, this); } catch (final IOException e) { Throwables.propagate(e); } } @Override public void failed(final Throwable exc, final ByteBuffer attachment) { log.error(exc.getMessage(), exc); } }); } catch (final IOException e) { if (e instanceof NoSuchFileException) { throw new NotFoundException(path); } Throwables.propagate(e); } return pis; }