Home | History | Annotate | Download | only in jar
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.apksig.internal.jar;
     18 
     19 import java.io.IOException;
     20 import java.io.OutputStream;
     21 import java.util.SortedMap;
     22 import java.util.jar.Attributes;
     23 
     24 /**
     25  * Producer of JAR signature file ({@code *.SF}).
     26  *
     27  * @see <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#JAR_Manifest">JAR Manifest format</a>
     28  */
     29 public abstract class SignatureFileWriter {
     30     private SignatureFileWriter() {}
     31 
     32     public static void writeMainSection(OutputStream out, Attributes attributes)
     33             throws IOException {
     34 
     35         // Main section must start with the Signature-Version attribute.
     36         // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
     37         String signatureVersion = attributes.getValue(Attributes.Name.SIGNATURE_VERSION);
     38         if (signatureVersion == null) {
     39             throw new IllegalArgumentException(
     40                     "Mandatory " + Attributes.Name.SIGNATURE_VERSION + " attribute missing");
     41         }
     42         ManifestWriter.writeAttribute(out, Attributes.Name.SIGNATURE_VERSION, signatureVersion);
     43 
     44         if (attributes.size() > 1) {
     45             SortedMap<String, String> namedAttributes =
     46                     ManifestWriter.getAttributesSortedByName(attributes);
     47             namedAttributes.remove(Attributes.Name.SIGNATURE_VERSION.toString());
     48             ManifestWriter.writeAttributes(out, namedAttributes);
     49         }
     50         writeSectionDelimiter(out);
     51     }
     52 
     53     public static void writeIndividualSection(OutputStream out, String name, Attributes attributes)
     54             throws IOException {
     55         ManifestWriter.writeIndividualSection(out, name, attributes);
     56     }
     57 
     58     public static void writeSectionDelimiter(OutputStream out) throws IOException {
     59         ManifestWriter.writeSectionDelimiter(out);
     60     }
     61 }
     62