export: improve stream handling, performance
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
package org.sufficientlysecure.keychain.operations;
|
||||
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -111,12 +113,18 @@ public class ExportOperation extends BaseOperation<ExportKeyringParcel> {
|
||||
? exportInput.mOutputUri
|
||||
: TemporaryStorageProvider.createFile(mContext);
|
||||
|
||||
int exportedDataSize;
|
||||
|
||||
{ // export key data, and possibly return if we don't encrypt
|
||||
|
||||
OutputStream outStream = mContext.getContentResolver().openOutputStream(exportOutputUri);
|
||||
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(
|
||||
mContext.getContentResolver().openOutputStream(exportOutputUri)));
|
||||
|
||||
boolean exportSuccess = exportKeysToStream(
|
||||
log, exportInput.mMasterKeyIds, exportInput.mExportSecret, outStream);
|
||||
|
||||
exportedDataSize = outStream.size();
|
||||
|
||||
if (!exportSuccess) {
|
||||
// if there was an error, it will be in the log so we just have to return
|
||||
return new ExportResult(ExportResult.RESULT_ERROR, log);
|
||||
@@ -138,7 +146,7 @@ public class ExportOperation extends BaseOperation<ExportKeyringParcel> {
|
||||
InputStream inStream = mContext.getContentResolver().openInputStream(exportOutputUri);
|
||||
|
||||
String filename;
|
||||
if (exportInput.mMasterKeyIds.length == 1) {
|
||||
if (exportInput.mMasterKeyIds != null && exportInput.mMasterKeyIds.length == 1) {
|
||||
filename = "backup_" + KeyFormattingUtils.convertKeyIdToHex(exportInput.mMasterKeyIds[0]);
|
||||
filename += exportInput.mExportSecret ? ".sec.asc" : ".pub.asc";
|
||||
} else {
|
||||
@@ -146,9 +154,10 @@ public class ExportOperation extends BaseOperation<ExportKeyringParcel> {
|
||||
filename += exportInput.mExportSecret ? ".asc" : ".pub.asc";
|
||||
}
|
||||
|
||||
InputData inputData = new InputData(inStream, 0L, filename);
|
||||
InputData inputData = new InputData(inStream, exportedDataSize, filename);
|
||||
|
||||
OutputStream outStream = mContext.getContentResolver().openOutputStream(exportInput.mOutputUri);
|
||||
outStream = new BufferedOutputStream(outStream);
|
||||
|
||||
PgpSignEncryptResult encryptResult = pseOp.execute(inputParcel, new CryptoInputParcel(), inputData, outStream);
|
||||
if (!encryptResult.success()) {
|
||||
|
||||
@@ -354,6 +354,22 @@ public class ExportTest {
|
||||
"backup_" + KeyFormattingUtils.convertKeyIdToHex(mStaticRing1.getMasterKeyId()) + ".pub.asc",
|
||||
result.getDecryptionMetadata().getFilename());
|
||||
|
||||
TestingUtils.assertArrayEqualsPrefix("exported data must start with ascii armor header",
|
||||
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes());
|
||||
TestingUtils.assertArrayEqualsSuffix("exported data must end with ascii armor header",
|
||||
"-----END PGP PUBLIC KEY BLOCK-----\n".getBytes(), result.getOutputBytes());
|
||||
|
||||
{
|
||||
IteratorWithIOThrow<UncachedKeyRing> unc
|
||||
= UncachedKeyRing.fromStream(new ByteArrayInputStream(result.getOutputBytes()));
|
||||
|
||||
assertTrue("export must have one key", unc.hasNext());
|
||||
UncachedKeyRing ring = unc.next();
|
||||
Assert.assertEquals("exported key has correct masterkeyid",
|
||||
mStaticRing1.getMasterKeyId(), ring.getMasterKeyId());
|
||||
assertFalse("export must have exactly one key", unc.hasNext());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user