Example usage for java.io PipedReader PipedReader

List of usage examples for java.io PipedReader PipedReader

Introduction

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

Prototype

public PipedReader(int pipeSize) 

Source Link

Document

Creates a PipedReader so that it is not yet #connect(java.io.PipedWriter) connected and uses the specified pipe size for the pipe's buffer.

Usage

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());
        }/*from  w  w w  . ja v  a 2s .c  om*/
    };
    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;//from   w w  w  . j  a  v a  2  s . co 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.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  ww  w  .j ava  2s  . 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 ww  w .j a va 2 s  . 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 w  w w . j  a  v a  2 s .  c o  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;
}