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:Main.java

public static void main(String[] args) throws Exception {
    PipedInputStream in = new PipedInputStream(20);
    PipedOutputStream out = new PipedOutputStream(in);

    out.close();/* ww w  .j  a  va  2s .c o  m*/
}

From source file:Pipe.java

public static void main(String args[]) throws Exception {
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);

    Sender s = new Sender(out);
    Receiver r = new Receiver(in);
    Thread t1 = new Thread(s);
    Thread t2 = new Thread(r);
    t1.start();/*from ww  w .j ava  2s .c  o m*/
    t2.start();
}

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

/**
 * Create an ETag value from a Jena model.
 *
 * @param m the model that represents the HTTP response body
 * @param lang the serialization language
 * @param base the base URI/*from w w w. j av a 2 s .  c  o  m*/
 * @return an ETag value
 * @throws IOException on I/O errors
 *
 * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19">HTTP 1.1: Section 14.19 - ETag</a>
 */
public static String generate(final Model m, final String lang, final String base) throws IOException {
    final PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    new Thread(new Runnable() {
        public void run() {
            m.write(out, lang, base);
            try {
                out.close();
            } catch (IOException e) {
                logger.error("Error creating MD5 hash of Model", e);
            }
        }
    }).start();

    return generate(in);
}

From source file:custom.SevenZFileExt.java

public InputStream getInputStream(final int bufferSize) {
    final PipedInputStream in = new PipedInputStream(bufferSize);
    try {//w  w w. j  a  v a  2  s .  com
        final PipedOutputStream out = new PipedOutputStream(in);
        Thread thread = new Thread(() -> {
            try {
                byte[] buffer = new byte[bufferSize];
                int len = read(buffer);
                while (len > 0) {
                    try {
                        out.write(buffer, 0, len);
                        len = read(buffer);
                    } catch (Exception ex) {
                        LOGGER.error(ex.getMessage(), ex);
                    }
                }
                out.flush();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception ex) {
                        LOGGER.error(ex.getMessage(), ex);
                    }
                }
            }
        });
        thread.setName("GenerateInputStreamSeven7File");
        thread.start();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return in;
}

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  .  ja 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);/*from  ww  w . j  a  v a2  s  . 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: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: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 ww  .  ja  v  a2 s  .c  om
            }
        }).start();

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

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);//from ww  w.j av a 2  s . co  m
                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);
                }//w  w  w.  j  a va  2s . co m
            }
        }
    }
}