Example usage for java.io PipedInputStream PipedInputStream

List of usage examples for java.io PipedInputStream PipedInputStream

Introduction

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

Prototype

public PipedInputStream() 

Source Link

Document

Creates a PipedInputStream so that it is not yet #connect(java.io.PipedOutputStream) connected .

Usage

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

@Override
public InputStream serialize(InputStream rawContent, DataSetMetadata metadata, long limit) {
    try {//  ww  w . j a va 2 s.  c  o  m
        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: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: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 w  ww  .j  av a2 s . com

    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   w  w  w .  j  ava2 s. c  om*/
    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 ww .j  a v a  2  s  .c om*/
        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:edu.lternet.pasta.dml.download.DocumentHandler.java

public DocumentHandler() {
    //initialize the streams for reading the document from server
    outputStream = new PipedOutputStream();
    inputStream = new PipedInputStream();

    try {/* w ww  . j  a  v a 2s  .c  om*/
        outputStream.connect(inputStream);
    } catch (IOException e1) {
        log.error("could not connect piped streams! " + e1.getMessage());
        e1.printStackTrace();
    }

}

From source file:mx.ipn.escom.supernaut.nile.logic.CommonBean.java

protected InputStream streamedMarshall() throws IOException {
    PipedInputStream in = new PipedInputStream();
    OutputStream out = new PipedOutputStream(in);
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
    gson.toJson(model, modelType, writer);
    return in;/*from w  w w  .  j a v a 2  s.  c om*/
}

From source file:edu.lternet.pasta.dml.util.DocumentDownloadUtil.java

private void init() {
    //initialize the streams for reading the document from server
    outputStream = new PipedOutputStream();
    inputStream = new PipedInputStream();

    try {/*  w w  w .j a  va2  s. c om*/
        outputStream.connect(inputStream);
    } catch (IOException e1) {
        log.error("could not connect piped streams! " + e1.getMessage());
        e1.printStackTrace();
    }
}

From source file:org.apache.axis2.transport.nhttp.util.PipeImpl.java

public PipeImpl() throws IOException {
    if (useNative) {
        Pipe pipe = Pipe.open();
        source = pipe.source();/*w w  w.  ja  va2 s  .  c  om*/
        sink = pipe.sink();

    } else {
        PipedInputStream pipedIn = new PipedInputStream();
        try {
            pipedOut = new PipedOutputStream(pipedIn);
        } catch (IOException e) {
            e.printStackTrace();
        }

        source = Channels.newChannel(pipedIn);
        sink = Channels.newChannel(pipedOut);
    }
}

From source file:ro.kuberam.libs.java.crypto.CryptoModuleTests.java

@Ignore
@Test/*w w  w.j  av a  2  s .  c  om*/
public void pipedStreams2Test() throws Exception {
    try (final InputStream document = getClass().getResourceAsStream("../doc-1.xml");
            final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        int next = document.read();
        while (next > -1) {
            bos.write(next);
            next = document.read();
        }
        bos.flush();
        final byte[] result = bos.toByteArray();

        try (final PipedOutputStream poStream = new PipedOutputStream();
                final PipedInputStream piStream = new PipedInputStream()) {

            // piped input stream connect to the piped output stream
            piStream.connect(poStream);

            // Writes specified byte array.
            poStream.write(result);

            // Reads the next byte of data from this piped input stream.
            for (int i = 0; i < result.length; i++) {
                System.out.println(piStream.read());
            }
        }
    }
}