code: public static BufferedReader getBufferedXMLReader(InputStream stream, int xmlLookahead)
              throws IOException {
  
          // create a buffer so we can reset the input stream
          BufferedInputStream input = new BufferedInputStream(stream);
          input.mark(xmlLookahead);
  
          // create object to hold encoding info
          EncodingInfo encoding = new EncodingInfo();
  
          // call this method to set the encoding info
          XmlCharsetDetector.getCharsetAwareReader(input, encoding);
  
          // call this method to create the reader
          Reader reader = XmlCharsetDetector.createReader(input, encoding);
  
          // rest the input
          input.reset();
  
          return getBufferedXMLReader(reader, xmlLookahead);
      }
docs: Wraps an xml input xstream in a buffered reader specifying a lookahead that can be used to
  preparse some of the xml document, resetting it back to its original state for actual
  parsing.
  
  @param stream The original xml stream.
  @param xmlLookahead The number of bytes to support for parse. If more than this number of
  bytes are preparsed the stream can not be properly reset.
  @return The buffered reader.