步骤 2 : 用法 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;
}
}
package util;
public interface ProgressMonitor {
public void progressChange(int percentage);
}
ProgressMonitor monitor = new ProgressMonitor() {
@Override
public void progressChange(int percentage) {
System.out.println(percentage);
}
};
ProgressInputStream pis = new ProgressInputStream(is,monitor,total);
ProgressMonitor monitor = new ProgressMonitor() { @Override public void progressChange(int percentage) { System.out.println(percentage); } }; ProgressInputStream pis = new ProgressInputStream(is,monitor,total);
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|