Example usage for java.io PipedWriter PipedWriter

List of usage examples for java.io PipedWriter PipedWriter

Introduction

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

Prototype

public PipedWriter() 

Source Link

Document

Creates a piped writer that is not yet connected to a piped reader.

Usage

From source file:com.daveoxley.cbus.CGateSession.java

private BufferedReader getReader(String id) throws CGateException {
    try {/*from   w w w . ja v a  2 s.com*/
        PipedWriter piped_writer = new PipedWriter();
        BufferedWriter out = new BufferedWriter(piped_writer);
        response_writers.put(id, out);

        PipedReader piped_reader = new PipedReader(piped_writer);
        return new BufferedReader(piped_reader);
    } catch (IOException e) {
        throw new CGateException(e);
    }
}

From source file:com.hp.hpl.jena.grddl.impl.GRDDL.java

private Result n3result() throws IOException {
    pipe = new PipedWriter();
    final PipedReader pr = new PipedReader(pipe);
    Result rslt = new StreamResult(pipe);
    subThread = new Thread() {
        public void run() {
            ((GRDDLReaderBase) reader).n3.read(model, pr, input.retrievalIRI());
        }// w  w w .ja  v  a  2 s  .co m
    };
    subThread.start();
    return rslt;
}

From source file:org.fcrepo.server.access.FedoraAccessServlet.java

