I have a simple code that gets xml file from given URL:
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(link);
that code returns xml document (org.w3c.dom.Document). I just need to get size of resulting xml document. Is there any elegant way to do it, WITHOUT involving third-party jars?
P.S. size in KB, or MB, not number of nods
First naive version: Load the file into a local buffer. Then you know how long is your input. Then parse the XML out of the buffer:
Drawback is the additional buffer in RAM.
Second more elegant version: Wrap the input stream with a custom
java.io.FilterInputStreamcounting the bytes streaming through it:Here is the
CountInputStream. Allread()methods are overwritten to delegate to the super class and count the resulting bytes: