Read big XML file from specific stream

@param is
stream of xml like fileinputstream
@param encoding
@param listener
listener callbacks when each xml element starts and ends
6337
public void read(InputStream is, String encoding, final TagListener listener) {

		mSaxHandler.initialize();

		Reader reader = null;

		try {

			reader = new InputStreamReader(is, encoding);

			SAXParserFactory spf = SAXParserFactory.newInstance();

			// To correspond to a big entities
			spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);

			XMLReader xmlReader;

			xmlReader = spf.newSAXParser().getXMLReader();

			mSaxHandler.setTagEventListener(new TagEventListener() {

				@Override
				public void onTagStarted(String fullTagName, String tagName) {
					if (listener != null) {
						listener.onTagStarted(BigXmlReader.this, fullTagName, tagName);
					}
				}

				@Override
				public void onTagFinished(String fullTagName, String tagName, String value, Attributes atts) {
					if (listener != null) {
						listener.onTagFinished(BigXmlReader.this, fullTagName, tagName, value, atts);
					}
				}
			});

			xmlReader.setContentHandler(mSaxHandler);
			InputSource iso = new InputSource(reader);

			xmlReader.parse(iso);

		} catch (BasicSAXHandlerException e) {
			// Force stopped
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
165498
function readStream (stream, encoding, length, limit, callback) {
  var complete = false
  var sync = true

  // check the length and limit options.
  // note: we intentionally leave the stream paused,
  // so users should handle the stream themselves.
  if (limit !== null && length !== null && length > limit) {
    return done(createError(413, 'request entity too large', {
      expected: length,
      length: length,
      limit: limit,
      type: 'entity.too.large'
    }))
  }

  // streams1: assert request encoding is buffer.
  // streams2+: assert the stream encoding is buffer.
  //   stream._decoder: streams1
  //   state.encoding: streams2
  //   state.decoder: streams2, specifically < 0.10.6
  var state = stream._readableState
  if (stream._decoder || (state && (state.encoding || state.decoder))) {
    // developer error
    return done(createError(500, 'stream encoding should not be set', {
      type: 'stream.encoding.set'
    }))
  }

  var received = 0
  var decoder

  try {
    decoder = getDecoder(encoding)
  } catch (err) {
    return done(err)
  }

  var buffer = decoder
    ? ''
    : []

  // attach listeners
  stream.on('aborted', onAborted)
  stream.on('close', cleanup)
  stream.on('data', onData)
  stream.on('end', onEnd)
  stream.on('error', onEnd)

  // mark sync section complete
  sync = false

  function done () {
    var args = new Array(arguments.length)

    // copy arguments
    for (var i = 0; i < args.length; i++) {
      args[i] = arguments[i]
    }

    // mark complete
    complete = true

    if (sync) {
      process.nextTick(invokeCallback)
    } else {
      invokeCallback()
    }

    function invokeCallback () {
      cleanup()

      if (args[0]) {
        // halt the stream on error
        halt(stream)
      }

      callback.apply(null, args)
    }
  }

  function onAborted () {
    if (complete) return

    done(createError(400, 'request aborted', {
      code: 'ECONNABORTED',
      expected: length,
      length: length,
      received: received,
      type: 'request.aborted'
    }))
  }

  function onData (chunk) {
    if (complete) return

    received += chunk.length

    if (limit !== null && received > limit) {
      done(createError(413, 'request entity too large', {
        limit: limit,
        received: received,
        type: 'entity.too.large'
      }))
    } else if (decoder) {
      buffer += decoder.write(chunk)
    } else {
      buffer.push(chunk)
    }
  }

  function onEnd (err) {
    if (complete) return
    if (err) return done(err)

    if (length !== null && received !== length) {
      done(createError(400, 'request size did not match content length', {
        expected: length,
        length: length,
        received: received,
        type: 'request.size.invalid'
      }))
    } else {
      var string = decoder
        ? buffer + (decoder.end() || '')
        : Buffer.concat(buffer)
      done(null, string)
    }
  }

  function cleanup () {
    buffer = null

    stream.removeListener('aborted', onAborted)
    stream.removeListener('data', onData)
    stream.removeListener('end', onEnd)
    stream.removeListener('error', onEnd)
    stream.removeListener('close', cleanup)
  }
}
415096
private void init(InputStream inputStream) {
        this.inputStream = inputStream;
        currentPath = new LinkedList<Node>();
        nodeQueue = new LinkedList<XmlNode>();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty("javax.xml.stream.isCoalescing", true);
        factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true);
        try {
            reader = factory.createXMLStreamReader(inputStream);
        } catch (XMLStreamException e) {
            throw new XmlReaderException(e);
        }
    }
812622
public static String applyFromInputStream(InputStream is) throws ConverterException {		
		String xml = converters.get(0).convertFromInputStream(is, false);						
		xml = applyFromSecond(xml);
		if (conversionDone()) {
			return converters.get(converters.size()-1).changeVersion(xml);
		} else {
			return xml;
		}
	}
