Chap
Chap

Reputation: 2796

Get the amount of bytes read in by javax.xml.stream.XMLStreamReader

Is there any way to get the amount of bytes read in by the XMLStreamReader, I am using a java.io.FileReader which is passed into the factory that creates the xml reader. I'm doubting this is possible with the XMLStreamReader but any work around is great.

Upvotes: 1

Views: 1084

Answers (2)

TofuBeer
TofuBeer

Reputation: 61536

Assuming you are doing somthing like this:

final XMLInputFactory inputFactory;
final XMLStreamReader reader;
final InputStream     stream;

inputFactory = XMLInputFactory.newInstance();
stream       = new FileInputStream(file);
reader       = inputFactory.createXMLStreamReader(stream);

You would do something like this:

final XMLInputFactory     inputFactory;
final XMLStreamReader     reader;
final InputStream         stream;
final CountingInputStream countingStream;

inputFactory   = XMLInputFactory.newInstance();
stream         = new FileInputStream(file);
countingStream = new CountingStream(stream);
reader         = inputFactory.createXMLStreamReader(countingStream);

Where CoutingInputStream is a class that you would need to write/find that keeps track of the number of bytes being read from the underlying InputStream object.

Upvotes: 1

Esko
Esko

Reputation: 29377

One popular way is to create a ByteCountingReader(Reader r);, I guess I don't have to be any more specific than this :-)

Upvotes: 1

Related Questions