Reputation: 343
I have an InputStream
object which contains several million file informations (Name, create date, author etc.) in XML
format. I've already tried to convert it to String
using IOUtils.copy
method, but since the size of that information is pretty large it throws an java.lang.OutOfMemoryError
, after running for few minutes.
Increasing JVM
memory is not an option, since the number of files, which I collect info from, is increasing forever. So can someone suggest me what should I do to solve the issue?
Upvotes: 0
Views: 278
Reputation: 4883
The problem that you are having is the very reason stream based IO exists, it is simply not viable to slurp huge amounts of data into memory before consuming it.
Parse your stream as... a stream! See the Oracle tutorials
for more information on stream based XML parsing using SAX.
XMLReader xmlreader =
SAXParserFactory.newInstance().newSAXParser().getXMLReader();
xmlreader.setContentHandler(new ContentHandler() {
...
});
xmlreader.parse(new InputSource(myInputStream));
Upvotes: 2