Example usage for java.io PipedOutputStream PipedOutputStream

List of usage examples for java.io PipedOutputStream PipedOutputStream

Introduction

In this page you can find the example usage for java.io PipedOutputStream PipedOutputStream.

Prototype

public PipedOutputStream(PipedInputStream snk) throws IOException 

Source Link

Document

Creates a piped output stream connected to the specified piped input stream.

Usage

From source file:com.navjagpal.fileshare.StreamingZipEntity.java

public InputStream getContent() throws IOException, IllegalStateException {
    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    new Thread(new Runnable() {
        public void run() {
            try {
                writeTo(out);//from  w  w  w  .j  ava 2 s . c  o m
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    return in;
}

From source file:org.talend.dataprep.schema.csv.CSVSerializer.java

@Override
public InputStream serialize(InputStream rawContent, DataSetMetadata metadata, long limit) {
    try {//from  w w w.java2  s.  c  om
        PipedInputStream pipe = new PipedInputStream();
        PipedOutputStream jsonOutput = new PipedOutputStream(pipe);
        // Serialize asynchronously for better performance (especially if caller doesn't consume all, see sampling).
        Runnable r = () -> {
            final Map<String, String> parameters = metadata.getContent().getParameters();
            final String separator = parameters.get(CSVFormatFamily.SEPARATOR_PARAMETER);
            try (CSVReader reader = new CSVReader(new InputStreamReader(rawContent, metadata.getEncoding()),
                    separator.charAt(0), '\"', '\0')) {
                JsonGenerator generator = new JsonFactory().createGenerator(jsonOutput);
                int i = 0;
                while (i++ < metadata.getContent().getNbLinesInHeader()) {
                    reader.readNext(); // Skip all header lines
                }
                generator.writeStartArray();
                writeLineContent(reader, metadata, generator, separator, limit);
                generator.writeEndArray();
                generator.flush();
            } catch (Exception e) {
                // Consumer may very well interrupt consumption of stream (in case of limit(n) use for sampling).
                // This is not an issue as consumer is allowed to partially consumes results, it's up to the
                // consumer to ensure data it consumed is consistent.
                LOGGER.debug("Unable to continue serialization for {}. Skipping remaining content.",
                        metadata.getId(), e);
            } finally {
                try {
                    jsonOutput.close();
                } catch (IOException e) {
                    LOGGER.error("Unable to close output", e);
                }
            }
        };
        executor.execute(r);
        return pipe;
    } catch (IOException e) {
        throw new TDPException(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON, e);
    }
}

From source file:de.resol.vbus.LiveOutputStreamTest.java

@Test
public void testWriteHeader() throws Exception {
    Datagram refDgram1 = new Datagram(0, 0, 0x2336, 0x3335, 0x4334, 0x5333, 0x63328330);

    PipedInputStream refIs1 = new PipedInputStream(2048);
    PipedOutputStream refOs1 = new PipedOutputStream(refIs1);

    LiveOutputStream testOs1 = new LiveOutputStream(refOs1);

    testOs1.writeHeader(refDgram1);/*from  w w w.  ja v  a  2 s  .c  o  m*/

    byte[] testBuffer1 = new byte[32];
    int testLength1 = refIs1.read(testBuffer1);

    assertEquals(16, testLength1);
    assertEquals("aa362335332034433353300332630851", Hex.encodeHexString(testBuffer1).substring(0, 32));
}

From source file:fitnesse.FitNesseExpediterTest.java

@Before
public void setUp() throws Exception {
    authenticator = new PromiscuousAuthenticator(root, injector);
    root.addChildPage("FrontPage");
    expediter = new FitNesseExpediter(injector, new PipedInputStream(),
            new PipedOutputStream(new PipedInputStream()));
}

From source file:com.lithium.flow.shell.sshj.SshjDownload.java

public SshjDownload(@Nonnull SFTPClient sftp, @Nonnull String path) throws IOException {
    this.sftp = checkNotNull(sftp);
    this.path = checkNotNull(path);

    pipeIn = new PipedInputStream() {
        @Override/*from   w  ww  .  j  av  a2  s.c  o  m*/
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                Sleep.softly(latch::await);
            }
        }
    };
    pipeOut = new PipedOutputStream(pipeIn);

    new Thread(this).start();
}

From source file:org.jboss.rusheye.parser.AbstractVisualSuiteDefinitionTest.java

@BeforeMethod
public void prepareEnvironment() throws IOException, SAXException {
    stub = new VisualSuiteStub();

    PipedInputStream in = new PipedInputStream();
    PipedOutputStream writerOut = new PipedOutputStream(in);
    documentOutputStream = new ByteArrayOutputStream();
    TeeOutputStream out = new TeeOutputStream(writerOut, documentOutputStream);

    OutputFormat format = new OutputFormat("\t", true);
    writer = new XMLWriter(out, format);
    // inputSource = new InputSource(in);
    inputStream = in;/*from   ww  w.  j  a va 2  s.co  m*/

    parser = new Parser();

    handler = parser.getHandler();
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.jena.SesameSyncUtils.java

public void writeModelToSesameContext(Model jenaModel, String serverURI, String repositoryId, String contextId)
        throws RepositoryException, IOException, RDFParseException {
    Repository myRepository = new HTTPRepository(serverURI, repositoryId);
    myRepository.initialize();/*from ww  w.  j  ava 2 s . com*/
    RepositoryConnection myConn = myRepository.getConnection();

    myConn.setAutoCommit(false);
    try {

        Resource contextRes = (contextId != null) ? new URIImpl(contextId) : null;

        if (contextRes != null) {
            myConn.clear(contextRes);
        } else {
            myConn.clear();
        }

        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream(in);

        try {

            new Thread(new JenaOutputter(jenaModel, out, myConn), "SesameSyncUtilities.JenaOutputter").start();

            if (contextRes != null) {
                myConn.add(in, "http://example.org/base/", RDFFormat.NTRIPLES, contextRes);
            } else {
                myConn.add(in, "http://example.org/base/", RDFFormat.NTRIPLES);
            }
        } finally {
            in.close();
        }

        myConn.commit();

    } catch (Throwable e) {
        myConn.rollback();
        e.printStackTrace();
        log.error("Error writing to Sesame repository", e);
        throw new RuntimeException("Error writing to Sesame repository", e);
    } finally {
        myConn.close();
    }

}

From source file:org.apache.james.blob.objectstorage.AESPayloadCodec.java

@Override
public Payload write(InputStream is) {
    PipedInputStream snk = new PipedInputStream();
    try {/* w  w  w.  ja  v a 2  s  .c  o  m*/
        PipedOutputStream src = new PipedOutputStream(snk);
        OutputStream outputStream = streamingAead.newEncryptingStream(src,
                PBKDF2StreamingAeadFactory.EMPTY_ASSOCIATED_DATA);
        Thread copyThread = new Thread(() -> {
            try (OutputStream stream = outputStream) {
                IOUtils.copy(is, stream);
            } catch (IOException e) {
                throw new RuntimeException("Stream copy failure ", e);
            }
        });
        copyThread.setUncaughtExceptionHandler(
                (Thread t, Throwable e) -> LOGGER.error("Unable to encrypt payload's input stream", e));
        copyThread.start();
        return Payloads.newInputStreamPayload(snk);
    } catch (IOException | GeneralSecurityException e) {
        throw new RuntimeException("Unable to build payload for object storage, failed to " + "encrypt", e);
    }
}

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();//from  w ww  .  ja va  2s. co  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.duracloud.manifest.impl.ManifestGeneratorImpl.java

@Override
public InputStream getManifest(String account, String storeId, String spaceId, ManifestFormat format)
        throws ManifestArgumentException, ManifestNotFoundException {

    log.info("retrieving manifest for account:{}, storeId:{}, spaceId:{}, format:{}", account, storeId, spaceId,
            format);/*from   w ww  .  j a va2  s .  c  o  m*/
    try {

        storeId = validateStoreId(storeId);
        validateSpaceId(storeId, spaceId);
        PipedInputStream is = new PipedInputStream(10 * 1024);
        final PipedOutputStream os = new PipedOutputStream(is);
        final Iterator<ManifestItem> it = this.manifestStore.getItems(account, storeId, spaceId);
        final ManifestFormatter formatter = getFormatter(format);
        if (!it.hasNext()) {
            formatter.writeManifestItemToOutput(null, os);
            os.close();
            return is;
        } else {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (it.hasNext()) {
                            formatter.writeManifestItemToOutput(it.next(), os);
                        }
                        try {
                            os.close();
                        } catch (IOException e) {
                            log.error("failed to close piped output stream : " + e.getMessage(), e);
                        }

                    } catch (Exception e) {
                        log.error("error writing to piped output stream : " + e.getMessage(), e);
                    }
                }
            }).start();
        }

        return is;

    } catch (IOException | RuntimeException ex) {
        log.error("failed to retrieve manifest: " + ex.getMessage(), ex);
        throw new ManifestGeneratorException(ex.getMessage());
    }

}