keep track of the position the input stream for decryption, makes it possible to give accurate progress information

This commit is contained in:
Thialfihar 2010-05-17 14:19:36 +00:00
parent eb636fce47
commit 51866bb2b2
3 changed files with 85 additions and 5 deletions

View file

@ -1527,7 +1527,8 @@ public class Apg {
} }
public static Bundle decrypt(Context context, public static Bundle decrypt(Context context,
InputStream inStream, OutputStream outStream, PositionAwareInputStream inStream, OutputStream outStream,
long dataLength,
String passPhrase, ProgressDialogUpdater progress, String passPhrase, ProgressDialogUpdater progress,
boolean assumeSymmetric) boolean assumeSymmetric)
throws IOException, GeneralException, PGPException, SignatureException { throws IOException, GeneralException, PGPException, SignatureException {
@ -1685,6 +1686,7 @@ public class Apg {
} }
int n = 0; int n = 0;
int done = 0; int done = 0;
long startPos = inStream.position();
while ((n = dataIn.read(buffer)) > 0) { while ((n = dataIn.read(buffer)) > 0) {
out.write(buffer, 0, n); out.write(buffer, 0, n);
done += n; done += n;
@ -1698,6 +1700,12 @@ public class Apg {
} }
// unknown size, but try to at least have a moving, slowing down progress bar // unknown size, but try to at least have a moving, slowing down progress bar
currentProgress = startProgress + (endProgress - startProgress) * done / (done + 100000); currentProgress = startProgress + (endProgress - startProgress) * done / (done + 100000);
if (dataLength - startPos == 0) {
currentProgress = endProgress;
} else {
currentProgress = (int)(startProgress + (endProgress - startProgress) *
(inStream.position() - startPos) / (dataLength - startPos));
}
progress.setProgress(currentProgress, 100); progress.setProgress(currentProgress, 100);
} }

View file

@ -436,21 +436,26 @@ public class DecryptActivity extends BaseActivity {
Message msg = new Message(); Message msg = new Message();
try { try {
InputStream in = null; PositionAwareInputStream in = null;
OutputStream out = null; OutputStream out = null;
long size = 0;
if (mDecryptTarget == Id.target.message) { if (mDecryptTarget == Id.target.message) {
String messageData = mMessage.getText().toString(); String messageData = mMessage.getText().toString();
in = new ByteArrayInputStream(messageData.getBytes()); in = new PositionAwareInputStream(new ByteArrayInputStream(messageData.getBytes()));
out = new ByteArrayOutputStream(); out = new ByteArrayOutputStream();
size = messageData.getBytes().length;
} else { } else {
in = new FileInputStream(mInputFilename); in = new PositionAwareInputStream(new FileInputStream(mInputFilename));
out = new FileOutputStream(mOutputFilename); out = new FileOutputStream(mOutputFilename);
File file = new File(mInputFilename);
size = file.length();
} }
if (mSignedOnly) { if (mSignedOnly) {
data = Apg.verifyText(this, in, out, this); data = Apg.verifyText(this, in, out, this);
} else { } else {
data = Apg.decrypt(this, in, out, Apg.getCachedPassPhrase(getSecretKeyId()), data = Apg.decrypt(this, in, out, size, Apg.getCachedPassPhrase(getSecretKeyId()),
this, mAssumeSymmetricEncryption); this, mAssumeSymmetricEncryption);
} }

View file

@ -0,0 +1,67 @@
package org.thialfihar.android.apg;
import java.io.IOException;
import java.io.InputStream;
public class PositionAwareInputStream extends InputStream {
private InputStream mStream;
private long mPosition;
public PositionAwareInputStream(InputStream in) {
mStream = in;
mPosition = 0;
}
@Override
public int read() throws IOException {
int ch = mStream.read();
++mPosition;
return ch;
}
@Override
public int available() throws IOException {
return mStream.available();
}
@Override
public void close() throws IOException {
mStream.close();
}
@Override
public boolean markSupported() {
return false;
}
@Override
public int read(byte[] b) throws IOException {
int result = mStream.read(b);
mPosition += result;
return result;
}
@Override
public int read(byte[] b, int offset, int length) throws IOException {
int result = mStream.read(b, offset, length);
mPosition += result;
return result;
}
@Override
public synchronized void reset() throws IOException {
mStream.reset();
mPosition = 0;
}
@Override
public long skip(long n) throws IOException {
long result = mStream.skip(n);
mPosition += result;
return result;
}
public long position() {
return mPosition;
}
}