Example usage for java.io PushbackInputStream PushbackInputStream

List of usage examples for java.io PushbackInputStream PushbackInputStream

Introduction

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

Prototype

public PushbackInputStream(InputStream in, int size) 

Source Link

Document

Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use.

Usage

From source file:com.netsteadfast.greenstep.util.SimpleUtils.java

/**
 * http://stackoverflow.com/questions/12593752/why-do-i-failed-to-read-excel-2007-using-poi
 * /*from ww w.j  a  v  a 2  s.  c  o  m*/
 * @param inp
 * @return
 * @throws IOException
 * @throws InvalidFormatException
 */
public static Workbook createPOIWorkbook(InputStream inp) throws IOException, InvalidFormatException {
    // If clearly doesn't do mark/reset, wrap up
    if (!inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }
    if (POIFSFileSystem.hasPOIFSHeader(inp)) {
        return new HSSFWorkbook(inp);
    }
    if (DocumentFactoryHelper.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}

From source file:org.osaf.cosmo.cmp.CmpServlet.java

private Document readXmlRequest(HttpServletRequest req) throws SAXException, IOException {
    if (req.getContentLength() == 0) {
        return null;
    }//from w ww .ja va  2s  .  c  o m
    InputStream in = req.getInputStream();
    if (in == null) {
        return null;
    }

    // check to see if there's any data to read
    PushbackInputStream filtered = new PushbackInputStream(in, 1);
    int read = filtered.read();
    if (read == -1) {
        return null;
    }
    filtered.unread(read);

    // there is data, so read the stream
    try {
        BUILDER_FACTORY.setNamespaceAware(true);
        DocumentBuilder docBuilder = BUILDER_FACTORY.newDocumentBuilder();
        return docBuilder.parse(filtered);
    } catch (ParserConfigurationException e) {
        throw new CmpException("error configuring xml builder", e);
    }
}

From source file:org.adl.parsers.dom.ADLDOMParser.java

/**
 * Sets up the file source for the test subject file.
 *
 * @param iFileName file to setup input source for.
 *
 * @return InputSource// www.  j  a  va2  s.  c o m
 */
private InputSource setupFileSource(String iFileName) {
    log.debug("setupFileSource()");
    String msgText;
    boolean defaultEncoding = true;
    String encoding = null;
    PushbackInputStream inputStream;
    FileInputStream inFile;

    try {
        File xmlFile = new File(iFileName);
        log.debug(xmlFile.getAbsolutePath());

        if (xmlFile.isFile()) {
            InputSource is = null;

            defaultEncoding = true;
            if (xmlFile.length() > 1) {
                inFile = new FileInputStream(xmlFile);
                inputStream = new PushbackInputStream(inFile, 4);

                // Reads the initial 4 bytes of the file to check for a Byte
                // Order Mark and determine the encoding

                byte bom[] = new byte[4];
                int n, pushBack;
                n = inputStream.read(bom, 0, bom.length);

                // UTF-8 Encoded
                if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
                    encoding = "UTF-8";
                    defaultEncoding = false;
                    pushBack = n - 3;
                }
                // UTF-16 Big Endian Encoded
                else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
                    encoding = "UTF-16BE";
                    defaultEncoding = false;
                    pushBack = n - 2;
                }
                // UTF-16 Little Endian Encoded               
                else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
                    encoding = "UTF-16LE";
                    defaultEncoding = false;
                    pushBack = n - 2;
                }
                // Default encoding
                else {
                    // Unicode BOM mark not found, unread all bytes                  
                    pushBack = n;
                }

                // Place any non-BOM bytes back into the stream
                if (pushBack > 0) {
                    inputStream.unread(bom, (n - pushBack), pushBack);
                }

                if (defaultEncoding == true) { //Reads in ASCII file.
                    FileReader fr = new FileReader(xmlFile);
                    is = new InputSource(fr);
                }
                // Reads the file in the determined encoding
                else {
                    //Creates a buffer with the size of the xml encoded file
                    BufferedReader inStream = new BufferedReader(new InputStreamReader(inputStream, encoding));
                    StringBuffer dataString = new StringBuffer();
                    String s = "";

                    //Builds the encoded file to be parsed
                    while ((s = inStream.readLine()) != null) {
                        dataString.append(s);
                    }

                    inStream.close();
                    inputStream.close();
                    inFile.close();
                    is = new InputSource(new StringReader(dataString.toString()));
                    is.setEncoding(encoding);
                }
            }
            return is;
        } else if ((iFileName.length() > 6)
                && (iFileName.substring(0, 5).equals("http:") || iFileName.substring(0, 6).equals("https:"))) {
            URL xmlURL = new URL(iFileName);
            InputStream xmlIS = xmlURL.openStream();
            InputSource is = new InputSource(xmlIS);
            return is;
        } else {
            msgText = "XML File: " + iFileName + " is not a file or URL";
            log.error(msgText);
        }
    } catch (NullPointerException npe) {
        msgText = "Null pointer exception" + npe;
        log.error(msgText);
    } catch (SecurityException se) {
        msgText = "Security Exception" + se;
        log.error(msgText);
    } catch (FileNotFoundException fnfe) {
        msgText = "File Not Found Exception" + fnfe;
        log.error(msgText);
    } catch (Exception e) {
        msgText = "General Exception" + e;
        log.error(msgText);
    }

    log.debug("setUpFileSource()");

    return new InputSource();
}