Example usage for java.util.jar Attributes remove

List of usage examples for java.util.jar Attributes remove

Introduction

In this page you can find the example usage for java.util.jar Attributes remove.

Prototype

public Object remove(Object name) 

Source Link

Document

Removes the attribute with the specified name (key) from this Map.

Usage

From source file:com.android.builder.internal.packaging.sign.SignatureExtension.java

/**
 * Reads the signature file (if any) on the zip file.
 * <p>/*from   ww w  .  jav  a  2 s  .  c o  m*/
 * When this method terminates, we have the following guarantees:
 * <ul>
 *      <li>An internal signature manifest exists.</li>
 *      <li>All entries in the in-memory signature file exist in the zip file.</li>
 *      <li>All entries in the zip file (with the exception of the signature-related files,
 *      as specified by https://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html)
 *      exist in the in-memory signature file.</li>
 *      <li>All entries in the in-memory signature file have digests that match their
 *      contents in the zip.</li>
 *      <li>All entries in the in-memory signature manifest exist also in the manifest file
 *      and the digests are the same.</li>
 *      <li>The main attributes of the in-memory signature manifest are valid. The manifest's
 *      digest has not been verified and may not even exist.</li>
 *      <li>If the internal in-memory signature manifest differs in any way from the one
 *      written in the file, {@link #mDirty} will be set to {@code true}. Otherwise,
 *      {@link #mDirty} will be set to {@code false}.</li>
 * </ul>
 *
 * @throws IOException failed to read the signature file
 */
private void readSignatureFile() throws IOException {
    boolean needsNewSignature = false;

    StoredEntry signatureEntry = mManifestExtension.zFile().get(SIGNATURE_FILE);
    if (signatureEntry != null) {
        byte[] signatureData = signatureEntry.read();
        mSignatureFile.read(new ByteArrayInputStream(signatureData));

        Attributes mainAttrs = mSignatureFile.getMainAttributes();
        String versionName = mainAttrs.getValue(SIGNATURE_VERSION_NAME);
        String createdBy = mainAttrs.getValue(SIGNATURE_CREATED_BY_NAME);
        String apkSigned = mainAttrs.getValue(SIGNATURE_ANDROID_APK_SIGNED_NAME);

        if (!SIGNATURE_VERSION_VALUE.equals(versionName) || !SIGNATURE_CREATED_BY_VALUE.equals(createdBy)
                || mainAttrs.get(mDigestAlgorithm.manifestAttributeName) != null
                || !Objects.equal(mApkSignedHeaderValue, apkSigned)) {
            needsNewSignature = true;
        }
    } else {
        needsNewSignature = true;
    }

    if (needsNewSignature) {
        Attributes mainAttrs = mSignatureFile.getMainAttributes();

        mainAttrs.putValue(SIGNATURE_CREATED_BY_NAME, SIGNATURE_CREATED_BY_VALUE);
        mainAttrs.putValue(SIGNATURE_VERSION_NAME, SIGNATURE_VERSION_VALUE);
        if (mApkSignedHeaderValue != null) {
            mainAttrs.putValue(SIGNATURE_ANDROID_APK_SIGNED_NAME, mApkSignedHeaderValue);
        } else {
            mainAttrs.remove(SIGNATURE_ANDROID_APK_SIGNED_NAME);
        }

        mDirty = true;
    }

    /*
     * At this point we have a valid in-memory signature file with a valid header. mDirty
     * states whether this is the same as the file-based signature file.
     *
     * Now, check we have the same files in the zip as in the signature file and that all
     * digests match. While we do this, make sure the manifest is also up-do-date.
     *
     * We ignore all signature-related files that exist in the zip that are signature-related.
     * This are defined in the jar format specification.
     */
    Set<StoredEntry> allEntries = mManifestExtension.zFile().entries().stream()
            .filter(se -> !isIgnoredFile(se.getCentralDirectoryHeader().getName())).collect(Collectors.toSet());

    Set<String> sigEntriesToRemove = Sets.newHashSet(mSignatureFile.getEntries().keySet());
    Set<String> manEntriesToRemove = Sets.newHashSet(mManifestExtension.allEntries().keySet());
    for (StoredEntry se : allEntries) {
        /*
         * Update the entry's digest, if needed.
         */
        setDigestForEntry(se);

        /*
         * This entry exists in the file, so remove it from the list of entries to remove
         * from the manifest and signature file.
         */
        sigEntriesToRemove.remove(se.getCentralDirectoryHeader().getName());
        manEntriesToRemove.remove(se.getCentralDirectoryHeader().getName());
    }

    for (String toRemoveInSignature : sigEntriesToRemove) {
        mSignatureFile.getEntries().remove(toRemoveInSignature);
        mDirty = true;
    }

    for (String toRemoveInManifest : manEntriesToRemove) {
        mManifestExtension.removeEntry(toRemoveInManifest);
    }
}

From source file:org.kepler.kar.KARFile.java

/**
 * @param lsid//from w w w  .  ja v  a2  s  .c  o m
 * @throws IOException
 */
public void setLSID(KeplerLSID lsid) throws IOException {
    Attributes atts = getManifest().getMainAttributes();
    if (atts.containsKey(LSID)) {
        atts.remove(LSID);
    }
    atts.put(LSID, lsid.toString());
}

From source file:org.kepler.kar.KARFile.java

/**
 * Set the version of the KARFile.//  w  ww  .j  a v  a2s .  c om
 * 
 * @param version
 * @throws IOException
 *             if the given version is not supported
 */
public void setVersion(String version) throws IOException {
    if (_supportedVersions.contains(version)) {
        Attributes atts = getManifest().getMainAttributes();
        if (atts.containsKey(KAR_VERSION)) {
            atts.remove(KAR_VERSION);
        }
        atts.put(KAR_VERSION, version);
    } else {
        throw new IOException(version + " is not a supported KAR version.\n" + getSupportedVersionString());
    }
}