监控标记支持Java中的InputStream

时间:2016-04-14 13:28:01

标签: java inputstream monitor

我试图在Java中为InputStream类实现我自己的监视器。我使用类FilterInputStream只覆盖我需要在应用程序中调用的方法,避免重新实现所有内容。我想知道如何处理mark(int readlimit)reset()方法的调用。以下是示例代码:

public class MonitoredInputStream extends FilterInputStream implements
        ProgressMonitor {

    /**
     * List of displays associated to this progress monitor
     */
    private final List<ProgressDisplay> displays = new ArrayList<ProgressDisplay>();

    private final int size;

    private int progress = 0;

    private int marked = -1;

    /**
     * Creates a monitored input stream
     * 
     * @param in
     *            the input stream
     * @throws IOException 
     */
    public MonitoredInputStream(InputStream in) throws IOException {
        super(in);
        this.size = in.available();
    }

    @Override
    public void addProgressDisplay(ProgressDisplay display) {
        int index = displays.indexOf(display);
        if (index < 0) {
            displays.add(display);
        }
    }

    @Override
    public void removeProgressDisplay(ProgressDisplay display) {
        int index = displays.indexOf(display);
        if (index > 0) {
            displays.remove(display);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.io.FilterInputStream#read()
     */
    @Override
    public int read() throws IOException {
        int read = super.read();
        updateDisplays(read);
        return read;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.io.FilterInputStream#read(byte[], int, int)
     */
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int read = super.read(b, off, len);
        updateDisplays(read);
        return read;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.io.FilterInputStream#read(byte[])
     */
    @Override
    public int read(byte[] b) throws IOException {
        int read = super.read(b);
        updateDisplays(read);
        return read;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.io.FilterInputStream#skip(long)
     */
    @Override
    public long skip(long n) throws IOException {
        long skipped = super.skip(n);
        if (skipped > Integer.MAX_VALUE) {
            skipped = Integer.MAX_VALUE;
        }
        updateDisplays((int) skipped);
        return skipped;
    }

    @Override
    public void updateDisplays(int progress) {
        addProgress(progress);
        displays.forEach(d -> d.progress(getProgress()));
    }

    /* (non-Javadoc)
     * @see java.io.FilterInputStream#mark(int)
     */
    @Override
    public synchronized void mark(int readlimit) {
        marked = 0;
        super.mark(readlimit);
    }

    /* (non-Javadoc)
     * @see java.io.FilterInputStream#reset()
     */
    @Override
    public synchronized void reset() throws IOException {
        marked = -1;
        super.reset();
    }

    private void addProgress(int prog){
        if(marked == -1){
            progress += prog;
        }else{
            marked += prog;
        }
    }

    private double getProgress() {
        int p = (marked == -1) ? progress : progress + marked;
        BigDecimal num = new BigDecimal(p);
        BigDecimal den = new BigDecimal(size);

        return num.divide(den, 10, BigDecimal.ROUND_CEILING).doubleValue();
    }
}

我将这个类与显式类结合使用,它基本上打印出InputStream报告的进度,但是当我使用标记的流时,它会得到奇怪的结果(例如负面进展或其他奇怪的行为)。 / p>

任何帮助将不胜感激!

谢谢Giovanni。

0 个答案:

没有答案