Java Code Examples for java.util.jar.Attributes#size()

The following examples show how to use java.util.jar.Attributes#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManifestWriter.java    From Xpatch with Apache License 2.0 6 votes vote down vote up
public static void writeMainSection(OutputStream out, Attributes attributes)
        throws IOException {

    // Main section must start with the Manifest-Version attribute.
    // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
    String manifestVersion = attributes.getValue(Attributes.Name.MANIFEST_VERSION);
    if (manifestVersion == null) {
        throw new IllegalArgumentException(
                "Mandatory " + Attributes.Name.MANIFEST_VERSION + " attribute missing");
    }
    writeAttribute(out, Attributes.Name.MANIFEST_VERSION, manifestVersion);

    if (attributes.size() > 1) {
        SortedMap<String, String> namedAttributes = getAttributesSortedByName(attributes);
        namedAttributes.remove(Attributes.Name.MANIFEST_VERSION.toString());
        writeAttributes(out, namedAttributes);
    }
    writeSectionDelimiter(out);
}
 
Example 2
Source File: SignatureFileWriter.java    From Xpatch with Apache License 2.0 6 votes vote down vote up
public static void writeMainSection(OutputStream out, Attributes attributes)
        throws IOException {

    // Main section must start with the Signature-Version attribute.
    // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
    String signatureVersion = attributes.getValue(Attributes.Name.SIGNATURE_VERSION);
    if (signatureVersion == null) {
        throw new IllegalArgumentException(
                "Mandatory " + Attributes.Name.SIGNATURE_VERSION + " attribute missing");
    }
    ManifestWriter.writeAttribute(out, Attributes.Name.SIGNATURE_VERSION, signatureVersion);

    if (attributes.size() > 1) {
        SortedMap<String, String> namedAttributes =
                ManifestWriter.getAttributesSortedByName(attributes);
        namedAttributes.remove(Attributes.Name.SIGNATURE_VERSION.toString());
        ManifestWriter.writeAttributes(out, namedAttributes);
    }
    writeSectionDelimiter(out);
}
 
Example 3
Source File: ManifestWriter.java    From walle with Apache License 2.0 6 votes vote down vote up
public static void writeMainSection(OutputStream out, Attributes attributes)
        throws IOException {

    // Main section must start with the Manifest-Version attribute.
    // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
    String manifestVersion = attributes.getValue(Attributes.Name.MANIFEST_VERSION);
    if (manifestVersion == null) {
        throw new IllegalArgumentException(
                "Mandatory " + Attributes.Name.MANIFEST_VERSION + " attribute missing");
    }
    writeAttribute(out, Attributes.Name.MANIFEST_VERSION, manifestVersion);

    if (attributes.size() > 1) {
        SortedMap<String, String> namedAttributes = getAttributesSortedByName(attributes);
        namedAttributes.remove(Attributes.Name.MANIFEST_VERSION.toString());
        writeAttributes(out, namedAttributes);
    }
    writeSectionDelimiter(out);
}
 
Example 4
Source File: SignatureFileWriter.java    From walle with Apache License 2.0 6 votes vote down vote up
public static void writeMainSection(OutputStream out, Attributes attributes)
        throws IOException {

    // Main section must start with the Signature-Version attribute.
    // See https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Signed_JAR_File.
    String signatureVersion = attributes.getValue(Attributes.Name.SIGNATURE_VERSION);
    if (signatureVersion == null) {
        throw new IllegalArgumentException(
                "Mandatory " + Attributes.Name.SIGNATURE_VERSION + " attribute missing");
    }
    ManifestWriter.writeAttribute(out, Attributes.Name.SIGNATURE_VERSION, signatureVersion);

    if (attributes.size() > 1) {
        SortedMap<String, String> namedAttributes =
                ManifestWriter.getAttributesSortedByName(attributes);
        namedAttributes.remove(Attributes.Name.SIGNATURE_VERSION.toString());
        ManifestWriter.writeAttributes(out, namedAttributes);
    }
    writeSectionDelimiter(out);
}
 
Example 5
Source File: ContentComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Compares two manifest files. First check is for number of attributes,
 * next one is comparing name-value pairs.
 *
 *@param mf1, mf2 manifests to compare
 *@param ignoredEntries array of manifest entries to ignore
 *@return true if files contains the same entries/values in manifest
 */
