Reputation: 38
I try to implement a Progressbar during an Upload. A picture (207 byte) is uploaded to a FTP Server. The problem is, that the class ProgressInputStream doesn´t print the correct progress size in the console. This is how I start the upload:
File file = new File(path);
String firstRemoteFile = "" + file.getName() + IMEI;
InputStream inputStream = new FileInputStream(file);
System.out.println("Start uploading first file"+file.length());
ProgressInputStream progressInput = new ProgressInputStream("test",inputStream, file.length());
boolean done = ftpClient.storeFile(firstRemoteFile, progressInput);
ProgressInputStream
public class ProgressInputStream extends InputStream {
private final long size;
public long progress;
private long lastUpdate = 0;
private final InputStream inputStream;
private final String name;
private boolean closed = false;
public ProgressInputStream(String name, InputStream inputStream, long size) {
this.size = size;
this.inputStream = inputStream;
this.name = name;
}
public ProgressInputStream(String name, FileContent content)
throws FileSystemException {
this.size = content.getSize();
this.name = name;
this.inputStream = content.getInputStream();
}
@Override
public void close() throws IOException {
super.close();
if (closed) throw new IOException("already closed");
closed = true;
}
@Override
public int read() throws IOException {
int count = inputStream.read();
if (count > 0)
progress += count;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
return count;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = inputStream.read(b, off, len);
if (count > 0)
progress += count;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
System.out.println("count"+count+"size"+size);
return count;
}
static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {
System.out.println("name "+ name+" progress "+ progress+" lastUpdate "+ lastUpdate+" "+ "sie "+ size);
return lastUpdate;
}}
Logcat
09-17 20:03:39.828: I/System.out(11346): name test progress 13913 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14337 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14337 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14567 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14567 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14687 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14770 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14770 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207
I dont know why the progress is this high. At the end progress should be 207.
I am grateful for any advice (-:
Upvotes: 1
Views: 219
Reputation: 12063
change this
progress += count;
to
progress++;
EDIT
Check here
Int count = inputStream.read(b, off, len);
Does count == the full length of your file ?
Upvotes: 1
Reputation: 253
Change your ProgressInputStream like this:
public class ProgressInputStream extends InputStream {
public ProgressInputStream(String name, InputStream inputStream, long size) {
this.size = size;
this.inputStream = inputStream;
this.name = name;
}
public ProgressInputStream(String name, FileContent content)
throws FileSystemException {
this.size = content.getSize();
this.name = name;
this.inputStream = content.getInputStream();
}
@Override
public void close() throws IOException {
super.close();
if (closed) throw new IOException("already closed");
closed = true;
}
@Override
public int read() throws IOException {
int count = inputStream.read();
progress ++;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
return count;
}
/*@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = inputStream.read(b, off, len);
progress ++;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
System.out.println("count"+count+"size"+size);
return count;
}*/
static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {
/* if (Config.isInUnitTests()) return lastUpdate;
if (size < B_IN_MB/10) return lastUpdate;
if (progress - lastUpdate > 1024 * 10) {
lastUpdate = progress;
int hashes = (int) (((double)progress / (double)size) * 40);
if (hashes > 40) hashes = 40;
String bar = StringUtils.repeat("#",
hashes);
bar = StringUtils.rightPad(bar, 40);
System.out.format("%s [%s] %.2fMB/%.2fMB\r",
name, bar, progress / B_IN_MB, size / B_IN_MB);
System.out.flush();
}*/
System.out.println("name "+ name+" progress "+ progress+" lastUpdate "+ lastUpdate+" "+ "sie "+ size);
return lastUpdate;
}}
Upvotes: 1