下面列出了java.util.jar.Attributes#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
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);
}
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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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();
}