Add progress bar to copy

This commit is contained in:
Victor Shcherb 2015-10-21 23:58:22 +02:00
parent 63ea53ee90
commit 7530aa7b60
2 changed files with 18 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package net.osmand.util;
import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import org.apache.commons.logging.Log;
@ -311,6 +312,21 @@ public class Algorithms {
}
}
public static void streamCopy(InputStream in, OutputStream out, IProgress pg, int bytesDivisor) throws IOException{
byte[] b = new byte[BUFFER_SIZE];
int read;
int cp = 0;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
cp += read;
if(pg != null && cp > bytesDivisor) {
pg.progress(cp / bytesDivisor);
cp = cp % bytesDivisor;
}
}
}
public static void oneByteStreamCopy(InputStream in, OutputStream out) throws IOException{
int read;
while ((read = in.read()) != -1) {

View file

@ -472,7 +472,8 @@ public class DashChooseAppDirFragment extends DashBaseFragment {
FileInputStream fin = new FileInputStream(f);
FileOutputStream fout = new FileOutputStream(t);
try {
Algorithms.streamCopy(fin, fout);
progress.startTask(ctx.getString(R.string.copying_osmand_one_file_descr, t.getName()), (int) (f.length() / 1024));
Algorithms.streamCopy(fin, fout, progress, 1024);
} finally {
fin.close();
fout.close();