702943
@Override
    public int readElementAsIntArray(int[] value, int from, int length) throws XMLStreamException
    {
        return readElementAsArray(_decoderFactory().getIntArrayDecoder(value, from, length));
    }
684130
static String getXmlProlog(final InputStream is, final String guessedEnc) throws IOException {
        String encoding = null;
        if (guessedEnc != null) {
            final byte[] bytes = new byte[BUFFER_SIZE];
            is.mark(BUFFER_SIZE);
            int offset = 0;
            int max = BUFFER_SIZE;
            int c = is.read(bytes, offset, max);
            int firstGT = -1;
            while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) {
                offset += c;
                max -= c;
                c = is.read(bytes, offset, max);
                firstGT = new String(bytes, 0, offset, guessedEnc).indexOf(">");
            }
            if (firstGT == -1) {
                if (c == -1) {
                    throw new IOException("Unexpected end of XML stream");
                } else {
                    throw new IOException("XML prolog or ROOT element not found on first " + offset + " bytes");
                }
            }
            final int bytesRead = offset;
            if (bytesRead > 0) {
                is.reset();
                String prolog = new String(bytes, guessedEnc).substring(0, firstGT);
                final Matcher m = ENCODING_PATTERN.matcher(prolog);
                if (m.find()) {
                    encoding = m.group(1).toUpperCase(Locale.ENGLISH);
                    encoding = encoding.substring(1, encoding.length() - 1);
                }
            }
        }
        return encoding;
    }
596967
public static void parse(SimpleXMLDocHandler doc, InputStream in) throws IOException {
        byte b4[] = new byte[4];
        int count = in.read(b4);
        if (count != 4)
            throw new IOException("Insufficient length.");
        String encoding = getEncodingName(b4);
        String decl = null;
        if (encoding.equals("UTF-8")) {
            StringBuffer sb = new StringBuffer();
            int c;
            while ((c = in.read()) != -1) {
                if (c == '>')
                    break;
                sb.append((char)c);
            }
            decl = sb.toString();
        }
        else if (encoding.equals("CP037")) {
            ByteArrayOutputStream bi = new ByteArrayOutputStream();
            int c;
            while ((c = in.read()) != -1) {
                if (c == 0x6e) // that's '>' in ebcdic
                    break;
                bi.write(c);
            }
            decl = new String(bi.toByteArray(), "CP037");
        }
        if (decl != null) {
            decl = getDeclaredEncoding(decl);
            if (decl != null)
                encoding = decl;
        }
        parse(doc, new InputStreamReader(in, IanaEncodings.getJavaEncoding(encoding)));
    }
853157
public void encode(AsnOutputStream aos) throws EncodeException {

        if (type == null)
            throw new EncodeException("Error encoding AbortSource: No type set");

        try {
            aos.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG, type.getType());

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding AbortSource: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding AbortSource: " + e.getMessage(), e);
        }
    }
981673
public void parseEvents(MarcXchangeEventConsumer consumer) throws XMLStreamException {
        Objects.requireNonNull(consumer);
        if (builder.getMarcListeners() != null) {
            for (Map.Entry<String, MarcListener> entry : builder.getMarcListeners().entrySet()) {
                consumer.setMarcListener(entry.getKey(), entry.getValue());
            }
        }
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(builder.getInputStream());
        while (xmlEventReader.hasNext()) {
            consumer.add(xmlEventReader.nextEvent());
        }
        xmlEventReader.close();
    }
