Search string ‘4914904’ exists at the tail of the stream.
Here’s the code
Scanner sc = new Scanner(xmlInputStream, "UTF-8");
if(sc.findWithinHorizon('4914904', 0) != null) { // <--- exception is thrown here
}
Any suggestions would be highly appreciated.
If you read the API for Scanner, you will see that if you pass the argument 0 to findWithinHorizon that it will read the entire buffer at once.
Since you don’t do anything with the value from this I see a few options.
Try changing to useDelimiter(String pattern) and then call
if(sc.hasNext())which may help some with the memory footprint.If you have XML, use an XML parser instead of a text scanner.
You could consider writing a custom method which parses the input stream one line at a time and perform the search. That way you don’t have to read in the full buffer.
Increase the memory you give the jvm when it starts
-Xmx256mOn a side note: Don’t re-write the code when you post here. Just copy and paste.