Example usage for java.io PipedReader close

List of usage examples for java.io PipedReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this piped stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void main(String[] args) {

    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader();

    try {/* w  w  w.java 2 s.c  o  m*/
        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // read what we wrote
        for (int i = 0; i < 2; i++) {
            System.out.println((char) reader.read());
        }

        reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {

    try {//w  w  w  . j  a  va 2 s. c o  m
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader(writer);

        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // check if reader is ready to read
        System.out.println(reader.ready());

        // print the char array
        for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
        }
        reader.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    try {//from  ww w. ja va 2  s. com
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader(writer, 100);

        // connect the reader and the writer
        reader.connect(writer);

        writer.write(70);
        writer.write(71);

        // check if reader is ready to read
        System.out.println(reader.ready());

        // print the char array
        for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) reader.read());
        }
        reader.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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;//w  ww .  ja  va 2 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);
        }
    }
}