Merge "Improve concurrency for JAR entry inspection requests." am: ac68c28831 am: 304cb0f015 am: c08fc58061

am: 8499c9af2b

* commit '8499c9af2bd2c3e4c815874b3e26c338293b3415':
  Improve concurrency for JAR entry inspection requests.

Change-Id: I32359d52d60e45491a29707ec304206591e20207
This commit is contained in:
Alex Klyubin 2016-05-23 19:40:39 +00:00 committed by android-build-merger
commit fbda082136
2 changed files with 40 additions and 9 deletions

View file

@ -28,6 +28,7 @@ import com.android.apksigner.core.util.DataSource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SignatureException;
@ -227,7 +228,7 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
GetJarEntryDataDigestRequest dataDigestRequest =
new GetJarEntryDataDigestRequest(
entryName,
V1SchemeSigner.getMessageDigestInstance(mV1ContentDigestAlgorithm));
V1SchemeSigner.getJcaMessageDigestAlgorithm(mV1ContentDigestAlgorithm));
mOutputJarEntryDigestRequests.put(entryName, dataDigestRequest);
mOutputJarEntryDigests.remove(entryName);
return dataDigestRequest;
@ -590,9 +591,9 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
private static class GetJarEntryDataRequest implements InspectJarEntryRequest {
private final String mEntryName;
private final Object mLock = new Object();
private final ByteArrayOutputStreamSink mBuf = new ByteArrayOutputStreamSink();
private boolean mDone;
private ByteArrayOutputStreamSink mBuf;
private GetJarEntryDataRequest(String entryName) {
mEntryName = entryName;
@ -607,6 +608,9 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
public DataSink getDataSink() {
synchronized (mLock) {
checkNotDone();
if (mBuf == null) {
mBuf = new ByteArrayOutputStreamSink();
}
return mBuf;
}
}
@ -640,7 +644,7 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
if (!mDone) {
throw new IllegalStateException("Not yet done");
}
return mBuf.getData();
return (mBuf != null) ? mBuf.getData() : new byte[0];
}
}
}
@ -650,17 +654,17 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
*/
private static class GetJarEntryDataDigestRequest implements InspectJarEntryRequest {
private final String mEntryName;
private final MessageDigest mMessageDigest;
private final DataSink mDataSink;
private final String mJcaDigestAlgorithm;
private final Object mLock = new Object();
private boolean mDone;
private DataSink mDataSink;
private MessageDigest mMessageDigest;
private byte[] mDigest;
private GetJarEntryDataDigestRequest(String entryName, MessageDigest digest) {
private GetJarEntryDataDigestRequest(String entryName, String jcaDigestAlgorithm) {
mEntryName = entryName;
mMessageDigest = digest;
mDataSink = new MessageDigestSink(new MessageDigest[] {mMessageDigest});
mJcaDigestAlgorithm = jcaDigestAlgorithm;
}
@Override
@ -672,10 +676,27 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
public DataSink getDataSink() {
synchronized (mLock) {
checkNotDone();
if (mDataSink == null) {
mDataSink = new MessageDigestSink(new MessageDigest[] {getMessageDigest()});
}
return mDataSink;
}
}
private MessageDigest getMessageDigest() {
synchronized (mLock) {
if (mMessageDigest == null) {
try {
mMessageDigest = MessageDigest.getInstance(mJcaDigestAlgorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(
mJcaDigestAlgorithm + " MessageDigest not available", e);
}
}
return mMessageDigest;
}
}
@Override
public void done() {
synchronized (mLock) {
@ -683,7 +704,9 @@ public class DefaultApkSignerEngine implements ApkSignerEngine {
return;
}
mDone = true;
mDigest = mMessageDigest.digest();
mDigest = getMessageDigest().digest();
mMessageDigest = null;
mDataSink = null;
}
}

View file

@ -168,6 +168,14 @@ public abstract class V1SchemeSigner {
}
}
/**
* Returns the JCA {@link MessageDigest} algorithm corresponding to the provided digest
* algorithm.
*/
public static String getJcaMessageDigestAlgorithm(DigestAlgorithm digestAlgorithm) {
return digestAlgorithm.getJcaMessageDigestAlgorithm();
}
/**
* Returns {@code true} if the provided JAR entry must be mentioned in signed JAR archive's
* manifest.