有什么方法可以获取XMLStreamReader读取的字节数,我正在使用java.io.FileReader,该参数传递给创建xml阅读器的工厂。我怀疑XMLStreamReader是否可以实现,但是任何解决方法都很棒。

最佳答案

假设您正在做这样的事情:

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

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


您将执行以下操作:

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);


其中CoutingInputStream是您需要编写/查找的类,该类跟踪从基础InputStream对象读取的字节数。

09-12 00:06