tags:

views:

257

answers:

2

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.

+1  A: 

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

Esko
+1  A: 

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.

TofuBeer