Example usage for java.io PushbackInputStream read

List of usage examples for java.io PushbackInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:org.deegree.framework.xml.XMLFragment.java

/**
 * reads the encoding of a XML document from its header. If no header available
 * <code>CharsetUtils.getSystemCharset()</code> will be returned
 * /*  w w w .  j a  v  a  2s.  com*/
 * @param pbis
 * @return encoding of a XML document
 * @throws IOException
 */
private String readEncoding(PushbackInputStream pbis) throws IOException {
    byte[] b = new byte[80];
    String s = "";
    int rd = 0;

    LinkedList<byte[]> bs = new LinkedList<byte[]>();
    LinkedList<Integer> rds = new LinkedList<Integer>();
    while (rd < 80) {
        rds.addFirst(pbis.read(b));
        if (rds.peek() == -1) {
            rds.poll();
            break;
        }
        rd += rds.peek();
        s += new String(b, 0, rds.peek()).toLowerCase();
        bs.addFirst(b);
        b = new byte[80];
    }

    String encoding = CharsetUtils.getSystemCharset();
    if (s.indexOf("?>") > -1) {
        int p = s.indexOf("encoding=");
        if (p > -1) {
            StringBuffer sb = new StringBuffer();
            int k = p + 1 + "encoding=".length();
            while (s.charAt(k) != '"' && s.charAt(k) != '\'') {
                sb.append(s.charAt(k++));
            }
            encoding = sb.toString();
        }
    }
    while (!bs.isEmpty()) {
        pbis.unread(bs.poll(), 0, rds.poll());
    }

    return encoding;
}

From source file:org.methodize.nntprss.feed.Channel.java

private static void skipBOM(PushbackInputStream is) throws IOException {
    byte[] header = new byte[PUSHBACK_BUFFER_SIZE];
    int bytesRead = is.read(header);
    if (header[0] == 0 && header[1] == 0 && (header[2] & 0xff) == 0xFE && (header[3] & 0xff) == 0xFF) {
        // UTF-32, big-endian
    } else if ((header[0] & 0xff) == 0xFF && (header[1] & 0xff) == 0xFE && header[2] == 0 && header[3] == 0) {
        // UTF-32, little-endian
    } else if ((header[0] & 0xff) == 0xFE && (header[1] & 0xff) == 0xFF) {
        is.unread(header, 2, 2);/*ww  w  .  j ava2  s .  c o m*/
        // UTF-16, big-endian
    } else if ((header[0] & 0xff) == 0xFF && (header[1] & 0xff) == 0xFE) {
        is.unread(header, 2, 2);
        // UTF-16, little-endian
    } else if ((header[0] & 0xff) == 0xEF && (header[1] & 0xff) == 0xBB && (header[2] & 0xff) == 0xBF) {
        // UTF-8
        is.unread(header, 3, 1);
    } else {
        is.unread(header, 0, PUSHBACK_BUFFER_SIZE);
    }
}

From source file:org.openhealthtools.openatna.syslog.GenericMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {/*from ww w.j  a v a  2s  .  c  om*/
        PushbackInputStream pin = new PushbackInputStream(in, 7);
        byte[] bytes = new byte[7];
        pin.read(bytes);

        boolean bsd = isBSD(bytes);
        pin.unread(bytes);
        if (bsd) {
            log.debug("message is BSD style");
            return bsdFactory.read(pin);
        } else {
            log.debug("message RFC 5424");
            return protFactory.read(pin);
        }
    } catch (IOException e) {
        throw new SyslogException(e);
    }
}

From source file:org.springframework.ws.soap.saaj.SaajSoapMessageFactory.java

/**
 * Checks for the UTF-8 Byte Order Mark, and removes it if present. The SAAJ RI cannot cope with these BOMs.
 *
 * @see <a href="http://jira.springframework.org/browse/SWS-393">SWS-393</a>
 * @see <a href="http://unicode.org/faq/utf_bom.html#22">UTF-8 BOMs</a>
 *///from w ww  . j ava2 s . co m
private InputStream checkForUtf8ByteOrderMark(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bytes = new byte[3];
    int bytesRead = pushbackInputStream.read(bytes);
    if (bytesRead != -1) {
        // check for the UTF-8 BOM, and remove it if there. See SWS-393
        if (!isByteOrderMark(bytes)) {
            pushbackInputStream.unread(bytes, 0, bytesRead);
        }
    }
    return pushbackInputStream;
}