how2j.cn

步骤 1 : ProgressInputStream   
步骤 2 : 用法   

步骤 1 :

ProgressInputStream

edit
package util; import java.io.IOException; import java.io.InputStream; public class ProgressInputStream extends InputStream{ long sum = 0; long per = 0; long newPer = 0; public int read() throws IOException { return is.read(); } public int hashCode() { return is.hashCode(); } public int read(byte[] b) throws IOException { int result = super.read(b); sum += result; newPer = sum * 100 / total; if (newPer > per) { per = newPer; monitor.progressChange((int)per); } return result; } public boolean equals(Object obj) { return is.equals(obj); } public int read(byte[] b, int off, int len) throws IOException { return is.read(b, off, len); } public long skip(long n) throws IOException { return is.skip(n); } public String toString() { return is.toString(); } public int available() throws IOException { return is.available(); } public void close() throws IOException { is.close(); } public void mark(int readlimit) { is.mark(readlimit); } public void reset() throws IOException { is.reset(); } public boolean markSupported() { return is.markSupported(); } InputStream is; int total; ProgressMonitor monitor; public ProgressInputStream(InputStream is,ProgressMonitor monitor,int total){ this.is = is; this.monitor = monitor; this.total = total; } }
ProgressMonitor monitor = new ProgressMonitor() { @Override public void progressChange(int percentage) { System.out.println(percentage); } }; ProgressInputStream pis = new ProgressInputStream(is,monitor,total);
1
2
3
4
5
6
7
8
9
10
11
ProgressMonitor monitor = new ProgressMonitor() {
         
        @Override
        public void progressChange(int percentage) {
            System.out.println(percentage);
             
        }
    };
 
    ProgressInputStream pis = new ProgressInputStream(is,monitor,total);
    


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