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.wso2.carbon.mediation.connector.message.util.AS4Utils.java

/**
 * Create {@link OMNode} object from {@link Messaging} object
 * @param marshaller {@link Marshaller} instance for {@link Messaging} class
 * @param messaging {@link Messaging} object to be converted to {@link OMNode}
 * @return OMNode object created from {@link Messaging} object
 * @throws IOException/*  www . j  av a  2  s .co m*/
 * @throws XMLStreamException
 * @throws PropertyException
 */
public static OMNode getOMNode(final Marshaller marshaller, final Messaging messaging)
        throws IOException, XMLStreamException, PropertyException {

    final PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream();
    in.connect(out);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    new Thread(new Runnable() {
        public void run() {
            try {
                // write the original OutputStream to the PipedOutputStream
                marshaller.marshal(messaging, out);
            } catch (JAXBException e) {
                log.error(e);
            }
        }
    }).start();

    //Create a new builder with the StAX reader
    StAXOMBuilder builder = new StAXOMBuilder(in);
    OMNode node = builder.getDocumentElement();
    node.close(true);
    return node;
}

From source file:org.eclipse.lyo.oslc.v3.sample.ETag.java

public static String generate(final JsonObject object) throws IOException {
    final PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    new Thread(new Runnable() {
        public void run() {
            object.output(new IndentedWriter(out));
            try {
                out.close();/*from   w w  w  .j  a v a 2  s .  c  o  m*/
            } catch (IOException e) {
                logger.error("Error creating MD5 hash of JSON", e);
            }
        }
    }).start();

    return generate(in);
}

From source file:mtsar.csv.TaskCSVTest.java

@Test
public void testCSV() throws IOException {
    try (final PipedInputStream pis = new PipedInputStream()) {
        try (final PipedOutputStream pos = new PipedOutputStream(pis)) {
            TaskCSV.write(tasks, pos);//  w  w w.j  av a  2s.c o m

            try (final Reader reader = new InputStreamReader(pis, StandardCharsets.UTF_8)) {
                try (final CSVParser csv = new CSVParser(reader, TaskCSV.FORMAT)) {
                    final List<Task> parsed = Lists.newArrayList(TaskCSV.parse(stage, csv));
                    assertThat(parsed).hasSize(2);
                    assertThat(parsed).usingElementComparatorIgnoringFields("dateTime").isEqualTo(tasks);
                }
            }
        }
    }
}

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

@Test
public void pipedStreams1Test() throws Exception {
    final String message = "String for tests.";

    try (final PipedInputStream in = new PipedInputStream();
            final PipedOutputStream outp = new PipedOutputStream(in)) {
        new Thread(() -> {
            try {
                outp.write(message.getBytes(StandardCharsets.UTF_8));
            } catch (final IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//from  w w  w  .  j ava2s  .  com
            }
        }).start();

        System.out.println("result: " + in);
    }
}

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

@Test
public void testConstructor() throws Exception {
    PipedInputStream refIs1 = new PipedInputStream();
    int refChannel1 = 0x13;

    LiveInputStream testIs = new LiveInputStream(refIs1, refChannel1);

    assertEquals(refIs1, testIs.is);//from   ww w  .  ja v  a 2s. c  om
    assertEquals(refChannel1, testIs.channel);
}

From source file:com.exorath.plugin.map.impl.MapServiceMapUploadProvider.java

@Override
public String upload(final File worldDir, String mapId, String envId) {
    //This is a piped stream solution: see http://blog.ostermiller.org/convert-java-outputstream-inputstream for more info
    try (PipedInputStream in = new PipedInputStream()) {
        try (PipedOutputStream out = new PipedOutputStream(in)) {

            new Thread(() -> {
                zip(worldDir, out);//  w  w w .  j  a  va2  s.com
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

            HttpRequestWithBody req = Unirest.post(address + "/accounts/{accountId}/maps/{mapId}/env/{envId}")
                    .routeParam("accountId", accountId).routeParam("mapId", mapId).routeParam("envId", envId);
            req.field("file", in, ContentType.MULTIPART_FORM_DATA, "file");
            HttpResponse<String> res = req.asString();
            String body = res.getBody();
            if (res.getStatus() < 200 || res.getStatus() >= 300 || body == null)
                return null;
            JsonObject jsonObject = GSON.fromJson(body, JsonObject.class);
            if (jsonObject == null || !jsonObject.has("success")
                    || jsonObject.get("success").getAsBoolean() == false || !jsonObject.has("versionId"))
                return null;
            return jsonObject.get("versionId").getAsString();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:mtsar.csv.WorkerCSVTest.java

@Test
public void testCSV() throws IOException {
    try (final PipedInputStream pis = new PipedInputStream()) {
        try (final PipedOutputStream pos = new PipedOutputStream(pis)) {
            WorkerCSV.write(workers, pos);

            try (final Reader reader = new InputStreamReader(pis, StandardCharsets.UTF_8)) {
                try (final CSVParser csv = new CSVParser(reader, WorkerCSV.FORMAT)) {
                    final List<Worker> parsed = Lists.newArrayList(WorkerCSV.parse(stage, csv));
                    assertThat(parsed).hasSize(2);
                    assertThat(parsed).usingElementComparatorIgnoringFields("dateTime").isEqualTo(workers);
                }//from   w  w  w  .  j av  a2s. c  o  m
            }
        }
    }
}

From source file:org.mule.module.pgp.LazyTransformedInputStream.java

public LazyTransformedInputStream(TransformPolicy transformPolicy, StreamTransformer transformer)
        throws IOException {
    Validate.notNull(transformPolicy, "The transformPolicy should not be null");
    Validate.notNull(transformer, "The transformer should not be null");

    this.in = new PipedInputStream();
    this.out = new PipedOutputStream(this.in);
    this.transformPolicy = transformPolicy;
    this.transformer = transformer;
    this.transformPolicy.initialize(this);
}

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);// w w  w. j  a v  a2 s  .c  o  m
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
    return in;
}

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/* w w  w . jav  a  2  s  .c om*/
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                Sleep.softly(latch::await);
            }
        }
    };
    pipeOut = new PipedOutputStream(pipeIn);

    new Thread(this).start();
}