Example usage for java.io PushbackInputStream mark

List of usage examples for java.io PushbackInputStream mark

Introduction

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

Prototype

public synchronized void mark(int readlimit) 

Source Link

Document

Marks the current position in this input stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    byte[] arrByte = new byte[1024];
    byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    InputStream is = new ByteArrayInputStream(byteArray);
    PushbackInputStream pis = new PushbackInputStream(is);

    try {//from  w w  w.  j a v a2  s  .  c  o m

        for (int i = 0; i < byteArray.length; i++) {

            arrByte[i] = (byte) pis.read();

            System.out.println((char) arrByte[i]);
        }

        pis.mark(5);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:in.arun.faces.fo.pdf.FoOutputStream.java

private PdfResult buildPdf(FoOutputStream foOutput) {
    byte[] responseText = foOutput.getBytes();

    if (responseText == null) {
        return null;
    }/*from  ww  w.j a  va2s .c o m*/

    PdfResult result = new PdfResult();

    try {
        PushbackInputStream pbis = new PushbackInputStream(
                new BufferedInputStream(new ByteArrayInputStream(responseText)));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        //Skip contentType text/html - Looking for bug fix!
        //pbis.skip(9);
        while (pbis.available() > 0) {
            pbis.mark(1);
            if (pbis.read() == '<') {
                pbis.unread('<');
                break;
            }
        }

        //Transforming XML to PDF
        FopFactory fopFactory = FopFactory.newInstance();
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, baos);

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        Source src = new StreamSource(pbis);
        Result res = new SAXResult(fop.getDefaultHandler());

        transformer.transform(src, res);

        result.contentType = MimeConstants.MIME_PDF;
        result.content = baos.toByteArray();
    } catch (IOException | FOPException | TransformerException x) {
        logger.log(Level.SEVERE, "Error while trying to create PDF.", x);

        StringBuilder builder = new StringBuilder();

        builder.append(x.getMessage());
        builder.append("<br/>");

        builder.append("<pre>");
        String formattedFo = new String(responseText);
        formattedFo = formattedFo.replaceAll("<", "&lt;");
        formattedFo = formattedFo.replaceAll(">", "&gt;");
        builder.append(formattedFo);
        builder.append("</pre>");

        result.contentType = "text/html";
        result.content = builder.toString().getBytes();
    }

    return result;
}