357899
public void go(DocListener document, InputSource is, String tagmap) {
        try {
            parser.parse(is, new SAXmyHandler(document, new TagMap(tagmap)));
        }
        catch(SAXException se) {
            throw new ExceptionConverter(se);
        }
        catch(IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
    }
1257982
private void read(InputStream is, Table table) throws IOException
   {
      byte[] headerBytes = new byte[6];
      is.read(headerBytes);

      byte[] recordCountBytes = new byte[2];
      is.read(recordCountBytes);
      //int recordCount = getShort(recordCountBytes, 0);
      //System.out.println("Header: " + new String(headerBytes) + " Record count:" + recordCount);

      byte[] buffer = new byte[m_definition.getRecordSize()];
      while (true)
      {
         int bytesRead = is.read(buffer);
         if (bytesRead == -1)
         {
            break;
         }

         if (bytesRead != buffer.length)
         {
            throw new IOException("Unexpected end of file");
         }

         if (buffer[0] == 0)
         {
            readRecord(buffer, table);
         }
      }
   }
1070117
public void writeEndElement() throws XMLStreamException {
		try {
			this.checkPendingATEvents();
			encoder.encodeEndElement();
		} catch (Exception e) {
			throw new XMLStreamException(e.getLocalizedMessage(), e);
		}
	}
966374
@Override
	public void readFrom(InputStream is) throws IOException {
		this.currentItem = 0;
		// read from source and write into tempfile
		// open temp file depending on options compressed/encrypted
		OutputStream tempOS = null;
		if ((this.ca != null) || (this.compressTempFile)) {
			tempOS = new FileOutputStream(this.tempFile);
			if (this.ca != null) { // encrypt file if configured
				tempOS = new CipherOutputStream(tempOS, this.ciEncrypt);
			}
			if (this.compressTempFile) { // compress file if configured
				tempOS = new GZIPOutputStream(tempOS, EncryptedCachedDiskStringsTable.compressBufferSize);
			}
		} else { // not encrypted and not compressed: configure a random access file for
					// writing/reading = highest performance
			this.tempRAF = new RandomAccessFile(this.tempFile, "rw");
		}
		// read from source
		// use Stax event reader
		XMLEventReader xer = null;
		try {
			xer = StaxHelper.newXMLInputFactory().createXMLEventReader(this.originalIS);
			while (xer.hasNext()) {
				XMLEvent xe = xer.nextEvent();
				// check if it is a string item (entry in string table)
				if (xe.isStartElement() && xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("si")) {
					String siText = this.parseSIText(xer);
					// add string to temp file
					this.addString(siText, tempOS);
					this.count++;
				}
			}
			// close tempfile
			// make tempFile available as a reader
		} catch (XMLStreamException e) {
			LOG.error("Cannot read original SharedStringTable from document. Exception " + e);
			throw new IOException(e);
		} catch (FormatNotUnderstoodException e) {
			LOG.error("Cannot read properly SharedStringTable from document. Exception " + e);
			throw new IOException(e);
		} finally {
			// close temporary Stream (tempfile should be deleted using the close method of
			// this class and not here)
			if (tempOS != null) {
				tempOS.close();
			}
		}
		// open the input stream towards the temp file
		this.accessTempFile(0L);

	}
384288
public static String getStreamContents(InputStream is) throws IOException {
        Preconditions.checkNotNull(is, "Cannot get String from a null object");
        final char[] buffer = new char[0x10000];
        final StringBuilder out = new StringBuilder();
        try (Reader in = new InputStreamReader(is, "UTF-8")) {
            int read;
            do {
                read = in.read(buffer, 0, buffer.length);
                if (read > 0) {
                    out.append(buffer, 0, read);
                }
            } while (read >= 0);
        }
        return out.toString();
    }
697630
@Override
    public void record( final Context context,
                        final InputStream inputStream,
                        final long length,
                        final String encoding,
                        final Node outputNode ) throws Exception {
            JavaMetadata javaMetadata = JavaMetadata.instance(inputStream, length, encoding);
            record(context, outputNode, javaMetadata);
    }
836156
public void read(InputStream is) throws IOException {
        // Buffered input stream for reading manifest data
        FastInputStream fis = new FastInputStream(is);
        // Line buffer
        byte[] lbuf = new byte[512];
        // Read the main attributes for the manifest
        attr.read(fis, lbuf);
        // Total number of entries, attributes read
        int ecount = 0, acount = 0;
        // Average size of entry attributes
        int asize = 2;
        // Now parse the manifest entries
        int len;
        String name = null;
        boolean skipEmptyLines = true;
        byte[] lastline = null;

        while ((len = fis.readLine(lbuf)) != -1) {
            if (lbuf[--len] != '\n') {
                throw new IOException("manifest line too long");
            }
            if (len > 0 && lbuf[len-1] == '\r') {
                --len;
            }
            if (len == 0 && skipEmptyLines) {
                continue;
            }
            skipEmptyLines = false;

            if (name == null) {
                name = parseName(lbuf, len);
                if (name == null) {
                    throw new IOException("invalid manifest format");
                }
                if (fis.peek() == ' ') {
                    // name is wrapped
                    lastline = new byte[len - 6];
                    System.arraycopy(lbuf, 6, lastline, 0, len - 6);
                    continue;
                }
            } else {
                // continuation line
                byte[] buf = new byte[lastline.length + len - 1];
                System.arraycopy(lastline, 0, buf, 0, lastline.length);
                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
                if (fis.peek() == ' ') {
                    // name is wrapped
                    lastline = buf;
                    continue;
                }
                name = new String(buf, 0, buf.length, "UTF8");
                lastline = null;
            }
            Attributes attr = getAttributes(name);
            if (attr == null) {
                attr = new Attributes(asize);
                entries.put(name, attr);
            }
            attr.read(fis, lbuf);
            ecount++;
            acount += attr.size();
            //XXX: Fix for when the average is 0. When it is 0,
            // you get an Attributes object with an initial
            // capacity of 0, which tickles a bug in HashMap.
            asize = Math.max(2, acount / ecount);

            name = null;
            skipEmptyLines = true;
        }
    }
1308934
public void readFrom(java.io.InputStream is) throws IOException {
        int howmuch = 0;
        do {
            _write_pos += howmuch;
            moveWindow(_write_pos);
            howmuch = is.read(_memory_buffer);
        } while (howmuch != -1);
    }