Subject: Work around removal of pkcs7.encodeSignedData(OutputStream)

pkcs7.encodeSignedData(OutputStream) was removed in Java 21.
Explicitly instantiate DerOutputStream to encode pkcs7 contents.

Author: Vladimir Petko <vladimir.petko@canonical.com>
Bug-Ubuntu: https://bugs.launchpad.net/debian/+source/android-platform-tools-base/+bug/2056088
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1058331
Forwarded: not-needed
Last-Update: 2024-03-06

--- a/sdklib/src/main/java/com/android/sdklib/internal/build/SignedJarBuilder.java
+++ b/sdklib/src/main/java/com/android/sdklib/internal/build/SignedJarBuilder.java
@@ -25,6 +25,7 @@
 import sun.security.pkcs.SignerInfo;
 import sun.security.x509.AlgorithmId;
 import sun.security.x509.X500Name;
+import sun.security.util.DerOutputStream;

 import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
@@ -394,6 +395,9 @@
                 new X509Certificate[] { publicKey },
                 new SignerInfo[] { signerInfo });

-        pkcs7.encodeSignedData(mOutputJar);
+        try (DerOutputStream derout = new DerOutputStream()) {
+            pkcs7.encodeSignedData(derout);
+            mOutputJar.write(derout.toByteArray());
+        }
     }
 }
