Example usage for java.io PushbackInputStream available

List of usage examples for java.io PushbackInputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for 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' };

    try {//  w  ww.  j  a v  a2 s .  c o  m
        InputStream is = new ByteArrayInputStream(byteArray);
        PushbackInputStream pis = new PushbackInputStream(is);

        System.out.println(pis.available());

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

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

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

    } 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;
    }//w w  w. j  av a  2  s.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;
}

From source file:org.apache.james.mailbox.maildir.mail.model.MaildirMessage.java

/**
 * Return the position in the given {@link InputStream} at which the Body of
 * the MailboxMessage starts/*from   w  w w.  j av  a2s.c om*/
 */
private int bodyStartOctet(InputStream msgIn) throws IOException {
    // we need to pushback maximal 3 bytes
    PushbackInputStream in = new PushbackInputStream(msgIn, 3);
    int localBodyStartOctet = in.available();
    int i;
    int count = 0;
    while ((i = in.read()) != -1 && in.available() > 4) {
        if (i == 0x0D) {
            int a = in.read();
            if (a == 0x0A) {
                int b = in.read();

                if (b == 0x0D) {
                    int c = in.read();

                    if (c == 0x0A) {
                        localBodyStartOctet = count + 4;
                        break;
                    }
                    in.unread(c);
                }
                in.unread(b);
            }
            in.unread(a);
        }
        count++;
    }
    return localBodyStartOctet;
}

From source file:org.commoncrawl.hadoop.io.deprecated.ArcFileReader.java

/**
 * calculated raw arc file stream pos (taking into account any buffered data
 * contained within PushBackInputStream/*w  ww  .j av a  2 s . c o  m*/
 * 
 * @return current stream position in bytes
 * @throws IOException
 *           if error occurs
 */
private final int getARCFileStreamPos() throws IOException {
    PushbackInputStream in = (PushbackInputStream) this.in;
    return _streamPos - in.available();
}