public void getObjectProfile(Context context, String PID, Date asOfDateTime, boolean xml,
        HttpServletRequest request, HttpServletResponse response) throws ServerException {

    OutputStreamWriter out = null;
    Date versDateTime = asOfDateTime;
    ObjectProfile objProfile = null;//www .ja v  a2  s .c o m
    PipedWriter pw = null;
    PipedReader pr = null;
    try {
        pw = new PipedWriter();
        pr = new PipedReader(pw);
        objProfile = m_access.getObjectProfile(context, PID, asOfDateTime);
        if (objProfile != null) {
            // Object Profile found.
            // Serialize the ObjectProfile object into XML
            new ProfileSerializerThread(context, PID, objProfile, versDateTime, pw).start();
            if (xml) {
                // Return results as raw XML
                response.setContentType(CONTENT_TYPE_XML);

                // Insures stream read from PipedReader correctly translates
                // utf-8
                // encoded characters to OutputStreamWriter.
                out = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
                char[] buf = new char[BUF];
                int len = 0;
                while ((len = pr.read(buf, 0, BUF)) != -1) {
                    out.write(buf, 0, len);
                }
                out.flush();
            } else {
                // Transform results into an html table
                response.setContentType(CONTENT_TYPE_HTML);
                out = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
                File xslFile = new File(m_server.getHomeDir(), "access/viewObjectProfile.xslt");
                Templates template = XmlTransformUtility.getTemplates(xslFile);
                Transformer transformer = template.newTransformer();
                transformer.setParameter("fedora", context.getEnvironmentValue(FEDORA_APP_CONTEXT_NAME));
                transformer.transform(new StreamSource(pr), new StreamResult(out));
            }
            out.flush();

        } else {
            throw new GeneralException("No object profile returned");
        }
    } catch (ServerException e) {
        throw e;
    } catch (Throwable th) {
        String message = "Error getting object profile";
        logger.error(message, th);
        throw new GeneralException(message, th);
    } finally {
        try {
            if (pr != null) {
                pr.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (Throwable th) {
            String message = "Error closing output";
            logger.error(message, th);
            throw new StreamIOException(message);
        }
    }
}

From source file:org.jboss.rusheye.result.statistics.TestOverallStatistics.java

@Test
public void testOverallStatistics() throws IOException, InterruptedException, BrokenBarrierException {
    List<String> list = new LinkedList<String>();
    PipedWriter pipedWriter = new PipedWriter();
    CyclicBarrier barrier = new CyclicBarrier(2);

    new Thread(new StreamToListWrapper(pipedWriter, list, barrier)).start();

    when(properties.getProperty("overall-statistics-output")).thenReturn(pipedWriter);
    when(test.getName()).thenReturn("testName");
    when(test.getPatterns()).thenReturn(Arrays.asList(pattern));
    when(pattern.getName()).thenReturn("patternName");
    when(pattern.getConclusion()).thenReturn(ResultConclusion.PERCEPTUALLY_SAME);
    when(pattern.getOutput()).thenReturn("someLocation");

    overallStatistics.setProperties(properties);

    overallStatistics.onPatternCompleted(pattern);

    overallStatistics.onTestCompleted(test);
    barrier.await();//from   w w  w  . j av a 2  s .  c  om
    Assert.assertTrue(list.contains("[ PERCEPTUALLY_SAME ] testName"));

    overallStatistics.onSuiteCompleted();
    barrier.await();
    Assert.assertTrue(list.contains("  Overall Statistics:"));
    Assert.assertTrue(list.contains("  PERCEPTUALLY_SAME: 1"));
}

From source file:org.lockss.util.TestStreamUtil.java

public void testReadCharShortRead() throws Exception {
    char[] snd1 = { '0', '1', 0, '3' };
    final int len = 12;
    final char[] buf = new char[len];
    PipedWriter outs = new PipedWriter();
    final Reader ins = new PipedReader(outs);
    final Exception[] ex = { null };
    final int[] res = { 0 };
    Thread th = new Thread() {
        public void run() {
            try {
                res[0] = StreamUtil.readChars(ins, buf, len);
                StreamUtil.readChars(ins, buf, len);
            } catch (IOException e) {
                ex[0] = e;/*from w w  w  . j  a  v a 2  s . c om*/
            }
        }
    };
    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 testReadCharMultipleRead() throws Exception {
    char[] snd1 = { '0', '1', 0, '3' };
    char[] snd2 = { '4', '5', '6', '7', '8', '9', 'a', 'b' };
    char[] exp = { '0', '1', 0, '3', '4', '5', '6', '7', '8', '9', 'a', 'b' };
    final int len = exp.length;
    final char[] buf = new char[len];
    PipedWriter outs = new PipedWriter();
    final Reader ins = new PipedReader(outs);
    final Exception[] ex = { null };
    final int[] res = { 0 };
    Thread th = new Thread() {
        public void run() {
            try {
                res[0] = StreamUtil.readChars(ins, buf, len);
            } catch (IOException e) {
                ex[0] = e;//from   w w  w . j ava2s  . 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:sadl.utils.IoUtils.java

public static Pair<TimedInput, TimedInput> readTrainTestFile(Path trainTestFile,
        Function<Reader, TimedInput> f) {
    try (BufferedReader br = Files.newBufferedReader(trainTestFile);
            PipedWriter trainWriter = new PipedWriter();
            PipedReader trainReader = new PipedReader(trainWriter);
            PipedWriter testWriter = new PipedWriter();
            PipedReader testReader = new PipedReader(testWriter)) {
        String line = "";
        final ExecutorService ex = Executors.newFixedThreadPool(2);
        final Future<TimedInput> trainWorker = ex.submit(() -> f.apply(trainReader));
        final Future<TimedInput> testWorker = ex.submit(() -> f.apply(testReader));
        ex.shutdown();//from   www .  j  a  va2  s.co m
        boolean writeTrain = true;
        while ((line = br.readLine()) != null) {
            if (line.startsWith(SmacDataGenerator.TRAIN_TEST_SEP)) {
                writeTrain = false;
                trainWriter.close();
                continue;
            }
            if (writeTrain) {
                trainWriter.write(line);
                trainWriter.write('\n');
            } else {
                testWriter.write(line);
                testWriter.write('\n');
            }
        }
        testWriter.close();
        ex.shutdown();
        if (writeTrain) {
            trainWriter.close();
            ex.shutdownNow();
            throw new IOException("The provided file " + trainTestFile + " does not contain the separator "
                    + SmacDataGenerator.TRAIN_TEST_SEP);
        }
        final Pair<TimedInput, TimedInput> result = Pair.of(trainWorker.get(), testWorker.get());
        return result;
    } catch (final IOException | InterruptedException | ExecutionException e) {
        logger.error("Unexpected exception!", e);
    }
    return null;
}