public static boolean equalsManifest(File mf1, File mf2, String[] ignoredEntries) {
    if (ignoredEntries == null) {
        ignoredEntries = new String[] {};
    }
    try {
        Manifest m1 = new Manifest(new FileInputStream(mf1));
        Manifest m2 = new Manifest(new FileInputStream(mf2));
        Attributes a1 = m1.getMainAttributes();
        Attributes a2 = m2.getMainAttributes();
        if (a1.size() != a2.size()) {
            return false;
        }
        for (Iterator<Object> i = a1.keySet().iterator(); i.hasNext();) {
            Attributes.Name a = (Attributes.Name) i.next();
            boolean b = true;
            for (int j = 0; j < ignoredEntries.length; j++) {
                if (a.toString().equals(ignoredEntries[j])) {
                    a2.remove(a);
                    b = false;
                    break;
                }
            }
            if (b && (a1.get(a).equals(a2.get(a)))) {
                a2.remove(a);
            }
        }
        return a2.isEmpty();
    } catch (FileNotFoundException fnfe) {
        LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", fnfe); //NOI18N
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", ioe); //NOI18N
    }
    return false;
}
 
Example 6
Source File: ContentComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Compares two manifest files. First check is for number of attributes,
 * next one is comparing name-value pairs.
 *
 *@param mf1, mf2 manifests to compare
 *@param ignoredEntries array of manifest entries to ignore
 *@return true if files contains the same entries/values in manifest
 */
public static boolean equalsManifest(File mf1, File mf2, String[] ignoredEntries) {
    if (ignoredEntries == null) {
        ignoredEntries = new String[]{};
    }
    try {
        Manifest m1 = new Manifest(new FileInputStream(mf1));
        Manifest m2 = new Manifest(new FileInputStream(mf2));
        Attributes a1 = m1.getMainAttributes();
        Attributes a2 = m2.getMainAttributes();
        if (a1.size() != a2.size()) {
            return false;
        }
        for (Iterator<Object> i = a1.keySet().iterator(); i.hasNext();) {
            Attributes.Name a = (Attributes.Name) i.next();
            boolean b = true;
            for (int j = 0; j < ignoredEntries.length; j++) {
                if (a.toString().equals(ignoredEntries[j])) {
                    a2.remove(a);
                    b = false;
                    break;
                }
            }
            if (b && (a1.get(a).equals(a2.get(a)))) {
                a2.remove(a);
            }
        }
        return a2.isEmpty();
    } catch (FileNotFoundException fnfe) {
        System.err.println("Exception from test - comparing manifests");
        fnfe.printStackTrace(System.err);
    } catch (IOException ioe) {
        System.err.println("Exception from test - comparing manifests");
        ioe.printStackTrace(System.err);
    }
    return false;
}
 
Example 7
Source File: JkManifest.java    From jeka with Apache License 2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if this manifest has no entry or has only
 * "Manifest-Version" entry.
 */
public boolean isEmpty() {
    final Attributes mainAttributes = manifest.getMainAttributes();
    if (mainAttributes.size() > 1) {
        return false;
    }
    if (mainAttributes.size() == 1
            && !mainAttributes.containsKey(Attributes.Name.MANIFEST_VERSION)) {
        return false;
    }
    return manifest.getEntries().size() == 0;
}
 
Example 8
Source File: JarSigner.java    From keystore-explorer with GNU General Public License v3.0 4 votes vote down vote up
private static String getManifestEntriesAttrs(JarFile jar) throws IOException {

		StringBuilder sbManifest = new StringBuilder();

		// Get current manifest
		Manifest manifest = jar.getManifest();

		// Write out entry attributes to manifest
		if (manifest != null) {
			// Get entry attributes
			Map<String, Attributes> entries = manifest.getEntries();

			boolean firstEntry = true;

			// For each entry...
			for (String entryName : entries.keySet()) {
				// Get entry's attributes
				Attributes entryAttrs = entries.get(entryName);

				// Completely ignore entries that contain only a xxx-Digest
				// attribute
				if ((entryAttrs.size() == 1)
						&& (entryAttrs.keySet().toArray()[0].toString().endsWith("-Digest"))) {
					continue;
				}

				if (!firstEntry) {
					// Entries subsequent to the first are split by a newline
					sbManifest.append(CRLF);
				}

				// Get entry attributes as a string to preserve their order
				String manifestEntryAttributes = getManifestEntryAttrs(jar, entryName);

				// Write them out
				sbManifest.append(manifestEntryAttributes);

				// The next entry will not be the first entry
				firstEntry = false;
			}
		}

		return sbManifest.toString();
	}