aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/ProgressInputStream.java
blob: 552aee0f62677a746c54e0de59b006982ddccb34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cgeo.geocaching.files;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Stream to measure progress of reading automatically.
 * <p>
 * The method @link ProgressInputStream#read(byte[]) does not need to be overridden as it delegates to @link
 * ProgressInputStream#read(byte[], int, int) anyway.
 * </p>
 *
 */
public class ProgressInputStream extends FilterInputStream {

    private int progress = 0;

    protected ProgressInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        final int read = super.read();
        if (read >= 0) {
            progress++;
        }
        return read;
    }

    @Override
    public int read(byte[] buffer, int offset, int count) throws IOException {
        final int read = super.read(buffer, offset, count);
        progress += read;
        return read;
    }

    int getProgress() {
        return progress;
    }

}