The Sun BabelFish Blog
Don't panic !
LimitedInputStream: how to secure against OutOfMemoryErrors
When reading data off the web one has to always to be ready for the worst.
One of the situations that is perhaps too often ignored is that the file
one may want to read, may be huge, if not infinite. When parsing xml with
SAX this could just lead to method that never returns, but when working
with a DOM this could also lead to a huge data structure being created,
and the fatal java.lang.OutOfMemoryError being thrown. To guard against
this sitation I yesterday wrote a special LimitedInputStream
class. You just use it like this:
Document document = builder.parse(
new LimitedInputStream(
rsd.openConnection().getInputStream(),
100 * 1024)
);
If the file you download exceeds the size in bytes passed to the
constructor an IOException is thrown. In my case I am trying to parse Real
Simple Discoverability xml files, which ought to be very small (much
less than the 100k I allow for).
Very simple, but I am sure, very useful. This has probably been done before, but anyway, it's released under a BSD licence, so anyone can use it. Feedback appreciated.
-----
Update: I just realised that I could have used the URLConnection.getContentLength() method to do this! Duh.
----Update: Tim Bray tells me that the Content Length is not a reliable indicator of the length of the message, and is not allways present. So it is still a good precautionary step to use the LimitedInputStream or the LimitInputStream developed by the jxta group (thanks to Mike Duigou).
Posted at 10:37PM Sep 15, 2005 [permalink/trackback] by Henry Story in Java | Comments[2]
Note on comments:
- I know the forms below are a little small. We have asked for years for this to be changed, but I don't think it's going to happen soon. In Apple's Safari you can resize the entry box with you mouse. For people using other browsers click on this javascript link, that should allow you to resize your form.
- Comments are moderated, so they will take a little time to appear. Currently moderation means I have to read them personally. Hopefully with OpenId deployment, this will become more automated.
- HTML markup no longer works here, due to some decision made somewhere. Sorry about that.
- If you are having trouble posting, it may be that you need javascript to be enabled. I don't think javascript should be needed for submitting a form, but that's the way it is here.
- Check your comments by using the preview button...

Posted by Mike Duigou on September 16, 2005 at 01:18 AM CEST #
Posted by Henry Story on September 17, 2005 at 11:05 AM CEST #