我在Java中编写了一个用于解析2.5GB维基百科文章XML文件的SAX解析器,有没有一种方法能够监视Java中解析进度呢?
感谢EJP的建议使用ProgressMonitorInputStream
,最终我扩展了FilterInputStream
以便可以使用ChangeListener
来监视当前读取的字节数。
通过这种方式,您可以更精细地控制进度,例如为大型XML文件的并行读取显示多个进度条。这正是我所做的。
因此,可监测流的简化版本如下:
/**
* A class that monitors the read progress of an input stream.
*
* @author Hermia Yeung "Sheepy"
* @since 2012-04-05 18:42
*/
public class MonitoredInputStream extends FilterInputStream {
private volatile long mark = 0;
private volatile long lastTriggeredLocation = 0;
private volatile long location = 0;
private final int threshold;
private final List<ChangeListener> listeners = new ArrayList<>(4);
/**
* Creates a MonitoredInputStream over an underlying input stream.
* @param in Underlying input stream, should be non-null because of no public setter
* @param threshold Min. position change (in byte) to trigger change event.
*/
public MonitoredInputStream(InputStream in, int threshold) {
super(in);
this.threshold = threshold;
}
/**
* Creates a MonitoredInputStream over an underlying input stream.
* Default threshold is 16KB, small threshold may impact performance impact on larger streams.
* @param in Underlying input stream, should be non-null because of no public setter
*/
public MonitoredInputStream(InputStream in) {
super(in);
this.threshold = 1024*16;
}
public void addChangeListener(ChangeListener l) { if (!listeners.contains(l)) listeners.add(l); }
public void removeChangeListener(ChangeListener l) { listeners.remove(l); }
public long getProgress() { return location; }
protected void triggerChanged( final long location ) {
if ( threshold > 0 && Math.abs( location-lastTriggeredLocation ) < threshold ) return;
lastTriggeredLocation = location;
if (listeners.size() <= 0) return;
try {
final ChangeEvent evt = new ChangeEvent(this);
for (ChangeListener l : listeners) l.stateChanged(evt);
} catch (ConcurrentModificationException e) {
triggerChanged(location); // List changed? Let's re-try.
}
}
@Override public int read() throws IOException {
final int i = super.read();
if ( i != -1 ) triggerChanged( location++ );
return i;
}
@Override public int read(byte[] b, int off, int len) throws IOException {
final int i = super.read(b, off, len);
if ( i > 0 ) triggerChanged( location += i );
return i;
}
@Override public long skip(long n) throws IOException {
final long i = super.skip(n);
if ( i > 0 ) triggerChanged( location += i );
return i;
}
@Override public void mark(int readlimit) {
super.mark(readlimit);
mark = location;
}
@Override public void reset() throws IOException {
super.reset();
if ( location != mark ) triggerChanged( location = mark );
}
}
它不知道 - 或者说不关心 - 底层流的大小,因此您需要通过其他方式获取它,比如从文件本身获取。
因此,这里提供了简化的示例用法:
try (
MonitoredInputStream mis = new MonitoredInputStream(new FileInputStream(file), 65536*4)
) {
// Setup max progress and listener to monitor read progress
progressBar.setMaxProgress( (int) file.length() ); // Swing thread or before display please
mis.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent e) {
SwingUtilities.invokeLater( new Runnable() { @Override public void run() {
progressBar.setProgress( (int) mis.getProgress() ); // Promise me you WILL use MVC instead of this anonymous class mess!
}});
}});
// Start parsing. Listener would call Swing event thread to do the update.
SAXParserFactory.newInstance().newSAXParser().parse(mis, this);
} catch ( IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
} finally {
progressBar.setVisible(false); // Again please call this in swing event thread
}
在我的案例中,进度条从左到右平稳地增长,没有异常跳跃。调整阈值可以在性能和响应之间取得最佳平衡。如果太小,在小型设备上读取速度可能会超过两倍;如果太大,进度条将不会平滑。
希望对你有所帮助。如果你发现了错误或者打字错误,请随意编辑,或者点赞来为我加油! :D使用javax.swing.ProgressMonitorInputStream
。
通过覆盖 org.xml.sax.helpers.DefaultHandler/BaseHandler
的方法 setDocumentLocator
,可以获取文件的当前行/列的估计值。当需要时,该方法会使用一个对象调用,您可以从中获得当前行/列的近似值。
编辑:据我所知,没有标准方法可以获取绝对位置。然而,我确信一些 SAX 实现提供了这种信息。
假设您知道有多少篇文章,那么您不能在处理程序中保持一个计数器吗?例如:
public void startElement (String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
if(qName.equals("article")){
counter++
}
...
}
(我不知道你是否正在解析“文章”,这只是一个例子)
如果您事先不知道文章数量,您需要先进行计数。然后您可以打印状态nb tags read/total nb of tags
,每100个标签说一次(counter % 100 == 0
)。
甚至可以有另一个线程监视进度。在这种情况下,您可能希望同步访问计数器,但不必要,因为它不需要真正准确。
我的两分钱