Commit c5f651bf by Mark Wielaard Committed by Mark Wielaard

backport: *.java: Reformat all to unofficial standard coding style.

	Merge with Classpath (changes by Bryce McKinlay)
	* java/util/jar/*.java: Reformat all to unofficial standard coding
	style. No changes of substance.

From-SVN: r37538
parent c003f378
2000-11-17 Mark Wielaar <mark@klomp.org>
Merge with Classpath (changes by Bryce McKinlay)
* java/util/jar/*.java: Reformat all to unofficial standard coding
style. No changes of substance.
2000-11-17 Mark Wielaard <mark@klomp.org> 2000-11-17 Mark Wielaard <mark@klomp.org>
* java/util/zip/*.java: Javadoc updates. * java/util/zip/*.java: Javadoc updates.
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -54,533 +54,560 @@ import java.util.Set; ...@@ -54,533 +54,560 @@ import java.util.Set;
* @see java.util.jar.Attributes.Name * @see java.util.jar.Attributes.Name
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class Attributes implements Cloneable, Map { public class Attributes implements Cloneable, Map
{
// Fields
/**
* The map that holds all the attribute name/value pairs. In this
* implementation it is actually a Hashtable, but that can be different in
* other implementations.
*/
protected Map map;
// Inner class
/**
* Represents a name of a Manifest Attribute. Defines a couple of well
* know names for the general main attributes, stand alone application
* attributes, applet attributes, extension identification attributes,
* package versioning and sealing attributes, file contents attributes,
* bean objects attribute and signing attributes. See the
* <p>
* The characters of a Name must obey the following restrictions:
* <ul>
* <li> Must contain at least one character
* <li> The first character must be alphanumeric (a-z, A-Z, 0-9)
* <li> All other characters must be alphanumeric, a '-' or a '_'
* </ul>
* <p>
* When comparing Names (with <code>equals</code>) all characters are
* converted to lowercase. But you can get the original case sensitive
* string with the <code>toString()</code> method.
*
* @since 1.2
* @author Mark Wielaard (mark@klomp.org)
*/
public static class Name
{
// Fields // Fields
// General Main Attributes
/** /**
* The map that holds all the attribute name/value pairs. In this * General main attribute -
* implementation it is actually a Hashtable, but that can be different in * the version of this Manifest file.
* other implementations.
*/ */
protected Map map; public static final Name MANIFEST_VERSION = new Name("Manifest-Version");
// Inner class
/** /**
* Represents a name of a Manifest Attribute. Defines a couple of well * General main attribute -
* know names for the general main attributes, stand alone application * tool and version that created this Manifest file.
* attributes, applet attributes, extension identification attributes,
* package versioning and sealing attributes, file contents attributes,
* bean objects attribute and signing attributes. See the
* <p>
* The characters of a Name must obey the following restrictions:
* <ul>
* <li> Must contain at least one character
* <li> The first character must be alphanumeric (a-z, A-Z, 0-9)
* <li> All other characters must be alphanumeric, a '-' or a '_'
* </ul>
* <p>
* When comparing Names (with <code>equals</code>) all characters are
* converted to lowercase. But you can get the original case sensitive
* string with the <code>toString()</code> method.
*
* @since 1.2
* @author Mark Wielaard (mark@klomp.org)
*/ */
public static class Name { public static final Name CREATED_BY = new Name("Created-By");
// Fields
// General Main Attributes
/**
* General main attribute -
* the version of this Manifest file.
*/
public static final Name MANIFEST_VERSION
= new Name("Manifest-Version");
/**
* General main attribute -
* tool and version that created this Manifest file.
*/
public static final Name CREATED_BY
= new Name("Created-By");
/**
* General main attribute -
* the version of the jar file signature.
*/
public static final Name SIGNATURE_VERSION
= new Name("Signature-Version");
/**
* General main attribute -
* (relative) URLs of the libraries/classpaths that the Classes in
* this jar file depend on.
*/
public static final Name CLASS_PATH
= new Name("Class-Path");
/**
* Stand alone application attribute -
* the entry (without the .class ending) that is the main
* class of this jar file.
*/
public static final Name MAIN_CLASS
= new Name("Main-Class");
/**
* Applet attribute -
* a list of extension libraries that the applet in this
* jar file depends on.
* For every named extension there should be some Attributes in the
* Manifest manifest file with the following Names:
* <ul>
* <li> &lt;extension&gt;-Extension-Name:
* unique name of the extension
* <li> &lt;extension&gt;-Specification-Version:
* minimum specification version
* <li> &lt;extension&gt;-Implementation-Version:
* minimum implementation version
* <li> &lt;extension&gt;-Implementation-Vendor-Id:
* unique id of implementation vendor
* <li> &lt;extension&gt;-Implementation-URL:
* where the latest version of the extension library can be found
* </ul>
*/
public static final Name EXTENSION_LIST
= new Name("Extension-List");
/**
* Extension identification attribute -
* the name if the extension library contained in the jar.
*/
public static final Name EXTENSION_NAME
= new Name("Extension-Name");
/**
* Extension identification attribute -
* synonym for <code>EXTENSTION_NAME</code>.
*/
public static final Name EXTENSION_INSTALLATION
= EXTENSION_NAME;
// Package versioning and sealing attributes
/**
* Package versioning -
* name of extension library contained in this jar.
*/
public static final Name IMPLEMENTATION_TITLE
= new Name("Implementation-Title");
/**
* Package versioning -
* version of the extension library contained in this jar.
*/
public static final Name IMPLEMENTATION_VERSION
= new Name("Implementation-Version");
/**
* Package versioning -
* name of extension library creator contained in this jar.
*/
public static final Name IMPLEMENTATION_VENDOR
= new Name("Implementation-Vendor");
/**
* Package versioning -
* unique id of extension library creator.
*/
public static final Name IMPLEMENTATION_VENDOR_ID
= new Name("Implementation-Vendor-Id");
/**
* Package versioning -
* location where this implementation can be downloaded.
*/
public static final Name IMPLEMENTATION_URL
= new Name("Implementation-URL");
/**
* Package versioning -
* title of the specification contained in this jar.
*/
public static final Name SPECIFICATION_TITLE
= new Name("Specification-Title");
/**
* Package versioning -
* version of the specification contained in this jar.
*/
public static final Name SPECIFICATION_VERSION
= new Name("Specification-Version");
/**
* Package versioning -
* organisation that maintains the specification contains in this
* jar.
*/
public static final Name SPECIFICATION_VENDOR
= new Name("Specification-Vendor");
/**
* Package sealing -
* whether (all) package(s) is(/are) sealed. Value is either "true"
* or "false".
*/
public static final Name SEALED
= new Name("Sealed");
/**
* File contents attribute -
* Mime type and subtype for the jar entry.
*/
public static final Name CONTENT_TYPE
= new Name("Content-Type");
/**
* Bean objects attribute -
* whether the entry is a Java Bean. Value is either "true" or "false".
*/
public static final Name JAVA_BEAN
= new Name("Java-Bean");
/**
* Signing attribute -
* application specific signing attribute. Must be understood by
* the manifest parser when present to validate the jar (entry).
*/
public static final Name MAGIC
= new Name("Magic");
/** The (lowercase) String representation of this Name */
private final String name;
/** The original String given to the constructor */
private final String origName;
// Constructor
/**
* Creates a new Name from the given String.
* Throws an IllegalArgumentException if the given String is empty or
* contains any illegal Name characters.
*
* @param name the name of the new Name
* @exception IllegalArgumentException if name isn't a valid String
* representation of a Name
* @exception NullPointerException if name is null
*/
public Name(String name) throws IllegalArgumentException,
NullPointerException {
// name must not be null
// this will throw a NullPointerException if it is
char chars[] = name.toCharArray();
// there must be at least one character
if (chars.length == 0)
throw new IllegalArgumentException(
"There must be at least one character in a name");
// first character must be alphanum
char c = chars[0];
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9')))
throw new IllegalArgumentException(
"First character must be alphanum");
// all other characters must be alphanums, '-' or '_'
for (int i = 1; i < chars.length; i++) {
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '-') || (c == '_')))
throw new IllegalArgumentException(
"Characters must be alphanums, '-' or '_'");
}
// Still here? Then convert to lower case and be done.
// Store the original name for toString();
this.origName = name;
this.name = name.toLowerCase();
}
/**
* Returns the hash code of the (lowercase) String representation of
* this Name.
*/
public int hashCode() {
return name.hashCode();
}
/**
* Checks if another object is equal to this Name object.
* Another object is equal to this Name object if it is an instance of
* Name and the (lowercase) string representation of the name is equal.
*/
public boolean equals(Object o) {
// Quick and dirty check
if (name == o)
return true;
try {
// Note that the constructor already converts the strings to
// lowercase.
String otherName = ((Name)o).name;
return name.equals(otherName);
} catch (ClassCastException cce) {
return false;
} catch (NullPointerException npe) {
return false;
}
}
/**
* Returns the string representation of this Name as given to the
* constructor (not neccesarily the lower case representation).
*/
public String toString() {
return origName;
}
}
// Constructors
/** /**
* Creates an empty Attributes map. * General main attribute -
* the version of the jar file signature.
*/ */
public Attributes() { public static final Name SIGNATURE_VERSION
map = new Hashtable(); = new Name("Signature-Version");
}
/** /**
* Creates an empty Attributes map with the given initial size. * General main attribute -
* @param size the initial size of the underlying map * (relative) URLs of the libraries/classpaths that the Classes in
* this jar file depend on.
*/ */
public Attributes(int size) { public static final Name CLASS_PATH = new Name("Class-Path");
map = new Hashtable(size);
}
/** /**
* Creates an Attributes map with the initial values taken from another * Stand alone application attribute -
* Attributes map. * the entry (without the .class ending) that is the main
* @param attr Attributes map to take the initial values from * class of this jar file.
*/ */
public Attributes(Attributes attr) { public static final Name MAIN_CLASS = new Name("Main-Class");
map = new Hashtable(attr.map);
}
// Methods
/** /**
* Gets the value of an attribute name given as a String. * Applet attribute -
* * a list of extension libraries that the applet in this
* @param name a String describing the Name to look for * jar file depends on.
* @return the value gotten from the map of null when not found * For every named extension there should be some Attributes in the
* Manifest manifest file with the following Names:
* <ul>
* <li> &lt;extension&gt;-Extension-Name:
* unique name of the extension
* <li> &lt;extension&gt;-Specification-Version:
* minimum specification version
* <li> &lt;extension&gt;-Implementation-Version:
* minimum implementation version
* <li> &lt;extension&gt;-Implementation-Vendor-Id:
* unique id of implementation vendor
* <li> &lt;extension&gt;-Implementation-URL:
* where the latest version of the extension library can be found
* </ul>
*/ */
public String getValue(String name) { public static final Name EXTENSION_LIST = new Name("Extension-List");
return (String)get(new Name(name));
}
/** /**
* Gets the value of the given attribute name. * Extension identification attribute -
* * the name if the extension library contained in the jar.
* @param name the Name to look for
* @return the value gotten from the map of null when not found
*/ */
public String getValue(Name name) { public static final Name EXTENSION_NAME = new Name("Extension-Name");
return (String)get(name);
}
/** /**
* Stores an attribute name (represented by a String) and value in this * Extension identification attribute -
* Attributes map. * synonym for <code>EXTENSTION_NAME</code>.
* When the (case insensitive string) name already exists the value is
* replaced and the old value is returned.
*
* @param name a (case insensitive) String representation of the attribite
* name to add/replace
* @param value the (new) value of the attribute name
* @returns the old value of the attribute name or null if it didn't exist
* yet
*/ */
public String putValue(String name, String value) public static final Name EXTENSION_INSTALLATION = EXTENSION_NAME;
{
return putValue(new Name(name), value);
}
// Package versioning and sealing attributes
/** /**
* Stores an attribute name (represented by a String) and value in this * Package versioning -
* Attributes map. * name of extension library contained in this jar.
* When the name already exists the value is replaced and the old value
* is returned.
* <p>
* I don't know why there is no public method with this signature. I think
* there should be one.
*
* @param name the attribite name to add/replace
* @param value the (new) value of the attribute name
* @returns the old value of the attribute name or null if it didn't exist
* yet
*/ */
private String putValue(Name name, String value) public static final Name IMPLEMENTATION_TITLE
{ = new Name("Implementation-Title");
return (String)put(name, value);
}
// Methods from Cloneable interface
/** /**
* Return a clone of this attribute map. * Package versioning -
* version of the extension library contained in this jar.
*/ */
public Object clone() { public static final Name IMPLEMENTATION_VERSION
return new Attributes(this); = new Name("Implementation-Version");
}
// Methods from Map interface
/** /**
* Removes all attributes. * Package versioning -
* name of extension library creator contained in this jar.
*/ */
public void clear() { public static final Name IMPLEMENTATION_VENDOR
map.clear(); = new Name("Implementation-Vendor");
}
/** /**
* Checks to see if there is an attribute with the specified name. * Package versioning -
* XXX - what if the object is a String? * unique id of extension library creator.
*
* @param attrName the name of the attribute to check
* @return true if there is an attribute with the specified name, false
* otherwise
*/ */
public boolean containsKey(Object attrName) { public static final Name IMPLEMENTATION_VENDOR_ID
return map.containsKey(attrName); = new Name("Implementation-Vendor-Id");
}
/** /**
* Checks to see if there is an attribute name with the specified value. * Package versioning -
* * location where this implementation can be downloaded.
* @param attrValue the value of a attribute to check
* @return true if there is an attribute name with the specified value,
* false otherwise
*/ */
public boolean containsValue(Object attrValue) { public static final Name IMPLEMENTATION_URL
return map.containsValue(attrValue); = new Name("Implementation-URL");
}
/** /**
* Gives a Set of attribute name and values pairs as MapEntries. * Package versioning -
* @see java.util.Map.Entry * title of the specification contained in this jar.
* @see java.util.Map#entrySet()
*
* @return a set of attribute name value pairs
*/ */
public Set entrySet() { public static final Name SPECIFICATION_TITLE
return map.entrySet(); = new Name("Specification-Title");
}
/** /**
* Checks to see if two Attributes are equal. The supplied object must be * Package versioning -
* a real instance of Attributes and contain the same attribute name/value * version of the specification contained in this jar.
* pairs.
*
* @param o another Attribute object which should be checked for equality
* @return true if the object is an instance of Attributes and contains the
* same name/value pairs, false otherwise
*/ */
public boolean equals(Object o) { public static final Name SPECIFICATION_VERSION
// quick and dirty check = new Name("Specification-Version");
if (this == o)
return true;
try {
return map.equals(((Attributes)o).map);
} catch (ClassCastException cce) {
return false;
} catch (NullPointerException npe) {
return false;
}
}
/** /**
* Gets the value of a specified attribute name. * Package versioning -
* XXX - what if the object is a String? * organisation that maintains the specification contains in this
* * jar.
* @param attrName the name of the attribute we want the value of
* @return the value of the specified attribute name or null when there is
* no such attribute name
*/ */
public Object get(Object attrName) { public static final Name SPECIFICATION_VENDOR
return map.get(attrName); = new Name("Specification-Vendor");
}
/** /**
* Returns the hashcode of the attribute name/value map. * Package sealing -
* whether (all) package(s) is(/are) sealed. Value is either "true"
* or "false".
*/ */
public int hashCode() { public static final Name SEALED = new Name("Sealed");
return map.hashCode();
}
/** /**
* Returns true if there are no attributes set, false otherwise. * File contents attribute -
* Mime type and subtype for the jar entry.
*/ */
public boolean isEmpty() { public static final Name CONTENT_TYPE = new Name("Content-Type");
return map.isEmpty();
}
/** /**
* Gives a Set of all the values of defined attribute names. * Bean objects attribute -
* whether the entry is a Java Bean. Value is either "true" or "false".
*/ */
public Set keySet() { public static final Name JAVA_BEAN = new Name("Java-Bean");
return map.keySet();
}
/** /**
* Adds or replaces a attribute name/value pair. * Signing attribute -
* XXX - What if the name is a string? What if the name is neither a Name * application specific signing attribute. Must be understood by
* nor a String? What if the value is not a string? * the manifest parser when present to validate the jar (entry).
*
* @param name the name of the attribute
* @param value the (new) value of the attribute
* @return the old value of the attribute or null when there was no old
* attribute with this name
*/ */
public Object put(Object name, Object value) { public static final Name MAGIC = new Name("Magic");
return map.put(name, value);
} /** The (lowercase) String representation of this Name */
private final String name;
/** The original String given to the constructor */
private final String origName;
// Constructor
/** /**
* Adds or replaces all attribute name/value pairs from another * Creates a new Name from the given String.
* Attributes object to this one. The supplied Map must be an instance of * Throws an IllegalArgumentException if the given String is empty or
* Attributes. * contains any illegal Name characters.
* *
* @param attr the Attributes object to merge with this one * @param name the name of the new Name
* @exception ClassCastException if the supplied map is not an instance of * @exception IllegalArgumentException if name isn't a valid String
* Attributes * representation of a Name
* @exception NullPointerException if name is null
*/ */
public void putAll(Map attr) { public Name(String name) throws IllegalArgumentException,
if (!(attr instanceof Attributes)) { NullPointerException
throw new ClassCastException( {
"Supplied Map is not an instance of Attributes"); // name must not be null
} // this will throw a NullPointerException if it is
map.putAll(attr); char chars[] = name.toCharArray();
// there must be at least one character
if (chars.length == 0)
throw new
IllegalArgumentException
("There must be at least one character in a name");
// first character must be alphanum
char c = chars[0];
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')))
throw new
IllegalArgumentException("First character must be alphanum");
// all other characters must be alphanums, '-' or '_'
for (int i = 1; i < chars.length; i++)
{
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || (c == '-') || (c == '_')))
throw new
IllegalArgumentException
("Characters must be alphanums, '-' or '_'");
}
// Still here? Then convert to lower case and be done.
// Store the original name for toString();
this.origName = name;
this.name = name.toLowerCase();
} }
/** /**
* Remove a attribute name/value pair. * Returns the hash code of the (lowercase) String representation of
* XXX - What if the name is a String? * this Name.
*
* @param name the name of the attribute name/value pair to remove
* @return the old value of the attribute or null if the attribute didn't
* exist
*/ */
public Object remove(Object name) { public int hashCode()
return map.remove(name); {
return name.hashCode();
} }
/** /**
* Returns the number of defined attribute name/value pairs. * Checks if another object is equal to this Name object.
* Another object is equal to this Name object if it is an instance of
* Name and the (lowercase) string representation of the name is equal.
*/ */
public int size() { public boolean equals(Object o)
return map.size(); {
// Quick and dirty check
if (name == o)
return true;
try
{
// Note that the constructor already converts the strings to
// lowercase.
String otherName = ((Name) o).name;
return name.equals(otherName);
}
catch (ClassCastException cce)
{
return false;
}
catch (NullPointerException npe)
{
return false;
}
} }
/** /**
* Returns all the values of the defined attribute name/value pairs as a * Returns the string representation of this Name as given to the
* Collection. * constructor (not neccesarily the lower case representation).
*/ */
public Collection values() { public String toString()
return map.values(); {
return origName;
} }
}
// Constructors
/**
* Creates an empty Attributes map.
*/
public Attributes()
{
map = new Hashtable();
}
/**
* Creates an empty Attributes map with the given initial size.
* @param size the initial size of the underlying map
*/
public Attributes(int size)
{
map = new Hashtable(size);
}
/**
* Creates an Attributes map with the initial values taken from another
* Attributes map.
* @param attr Attributes map to take the initial values from
*/
public Attributes(Attributes attr)
{
map = new Hashtable(attr.map);
}
// Methods
/**
* Gets the value of an attribute name given as a String.
*
* @param name a String describing the Name to look for
* @return the value gotten from the map of null when not found
*/
public String getValue(String name)
{
return (String) get(new Name(name));
}
/**
* Gets the value of the given attribute name.
*
* @param name the Name to look for
* @return the value gotten from the map of null when not found
*/
public String getValue(Name name)
{
return (String) get(name);
}
/**
* Stores an attribute name (represented by a String) and value in this
* Attributes map.
* When the (case insensitive string) name already exists the value is
* replaced and the old value is returned.
*
* @param name a (case insensitive) String representation of the attribite
* name to add/replace
* @param value the (new) value of the attribute name
* @returns the old value of the attribute name or null if it didn't exist
* yet
*/
public String putValue(String name, String value)
{
return putValue(new Name(name), value);
}
/**
* Stores an attribute name (represented by a String) and value in this
* Attributes map.
* When the name already exists the value is replaced and the old value
* is returned.
* <p>
* I don't know why there is no public method with this signature. I think
* there should be one.
*
* @param name the attribite name to add/replace
* @param value the (new) value of the attribute name
* @returns the old value of the attribute name or null if it didn't exist
* yet
*/
private String putValue(Name name, String value)
{
return (String) put(name, value);
}
// Methods from Cloneable interface
/**
* Return a clone of this attribute map.
*/
public Object clone()
{
return new Attributes(this);
}
// Methods from Map interface
/**
* Removes all attributes.
*/
public void clear()
{
map.clear();
}
/**
* Checks to see if there is an attribute with the specified name.
* XXX - what if the object is a String?
*
* @param attrName the name of the attribute to check
* @return true if there is an attribute with the specified name, false
* otherwise
*/
public boolean containsKey(Object attrName)
{
return map.containsKey(attrName);
}
/**
* Checks to see if there is an attribute name with the specified value.
*
* @param attrValue the value of a attribute to check
* @return true if there is an attribute name with the specified value,
* false otherwise
*/
public boolean containsValue(Object attrValue)
{
return map.containsValue(attrValue);
}
/**
* Gives a Set of attribute name and values pairs as MapEntries.
* @see java.util.Map.Entry
* @see java.util.Map#entrySet()
*
* @return a set of attribute name value pairs
*/
public Set entrySet()
{
return map.entrySet();
}
/**
* Checks to see if two Attributes are equal. The supplied object must be
* a real instance of Attributes and contain the same attribute name/value
* pairs.
*
* @param o another Attribute object which should be checked for equality
* @return true if the object is an instance of Attributes and contains the
* same name/value pairs, false otherwise
*/
public boolean equals(Object o)
{
// quick and dirty check
if (this == o)
return true;
try
{
return map.equals(((Attributes) o).map);
}
catch (ClassCastException cce)
{
return false;
}
catch (NullPointerException npe)
{
return false;
}
}
/**
* Gets the value of a specified attribute name.
* XXX - what if the object is a String?
*
* @param attrName the name of the attribute we want the value of
* @return the value of the specified attribute name or null when there is
* no such attribute name
*/
public Object get(Object attrName)
{
return map.get(attrName);
}
/**
* Returns the hashcode of the attribute name/value map.
*/
public int hashCode()
{
return map.hashCode();
}
/**
* Returns true if there are no attributes set, false otherwise.
*/
public boolean isEmpty()
{
return map.isEmpty();
}
/**
* Gives a Set of all the values of defined attribute names.
*/
public Set keySet()
{
return map.keySet();
}
/**
* Adds or replaces a attribute name/value pair.
* XXX - What if the name is a string? What if the name is neither a Name
* nor a String? What if the value is not a string?
*
* @param name the name of the attribute
* @param value the (new) value of the attribute
* @return the old value of the attribute or null when there was no old
* attribute with this name
*/
public Object put(Object name, Object value)
{
return map.put(name, value);
}
/**
* Adds or replaces all attribute name/value pairs from another
* Attributes object to this one. The supplied Map must be an instance of
* Attributes.
*
* @param attr the Attributes object to merge with this one
* @exception ClassCastException if the supplied map is not an instance of
* Attributes
*/
public void putAll(Map attr)
{
if (!(attr instanceof Attributes))
{
throw new
ClassCastException("Supplied Map is not an instance of Attributes");
}
map.putAll(attr);
}
/**
* Remove a attribute name/value pair.
* XXX - What if the name is a String?
*
* @param name the name of the attribute name/value pair to remove
* @return the old value of the attribute or null if the attribute didn't
* exist
*/
public Object remove(Object name)
{
return map.remove(name);
}
/**
* Returns the number of defined attribute name/value pairs.
*/
public int size()
{
return map.size();
}
/**
* Returns all the values of the defined attribute name/value pairs as a
* Collection.
*/
public Collection values()
{
return map.values();
}
} }
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -43,97 +43,112 @@ import java.util.zip.ZipEntry; ...@@ -43,97 +43,112 @@ import java.util.zip.ZipEntry;
* @since 1.2 * @since 1.2
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class JarEntry extends ZipEntry { public class JarEntry extends ZipEntry
{
// (Packge local) fields // (Packge local) fields
Attributes attr; Attributes attr;
Certificate certs[]; Certificate certs[];
// Constructors // Constructors
/** /**
* Creates a new JarEntry with the specified name and no attributes or * Creates a new JarEntry with the specified name and no attributes or
* or certificates. Calls <code>super(name)</code> so all other (zip)entry * or certificates. Calls <code>super(name)</code> so all other (zip)entry
* fields are null or -1. * fields are null or -1.
* *
* @param name the name of the new jar entry * @param name the name of the new jar entry
* @exception NullPointerException when the supplied name is null * @exception NullPointerException when the supplied name is null
* @exception IllegalArgumentException when the supplied name is longer * @exception IllegalArgumentException when the supplied name is longer
* than 65535 bytes * than 65535 bytes
*/ */
public JarEntry(String name) throws NullPointerException, public JarEntry(String name) throws NullPointerException,
IllegalArgumentException { IllegalArgumentException
super(name); {
attr = null; super(name);
certs = null; attr = null;
} certs = null;
}
/**
* Creates a new JarEntry with the specified ZipEntry as template for /**
* all properties of the entry. Both attributes and certificates will be * Creates a new JarEntry with the specified ZipEntry as template for
* null. * all properties of the entry. Both attributes and certificates will be
* * null.
* @param entry the ZipEntry whose fields should be copied *
*/ * @param entry the ZipEntry whose fields should be copied
public JarEntry(ZipEntry entry) { */
super(entry); public JarEntry(ZipEntry entry)
attr = null; {
certs = null; super(entry);
} attr = null;
certs = null;
/** }
* Creates a new JarEntry with the specified JarEntry as template for
* all properties of the entry. /**
* * Creates a new JarEntry with the specified JarEntry as template for
* @param entry the jarEntry whose fields should be copied * all properties of the entry.
*/ *
public JarEntry(JarEntry entry) { * @param entry the jarEntry whose fields should be copied
super(entry); */
try { public JarEntry(JarEntry entry)
attr = entry.getAttributes(); {
} catch(IOException _) {} super(entry);
certs = entry.getCertificates(); try
} {
attr = entry.getAttributes();
// Methods }
catch (IOException _)
/** {
* Returns a copy of the Attributes set for this entry. }
* When no Attributes are set in the manifest null is returned. certs = entry.getCertificates();
* }
* @return a copy of the Attributes set for this entry
* @exception IOException This will never be thrown. It is here for // Methods
* binary compatibility.
*/ /**
public Attributes getAttributes() throws IOException { * Returns a copy of the Attributes set for this entry.
if (attr != null) { * When no Attributes are set in the manifest null is returned.
return (Attributes) attr.clone(); *
} else { * @return a copy of the Attributes set for this entry
return null; * @exception IOException This will never be thrown. It is here for
} * binary compatibility.
} */
public Attributes getAttributes() throws IOException
/** {
* Returns a copy of the certificates set for this entry. if (attr != null)
* When no certificates are set or when not all data of this entry has {
* been read null is returned. return (Attributes) attr.clone();
* <p> }
* To make sure that this call returns a valid value you must read all else
* data from the JarInputStream for this entry. {
* When you don't need the data for an entry but want to know the return null;
* certificates that are set for the entry then you can skip all data by }
* calling <code>skip(entry.getSize())</code> on the JarInputStream for }
* the entry.
* /**
* @return a copy of the certificates set for this entry * Returns a copy of the certificates set for this entry.
*/ * When no certificates are set or when not all data of this entry has
public Certificate[] getCertificates() { * been read null is returned.
if (certs != null) { * <p>
return (Certificate []) certs.clone(); * To make sure that this call returns a valid value you must read all
} else { * data from the JarInputStream for this entry.
return null; * When you don't need the data for an entry but want to know the
} * certificates that are set for the entry then you can skip all data by
} * calling <code>skip(entry.getSize())</code> on the JarInputStream for
* the entry.
*
* @return a copy of the certificates set for this entry
*/
public Certificate[] getCertificates()
{
if (certs != null)
{
return (Certificate[])certs.clone();
}
else
{
return null;
}
}
} }
...@@ -39,27 +39,29 @@ import java.util.zip.ZipException; ...@@ -39,27 +39,29 @@ import java.util.zip.ZipException;
* @since 1.2 * @since 1.2
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class JarException extends ZipException {
// Constructors public class JarException extends ZipException
{
// Constructors
/** /**
* Create a new JarException without a descriptive error message. * Create a new JarException without a descriptive error message.
*/ */
public JarException() { public JarException()
super(); {
} super();
}
/** /**
* Create a new JarException with a descriptive error message indicating * Create a new JarException with a descriptive error message indicating
* what went wrong. This message can later be retrieved by calling the * what went wrong. This message can later be retrieved by calling the
* <code>getMessage()</code> method. * <code>getMessage()</code> method.
* @see java.lang.Throwable@getMessage() * @see java.lang.Throwable@getMessage()
* *
* @param message The descriptive error message * @param message The descriptive error message
*/ */
public JarException(String message) { public JarException(String message)
super(message); {
} super(message);
}
} }
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -47,231 +47,251 @@ import java.util.Enumeration; ...@@ -47,231 +47,251 @@ import java.util.Enumeration;
* @since 1.2 * @since 1.2
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class JarFile extends ZipFile { public class JarFile extends ZipFile
{
// Fields // Fields
/** The name of the manifest entry: META-INF/MANIFEST.MF */
public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
/**
* The manifest of this file, if any, otherwise null.
* Read by the constructor.
*/
private final Manifest manifest;
/** Wether to verify the manifest and all entries */ /** The name of the manifest entry: META-INF/MANIFEST.MF */
private boolean verify; public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
// Constructors /**
* The manifest of this file, if any, otherwise null.
* Read by the constructor.
*/
private final Manifest manifest;
/** /** Wether to verify the manifest and all entries */
* Creates a new JarFile, tries to read the manifest and if the manifest private boolean verify;
* exists verifies it.
*
* @param fileName the name of the file to open
* @exception FileNotFoundException if the fileName cannot be found
* @exception IOException if another IO exception occurs while reading
*/
public JarFile(String fileName) throws FileNotFoundException,
IOException {
this (fileName, true);
}
/** // Constructors
* Creates a new JarFile, tries to read the manifest and if the manifest
* exists and verify is true verfies it.
*
* @param fileName the name of the file to open
* @param verify checks manifest and entries when true and a manifest
* exists, when false no checks are made
* @exception FileNotFoundException if the fileName cannot be found
* @exception IOException if another IO exception occurs while reading
*/
public JarFile(String fileName, boolean verify) throws
FileNotFoundException,
IOException {
super(fileName);
manifest = readManifest();
if (verify)
verify();
}
/** /**
* Creates a new JarFile, tries to read the manifest and if the manifest * Creates a new JarFile, tries to read the manifest and if the manifest
* exists verifies it. * exists verifies it.
* *
* @param file the file to open as a jar file * @param fileName the name of the file to open
* @exception FileNotFoundException if the file does not exits * @exception FileNotFoundException if the fileName cannot be found
* @exception IOException if another IO exception occurs while reading * @exception IOException if another IO exception occurs while reading
*/ */
public JarFile(File file) throws FileNotFoundException, public JarFile(String fileName) throws FileNotFoundException, IOException
IOException { {
this (file, true); this(fileName, true);
} }
/** /**
* Creates a new JarFile, tries to read the manifest and if the manifest * Creates a new JarFile, tries to read the manifest and if the manifest
* exists and verify is true verfies it. * exists and verify is true verfies it.
* *
* @param file the file to open to open as a jar file * @param fileName the name of the file to open
* @param verify checks manifest and entries when true and a manifest * @param verify checks manifest and entries when true and a manifest
* exists, when false no checks are made * exists, when false no checks are made
* @exception FileNotFoundException if file does not exist * @exception FileNotFoundException if the fileName cannot be found
* @exception IOException if another IO exception occurs while reading * @exception IOException if another IO exception occurs while reading
*/ */
public JarFile(File file, boolean verify) throws FileNotFoundException, public JarFile(String fileName, boolean verify) throws
IOException { FileNotFoundException, IOException
super(file); {
manifest = readManifest(); super(fileName);
if (verify) manifest = readManifest();
verify(); if (verify)
} verify();
}
/** /**
* Creates a new JarFile with the indicated mode, tries to read the * Creates a new JarFile, tries to read the manifest and if the manifest
* manifest and if the manifest exists and verify is true verfies it. * exists verifies it.
* *
* @param file the file to open to open as a jar file * @param file the file to open as a jar file
* @param verify checks manifest and entries when true and a manifest * @exception FileNotFoundException if the file does not exits
* exists, when false no checks are made * @exception IOException if another IO exception occurs while reading
* @param mode either ZipFile.OPEN_READ or */
* (ZipFile.OPEN_READ | ZipFile.OPEN_DELETE) public JarFile(File file) throws FileNotFoundException, IOException
* @exception FileNotFoundException if the file does not exist {
* @exception IOException if another IO exception occurs while reading this(file, true);
* @exception IllegalArgumentException when given an illegal mode }
*
* @since 1.3
*/
public JarFile(File file, boolean verify, int mode) throws
FileNotFoundException,
IOException,
IllegalArgumentException {
super(file, mode);
manifest = readManifest();
if (verify)
verify();
}
// Methods /**
* Creates a new JarFile, tries to read the manifest and if the manifest
* exists and verify is true verfies it.
*
* @param file the file to open to open as a jar file
* @param verify checks manifest and entries when true and a manifest
* exists, when false no checks are made
* @exception FileNotFoundException if file does not exist
* @exception IOException if another IO exception occurs while reading
*/
public JarFile(File file, boolean verify) throws FileNotFoundException,
IOException
{
super(file);
manifest = readManifest();
if (verify)
verify();
}
/** /**
* XXX - should verify the manifest file * Creates a new JarFile with the indicated mode, tries to read the
*/ * manifest and if the manifest exists and verify is true verfies it.
private void verify() { *
// only check if manifest is not null * @param file the file to open to open as a jar file
if (manifest == null) { * @param verify checks manifest and entries when true and a manifest
verify = false; * exists, when false no checks are made
return; * @param mode either ZipFile.OPEN_READ or
} * (ZipFile.OPEN_READ | ZipFile.OPEN_DELETE)
* @exception FileNotFoundException if the file does not exist
* @exception IOException if another IO exception occurs while reading
* @exception IllegalArgumentException when given an illegal mode
*
* @since 1.3
*/
public JarFile(File file, boolean verify, int mode) throws
FileNotFoundException, IOException, IllegalArgumentException
{
super(file, mode);
manifest = readManifest();
if (verify)
verify();
}
verify = true; // Methods
// XXX - verify manifest
}
/** /**
* Parses and returns the manifest if it exists, otherwise returns null. * XXX - should verify the manifest file
*/ */
private Manifest readManifest() { private void verify()
try { {
ZipEntry manEntry = super.getEntry(MANIFEST_NAME); // only check if manifest is not null
if (manEntry != null) { if (manifest == null)
InputStream in = super.getInputStream(manEntry); {
return new Manifest(in); verify = false;
} else { return;
return null; }
}
} catch (IOException ioe) {
return null;
}
}
/** verify = true;
* Returns a enumeration of all the entries in the JarFile. // XXX - verify manifest
* Note that also the Jar META-INF entries are returned. }
*
* @exception IllegalStateException when the JarFile is already closed
*/
public Enumeration entries() throws IllegalStateException {
return new JarEnumeration(super.entries());
}
/** /**
* Wraps a given Zip Entries Enumeration. For every zip entry a * Parses and returns the manifest if it exists, otherwise returns null.
* JarEntry is created and the corresponding Attributes are looked up. */
* XXX - Should also look up the certificates. private Manifest readManifest()
*/ {
private class JarEnumeration implements Enumeration { try
{
ZipEntry manEntry = super.getEntry(MANIFEST_NAME);
if (manEntry != null)
{
InputStream in = super.getInputStream(manEntry);
return new Manifest(in);
}
else
{
return null;
}
}
catch (IOException ioe)
{
return null;
}
}
private final Enumeration entries; /**
* Returns a enumeration of all the entries in the JarFile.
* Note that also the Jar META-INF entries are returned.
*
* @exception IllegalStateException when the JarFile is already closed
*/
public Enumeration entries() throws IllegalStateException
{
return new JarEnumeration(super.entries());
}
JarEnumeration(Enumeration e) { /**
entries = e; * Wraps a given Zip Entries Enumeration. For every zip entry a
} * JarEntry is created and the corresponding Attributes are looked up.
* XXX - Should also look up the certificates.
*/
private class JarEnumeration implements Enumeration
{
public boolean hasMoreElements() { private final Enumeration entries;
return entries.hasMoreElements();
}
public Object nextElement() { JarEnumeration(Enumeration e)
ZipEntry zip = (ZipEntry) entries.nextElement(); {
JarEntry jar = new JarEntry(zip); entries = e;
if (manifest != null) {
jar.attr = manifest.getAttributes(jar.getName());
}
// XXX jar.certs
return jar;
}
} }
/** public boolean hasMoreElements()
* XXX {
* It actually returns a JarEntry not a zipEntry return entries.hasMoreElements();
* @param name XXX
*/
public ZipEntry getEntry(String name) {
ZipEntry entry = super.getEntry(name);
if (entry != null) {
JarEntry jarEntry = new JarEntry(entry);
if (manifest != null) {
jarEntry.attr = manifest.getAttributes(name);
// XXX jarEntry.certs
}
return jarEntry;
}
return null;
} }
/** public Object nextElement()
* XXX should verify the inputstream {
* @param entry XXX ZipEntry zip = (ZipEntry) entries.nextElement();
* @exception ZipException XXX JarEntry jar = new JarEntry(zip);
* @exception IOException XXX if (manifest != null)
*/ {
public synchronized InputStream getInputStream(ZipEntry entry) throws jar.attr = manifest.getAttributes(jar.getName());
ZipException, }
IOException { // XXX jar.certs
return super.getInputStream(entry); // XXX verify return jar;
} }
}
/** /**
* Returns the JarEntry that belongs to the name if such an entry * XXX
* exists in the JarFile. Returns null otherwise * It actually returns a JarEntry not a zipEntry
* Convenience method that just casts the result from <code>getEntry</code> * @param name XXX
* to a JarEntry. */
* public ZipEntry getEntry(String name)
* @param name the jar entry name to look up {
* @return the JarEntry if it exists, null otherwise ZipEntry entry = super.getEntry(name);
*/ if (entry != null)
public JarEntry getJarEntry(String name) { {
return (JarEntry)getEntry(name); JarEntry jarEntry = new JarEntry(entry);
} if (manifest != null)
{
jarEntry.attr = manifest.getAttributes(name);
// XXX jarEntry.certs
}
return jarEntry;
}
return null;
}
/** /**
* Returns the manifest for this JarFile or null when the JarFile does not * XXX should verify the inputstream
* contain a manifest file. * @param entry XXX
*/ * @exception ZipException XXX
public Manifest getManifest() { * @exception IOException XXX
return manifest; */
} public synchronized InputStream getInputStream(ZipEntry entry) throws
ZipException, IOException
{
return super.getInputStream(entry); // XXX verify
}
/**
* Returns the JarEntry that belongs to the name if such an entry
* exists in the JarFile. Returns null otherwise
* Convenience method that just casts the result from <code>getEntry</code>
* to a JarEntry.
*
* @param name the jar entry name to look up
* @return the JarEntry if it exists, null otherwise
*/
public JarEntry getJarEntry(String name)
{
return (JarEntry) getEntry(name);
}
/**
* Returns the manifest for this JarFile or null when the JarFile does not
* contain a manifest file.
*/
public Manifest getManifest()
{
return manifest;
}
} }
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -39,137 +39,152 @@ import java.util.zip.ZipInputStream; ...@@ -39,137 +39,152 @@ import java.util.zip.ZipInputStream;
* @since 1.2 * @since 1.2
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class JarInputStream extends ZipInputStream { public class JarInputStream extends ZipInputStream
{
// Fields // Fields
/** The manifest for this file or null when there was no manifest. */ /** The manifest for this file or null when there was no manifest. */
private Manifest manifest; private Manifest manifest;
/** The first real JarEntry for this file. Used by readManifest() to store /** The first real JarEntry for this file. Used by readManifest() to store
an entry that isn't the manifest but that should be returned by an entry that isn't the manifest but that should be returned by
getNextEntry next time it is called. Null when no firstEntry was read getNextEntry next time it is called. Null when no firstEntry was read
while searching for the manifest entry, or when it has already been while searching for the manifest entry, or when it has already been
returned by getNextEntry(). */ returned by getNextEntry(). */
private JarEntry firstEntry; private JarEntry firstEntry;
// Constructors // Constructors
/** /**
* Creates a new JarInputStream and tries to read the manifest. * Creates a new JarInputStream and tries to read the manifest.
* If such a manifest is present the JarInputStream tries to verify all * If such a manifest is present the JarInputStream tries to verify all
* the entry signatures while reading. * the entry signatures while reading.
* *
* @param in InputStream to read the jar from * @param in InputStream to read the jar from
* @exception IOException when an error occurs when opening or reading * @exception IOException when an error occurs when opening or reading
*/ */
public JarInputStream(InputStream in) throws IOException { public JarInputStream(InputStream in) throws IOException
this(in, true); {
} this(in, true);
}
/**
* Creates a new JarInputStream and tries to read the manifest. /**
* If such a manifest is present and verify is true, the JarInputStream * Creates a new JarInputStream and tries to read the manifest.
* tries to verify all the entry signatures while reading. * If such a manifest is present and verify is true, the JarInputStream
* * tries to verify all the entry signatures while reading.
* @param in InputStream to read the jar from *
* @param verify wheter or not to verify the manifest entries * @param in InputStream to read the jar from
* @exception IOException when an error occurs when opening or reading * @param verify wheter or not to verify the manifest entries
*/ * @exception IOException when an error occurs when opening or reading
public JarInputStream(InputStream in, boolean verify) throws IOException { */
super(in); public JarInputStream(InputStream in, boolean verify) throws IOException
readManifest(verify); {
} super(in);
readManifest(verify);
// Methods }
/** // Methods
* Set the manifest if found. Skips all entries that start with "META-INF/"
* /**
* @param verify when true (and a Manifest is found) checks the Manifest, * Set the manifest if found. Skips all entries that start with "META-INF/"
* when false no check is performed *
* @exception IOException if an error occurs while reading * @param verify when true (and a Manifest is found) checks the Manifest,
*/ * when false no check is performed
private void readManifest(boolean verify) throws IOException { * @exception IOException if an error occurs while reading
firstEntry = (JarEntry) super.getNextEntry(); */
while ((firstEntry != null) && private void readManifest(boolean verify) throws IOException
firstEntry.getName().startsWith("META-INF/")) { {
if(firstEntry.getName().equals(JarFile.MANIFEST_NAME)) { firstEntry = (JarEntry) super.getNextEntry();
manifest = new Manifest(this); while ((firstEntry != null) &&
} firstEntry.getName().startsWith("META-INF/"))
firstEntry = (JarEntry) super.getNextEntry(); {
} if (firstEntry.getName().equals(JarFile.MANIFEST_NAME))
closeEntry(); {
manifest = new Manifest(this);
if (verify) { }
// XXX firstEntry = (JarEntry) super.getNextEntry();
} }
} closeEntry();
/** if (verify)
* Creates a JarEntry for a particular name and consults the manifest {
* for the Attributes of the entry. // XXX
* Used by <code>ZipEntry.getNextEntry()</code> }
* }
* @param name the name of the new entry
*/ /**
protected ZipEntry createZipEntry(String name) { * Creates a JarEntry for a particular name and consults the manifest
ZipEntry zipEntry = super.createZipEntry(name); * for the Attributes of the entry.
JarEntry jarEntry = new JarEntry(zipEntry); * Used by <code>ZipEntry.getNextEntry()</code>
if (manifest != null) { *
jarEntry.attr = manifest.getAttributes(name); * @param name the name of the new entry
} */
return jarEntry; protected ZipEntry createZipEntry(String name)
} {
ZipEntry zipEntry = super.createZipEntry(name);
/** JarEntry jarEntry = new JarEntry(zipEntry);
* Returns the Manifest for the jar file or null if there was no Manifest. if (manifest != null)
*/ {
public Manifest getManifest() { jarEntry.attr = manifest.getAttributes(name);
return manifest; }
} return jarEntry;
}
/**
* Returns the next entry or null when there are no more entries. /**
* Does actually return a JarEntry, if you don't want to cast it yourself * Returns the Manifest for the jar file or null if there was no Manifest.
* use <code>getNextJarEntry()</code>. Does not return any entries found */
* at the beginning of the ZipFile that are special public Manifest getManifest()
* (those that start with "META-INF/"). {
* return manifest;
* @exception IOException if an IO error occurs when reading the entry }
*/
public ZipEntry getNextEntry() throws IOException { /**
ZipEntry entry; * Returns the next entry or null when there are no more entries.
if (firstEntry != null) { * Does actually return a JarEntry, if you don't want to cast it yourself
entry = firstEntry; * use <code>getNextJarEntry()</code>. Does not return any entries found
firstEntry = null; * at the beginning of the ZipFile that are special
} else { * (those that start with "META-INF/").
entry = super.getNextEntry(); *
} * @exception IOException if an IO error occurs when reading the entry
return entry; */
} public ZipEntry getNextEntry() throws IOException
{
/** ZipEntry entry;
* Returns the next jar entry or null when there are no more entries. if (firstEntry != null)
* {
* @exception IOException if an IO error occurs when reading the entry entry = firstEntry;
*/ firstEntry = null;
public JarEntry getNextJarEntry() throws IOException { }
return (JarEntry)getNextEntry(); else
} {
entry = super.getNextEntry();
/** }
* XXX return entry;
* }
* @param buf XXX
* @param off XXX /**
* @param len XXX * Returns the next jar entry or null when there are no more entries.
* @return XXX *
* @exception IOException XXX * @exception IOException if an IO error occurs when reading the entry
*/ */
public int read(byte[] buf, int off, int len) throws IOException { public JarEntry getNextJarEntry() throws IOException
// XXX if (verify) {} {
return super.read(buf, off, len); return (JarEntry) getNextEntry();
} }
/**
* XXX
*
* @param buf XXX
* @param off XXX
* @param len XXX
* @return XXX
* @exception IOException XXX
*/
public int read(byte[]buf, int off, int len) throws IOException
{
// XXX if (verify) {}
return super.read(buf, off, len);
}
} }
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -38,61 +38,65 @@ import java.util.zip.ZipOutputStream; ...@@ -38,61 +38,65 @@ import java.util.zip.ZipOutputStream;
* *
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class JarOutputStream extends ZipOutputStream {
// Constructors public class JarOutputStream extends ZipOutputStream
{
// Constructors
/** /**
* Creates a new JarOutputStream without a manifest entry. * Creates a new JarOutputStream without a manifest entry.
* *
* @param out the stream to create the new jar on * @param out the stream to create the new jar on
* @exception IOException if something unexpected happend * @exception IOException if something unexpected happend
*/ */
public JarOutputStream(OutputStream out) throws IOException { public JarOutputStream(OutputStream out) throws IOException
this(out, null); {
} this(out, null);
}
/** /**
* Creates a new JarOutputStream with a manifest entry. * Creates a new JarOutputStream with a manifest entry.
* The manifest will be the first entry in the jar. * The manifest will be the first entry in the jar.
* *
* @param out the stream to create the new jar on * @param out the stream to create the new jar on
* @param man the manifest that should be put in the jar file or null * @param man the manifest that should be put in the jar file or null
* for no manifest entry * for no manifest entry
* @exception IOException if something unexpected happend * @exception IOException if something unexpected happend
*/ */
public JarOutputStream(OutputStream out, Manifest man) throws IOException { public JarOutputStream(OutputStream out, Manifest man) throws IOException
super(out); {
if (man != null) super(out);
writeManifest(man); if (man != null)
} writeManifest(man);
}
// Methods // Methods
/** /**
* Writes the manifest to a new JarEntry in this JarOutputStream with as * Writes the manifest to a new JarEntry in this JarOutputStream with as
* name JarFile.MANIFEST_NAME. * name JarFile.MANIFEST_NAME.
* *
* @param manifest the non null manifest to be written * @param manifest the non null manifest to be written
* @exception IOException if something unexpected happend * @exception IOException if something unexpected happend
*/ */
private void writeManifest(Manifest manifest) throws IOException { private void writeManifest(Manifest manifest) throws IOException
// Create a new Jar Entry for the Manifest {
JarEntry entry = new JarEntry(JarFile.MANIFEST_NAME); // Create a new Jar Entry for the Manifest
putNextEntry(entry); JarEntry entry = new JarEntry(JarFile.MANIFEST_NAME);
manifest.write(this); putNextEntry(entry);
closeEntry(); manifest.write(this);
} closeEntry();
}
/** /**
* Prepares the JarOutputStream for writing the next entry. * Prepares the JarOutputStream for writing the next entry.
* This implementation just calls <code>super.putNextEntre()</code>. * This implementation just calls <code>super.putNextEntre()</code>.
* *
* @param entry The information for the next entry * @param entry The information for the next entry
* @exception IOException when some unexpected I/O exception occured * @exception IOException when some unexpected I/O exception occured
*/ */
public void putNextEntry(ZipEntry entry) throws IOException { public void putNextEntry(ZipEntry entry) throws IOException
super.putNextEntry(entry); // XXX {
} super.putNextEntry(entry); // XXX
}
} }
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -48,359 +48,408 @@ import java.util.Set; ...@@ -48,359 +48,408 @@ import java.util.Set;
* @since 1.2 * @since 1.2
* @author Mark Wielaard (mark@klomp.org) * @author Mark Wielaard (mark@klomp.org)
*/ */
public class Manifest implements Cloneable { public class Manifest implements Cloneable
{
// Fields // Fields
/** The main attributes of the manifest (jar file). */ /** The main attributes of the manifest (jar file). */
private final Attributes mainAttr; private final Attributes mainAttr;
/** A map of atrributes for all entries described in this Manifest. */ /** A map of atrributes for all entries described in this Manifest. */
private final Map entries; private final Map entries;
// Constructors // Constructors
/** /**
* Creates a new empty Manifest. * Creates a new empty Manifest.
*/ */
public Manifest() { public Manifest()
mainAttr = new Attributes(); {
entries = new Hashtable(); mainAttr = new Attributes();
} entries = new Hashtable();
}
/**
* Creates a Manifest from the supplied input stream. /**
* * Creates a Manifest from the supplied input stream.
* @see read(Inputstream) *
* @see write(OutputStream) * @see read(Inputstream)
* * @see write(OutputStream)
* @param InputStream the input stream to read the manifest from *
* @exception IOException when an i/o exception occurs or the input stream * @param InputStream the input stream to read the manifest from
* does not describe a valid manifest * @exception IOException when an i/o exception occurs or the input stream
*/ * does not describe a valid manifest
public Manifest(InputStream in) throws IOException { */
this(); public Manifest(InputStream in) throws IOException
read(in); {
} this();
read(in);
/** }
* Creates a Manifest from another Manifest.
* Makes a deep copy of the main attributes, but a shallow copy of /**
* the other entries. This means that you can freely add, change or remove * Creates a Manifest from another Manifest.
* the main attributes or the entries of the new manifest without effecting * Makes a deep copy of the main attributes, but a shallow copy of
* the original manifest, but adding, changing or removing attributes from * the other entries. This means that you can freely add, change or remove
* a particular entry also changes the attributes of that entry in the * the main attributes or the entries of the new manifest without effecting
* original manifest. * the original manifest, but adding, changing or removing attributes from
* * a particular entry also changes the attributes of that entry in the
* @see clone() * original manifest.
* @param man the Manifest to copy from *
*/ * @see clone()
public Manifest (Manifest man) { * @param man the Manifest to copy from
mainAttr = new Attributes(man.getMainAttributes()); */
entries = new Hashtable(man.getEntries()); public Manifest(Manifest man)
} {
mainAttr = new Attributes(man.getMainAttributes());
// Methods entries = new Hashtable(man.getEntries());
}
/**
* Gets the main attributes of this Manifest. // Methods
*/
public Attributes getMainAttributes() { /**
return mainAttr; * Gets the main attributes of this Manifest.
} */
public Attributes getMainAttributes()
/** {
* Gets a map of entry Strings to Attributes for all the entries described return mainAttr;
* in this manifest. Adding, changing or removing from this entries map }
* changes the entries of this manifest.
*/ /**
public Map getEntries() { * Gets a map of entry Strings to Attributes for all the entries described
return entries; * in this manifest. Adding, changing or removing from this entries map
} * changes the entries of this manifest.
*/
/** public Map getEntries()
* Returns the Attributes associated with the Entry. {
* <p> return entries;
* Implemented as: }
* <code>return (Attributes)getEntries().get(entryName)</code>
* /**
* @param entryName the name of the entry to look up * Returns the Attributes associated with the Entry.
* @return the attributes associated with the entry or null when none * <p>
*/ * Implemented as:
public Attributes getAttributes(String entryName) { * <code>return (Attributes)getEntries().get(entryName)</code>
return (Attributes)getEntries().get(entryName); *
} * @param entryName the name of the entry to look up
* @return the attributes associated with the entry or null when none
/** */
* Clears the main attributes and removes all the entries from the public Attributes getAttributes(String entryName)
* manifest. {
*/ return (Attributes) getEntries().get(entryName);
public void clear() { }
mainAttr.clear();
entries.clear(); /**
} * Clears the main attributes and removes all the entries from the
* manifest.
/** */
* XXX public void clear()
*/ {
public void read(InputStream in) throws IOException { mainAttr.clear();
BufferedReader br = new BufferedReader( entries.clear();
new InputStreamReader(in, "8859_1")); }
read_main_section(getMainAttributes(), br);
read_individual_sections(getEntries(), br); /**
} * XXX
*/
// Private Static methods for reading the Manifest file from BufferedReader public void read(InputStream in) throws IOException
{
private static void read_main_section(Attributes attr, BufferedReader br =
BufferedReader br) throws new BufferedReader(new InputStreamReader(in, "8859_1"));
IOException { read_main_section(getMainAttributes(), br);
read_version_info(attr, br); read_individual_sections(getEntries(), br);
read_attributes(attr, br); }
}
// Private Static methods for reading the Manifest file from BufferedReader
private static void read_version_info(Attributes attr,
BufferedReader br) throws private static void read_main_section(Attributes attr,
IOException { BufferedReader br) throws IOException
String version_header = Attributes.Name.MANIFEST_VERSION.toString(); {
try { read_version_info(attr, br);
String value = expect_header(version_header, br); read_attributes(attr, br);
attr.putValue(version_header, value); }
} catch (IOException ioe) {
throw new JarException( private static void read_version_info(Attributes attr,
"Manifest should start with a " + version_header BufferedReader br) throws IOException
+ ": " + ioe.getMessage()); {
} String version_header = Attributes.Name.MANIFEST_VERSION.toString();
} try
{
private static String expect_header(String header, BufferedReader br) String value = expect_header(version_header, br);
throws IOException { attr.putValue(version_header, value);
}
String s = br.readLine(); catch (IOException ioe)
if (s == null) { {
throw new JarException("unexpected end of file"); throw new JarException("Manifest should start with a " +
} version_header + ": " + ioe.getMessage());
return expect_header(header, br, s); }
} }
private static String expect_header(String header, BufferedReader br, private static String expect_header(String header, BufferedReader br)
String s) throws IOException { throws IOException
try { {
String name = s.substring(0, header.length() + 1); String s = br.readLine();
if (name.equalsIgnoreCase(header + ":")) { if (s == null)
String value_start = s.substring(header.length() + 2); {
return read_header_value(value_start, br); throw new JarException("unexpected end of file");
} }
} catch (IndexOutOfBoundsException iobe) {} return expect_header(header, br, s);
// If we arrive here, something went wrong }
throw new JarException("unexpected '" + s + "'");
} private static String expect_header(String header, BufferedReader br,
String s) throws IOException
private static String read_header_value(String s, BufferedReader br) {
throws IOException { try
boolean try_next = true; {
while (try_next) { String name = s.substring(0, header.length() + 1);
// Lets see if there is something on the next line if (name.equalsIgnoreCase(header + ":"))
br.mark(1); {
if (br.read() == ' ') { String value_start = s.substring(header.length() + 2);
s += br.readLine(); return read_header_value(value_start, br);
} else { }
br.reset(); }
try_next = false; catch (IndexOutOfBoundsException iobe)
} {
} }
return s; // If we arrive here, something went wrong
} throw new JarException("unexpected '" + s + "'");
}
private static void read_attributes(Attributes attr,
BufferedReader br) throws private static String read_header_value(String s, BufferedReader br)
IOException { throws IOException
String s = br.readLine(); {
while (s != null && (!s.equals(""))) { boolean try_next = true;
read_attribute(attr, s, br); while (try_next)
s = br.readLine(); {
} // Lets see if there is something on the next line
} br.mark(1);
if (br.read() == ' ')
private static void read_attribute(Attributes attr, String s, {
BufferedReader br) throws IOException { s += br.readLine();
try { }
int colon = s.indexOf(": "); else
String name = s.substring(0, colon); {
String value_start = s.substring(colon+2); br.reset();
String value = read_header_value(value_start, br); try_next = false;
attr.putValue(name, value); }
} catch (IndexOutOfBoundsException iobe) { }
throw new JarException( return s;
"Manifest contains a bad header: " + s); }
}
} private static void read_attributes(Attributes attr,
BufferedReader br) throws IOException
private static void read_individual_sections(Map entries, {
BufferedReader br) throws String s = br.readLine();
IOException { while (s != null && (!s.equals("")))
String s = br.readLine(); {
while (s != null && (!s.equals(""))) { read_attribute(attr, s, br);
Attributes attr = read_section_name(s, br, entries); s = br.readLine();
read_attributes(attr, br); }
s = br.readLine(); }
}
} private static void read_attribute(Attributes attr, String s,
BufferedReader br) throws IOException
private static Attributes read_section_name(String s, BufferedReader br, {
Map entries) throws try
JarException { {
try { int colon = s.indexOf(": ");
String name = expect_header("Name", br, s); String name = s.substring(0, colon);
Attributes attr = new Attributes(); String value_start = s.substring(colon + 2);
entries.put(name, attr); String value = read_header_value(value_start, br);
return attr; attr.putValue(name, value);
} catch(IOException ioe) { }
throw new JarException catch (IndexOutOfBoundsException iobe)
("Section should start with a Name header: " {
+ ioe.getMessage()); throw new JarException("Manifest contains a bad header: " + s);
} }
} }
/** private static void read_individual_sections(Map entries,
* XXX BufferedReader br) throws
*/ IOException
public void write(OutputStream out) throws IOException { {
PrintWriter pw = new PrintWriter( String s = br.readLine();
new BufferedWriter( while (s != null && (!s.equals("")))
new OutputStreamWriter(out, "8859_1"))); {
write_main_section(getMainAttributes(), pw); Attributes attr = read_section_name(s, br, entries);
pw.println(); read_attributes(attr, br);
write_individual_sections(getEntries(), pw); s = br.readLine();
if (pw.checkError()) { }
throw new JarException("Error while writing manifest"); }
}
} private static Attributes read_section_name(String s, BufferedReader br,
Map entries) throws JarException
// Private Static functions for writing the Manifest file to a PrintWriter {
try
private static void write_main_section(Attributes attr, {
PrintWriter pw) String name = expect_header("Name", br, s);
throws JarException { Attributes attr = new Attributes();
entries.put(name, attr);
write_version_info(attr, pw); return attr;
write_main_attributes(attr, pw); }
} catch (IOException ioe)
{
private static void write_version_info(Attributes attr, PrintWriter pw) { throw new JarException
// First check if there is already a version attribute set ("Section should start with a Name header: " + ioe.getMessage());
String version = attr.getValue(Attributes.Name.MANIFEST_VERSION); }
if (version == null) { }
version = "1.0";
} /**
write_header(Attributes.Name.MANIFEST_VERSION.toString(), version, pw); * XXX
} */
public void write(OutputStream out) throws IOException
private static void write_header(String name, String value, {
PrintWriter pw) { PrintWriter pw =
pw.print(name + ": "); new PrintWriter(new
BufferedWriter(new OutputStreamWriter(out, "8859_1")));
int last = 68 - name.length(); write_main_section(getMainAttributes(), pw);
if (last > value.length()) { pw.println();
pw.println(value); write_individual_sections(getEntries(), pw);
} else { if (pw.checkError())
pw.println(value.substring(0, last)); {
} throw new JarException("Error while writing manifest");
while (last < value.length()) { }
pw.print(" "); }
int end = (last + 69);
if (end > value.length()) { // Private Static functions for writing the Manifest file to a PrintWriter
pw.println(value.substring(last));
} else { private static void write_main_section(Attributes attr,
pw.println(value.substring(last, end)); PrintWriter pw) throws JarException
} {
last = end; write_version_info(attr, pw);
} write_main_attributes(attr, pw);
} }
private static void write_main_attributes(Attributes attr, private static void write_version_info(Attributes attr, PrintWriter pw)
PrintWriter pw) throws {
JarException { // First check if there is already a version attribute set
Iterator it = attr.entrySet().iterator(); String version = attr.getValue(Attributes.Name.MANIFEST_VERSION);
while(it.hasNext()) { if (version == null)
Map.Entry entry = (Map.Entry)it.next(); {
// Don't print the manifest version again version = "1.0";
if (!Attributes.Name.MANIFEST_VERSION.equals(entry.getKey())) { }
write_attribute_entry(entry, pw); write_header(Attributes.Name.MANIFEST_VERSION.toString(), version, pw);
} }
}
} private static void write_header(String name, String value, PrintWriter pw)
{
private static void write_attribute_entry(Map.Entry entry, pw.print(name + ": ");
PrintWriter pw) throws
JarException { int last = 68 - name.length();
String name = entry.getKey().toString(); if (last > value.length())
String value = entry.getValue().toString(); {
pw.println(value);
if (name.equalsIgnoreCase("Name")) { }
throw new JarException("Attributes cannot be called 'Name'"); else
} {
if (name.startsWith("From")) { pw.println(value.substring(0, last));
throw new JarException( }
"Header cannot start with the four letters 'From'" while (last < value.length())
+ name); {
} pw.print(" ");
write_header(name, value, pw); int end = (last + 69);
} if (end > value.length())
{
private static void write_individual_sections(Map entries, pw.println(value.substring(last));
PrintWriter pw) }
throws JarException { else
{
Iterator it = entries.entrySet().iterator(); pw.println(value.substring(last, end));
while (it.hasNext()) { }
Map.Entry entry = (Map.Entry)it.next(); last = end;
write_header("Name", entry.getKey().toString(), pw); }
write_entry_attributes((Attributes)entry.getValue(), pw); }
pw.println();
} private static void write_main_attributes(Attributes attr, PrintWriter pw)
} throws JarException
{
private static void write_entry_attributes(Attributes attr, Iterator it = attr.entrySet().iterator();
PrintWriter pw) throws while (it.hasNext())
JarException { {
Iterator it = attr.entrySet().iterator(); Map.Entry entry = (Map.Entry) it.next();
while(it.hasNext()) { // Don't print the manifest version again
Map.Entry entry = (Map.Entry)it.next(); if (!Attributes.Name.MANIFEST_VERSION.equals(entry.getKey()))
write_attribute_entry(entry, pw); {
} write_attribute_entry(entry, pw);
} }
}
/** }
* Makes a deep copy of the main attributes, but a shallow copy of
* the other entries. This means that you can freely add, change or remove private static void write_attribute_entry(Map.Entry entry, PrintWriter pw)
* the main attributes or the entries of the new manifest without effecting throws JarException
* the original manifest, but adding, changing or removing attributes from {
* a particular entry also changes the attributes of that entry in the String name = entry.getKey().toString();
* original manifest. Calls <CODE>new Manifest(this)</CODE>. String value = entry.getValue().toString();
*/
public Object clone() { if (name.equalsIgnoreCase("Name"))
return new Manifest(this); {
} throw new JarException("Attributes cannot be called 'Name'");
}
/** if (name.startsWith("From"))
* Checks if another object is equal to this Manifest object. {
* Another Object is equal to this Manifest object if it is an instance of throw new
* Manifest and the main attributes and the entries of the other manifest JarException("Header cannot start with the four letters 'From'" +
* are equal to this one. name);
*/ }
public boolean equals(Object o) { write_header(name, value, pw);
return (o instanceof Manifest) && }
(mainAttr.equals(((Manifest)o).mainAttr)) &&
(entries.equals(((Manifest)o).entries)); private static void write_individual_sections(Map entries, PrintWriter pw)
} throws JarException
{
/**
* Calculates the hash code of the manifest. Implemented by a xor of the Iterator it = entries.entrySet().iterator();
* hash code of the main attributes with the hash code of the entries map. while (it.hasNext())
*/ {
public int hashCode() { Map.Entry entry = (Map.Entry) it.next();
return mainAttr.hashCode() ^ entries.hashCode(); write_header("Name", entry.getKey().toString(), pw);
} write_entry_attributes((Attributes) entry.getValue(), pw);
pw.println();
}
}
private static void write_entry_attributes(Attributes attr, PrintWriter pw)
throws JarException
{
Iterator it = attr.entrySet().iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
write_attribute_entry(entry, pw);
}
}
/**
* Makes a deep copy of the main attributes, but a shallow copy of
* the other entries. This means that you can freely add, change or remove
* the main attributes or the entries of the new manifest without effecting
* the original manifest, but adding, changing or removing attributes from
* a particular entry also changes the attributes of that entry in the
* original manifest. Calls <CODE>new Manifest(this)</CODE>.
*/
public Object clone()
{
return new Manifest(this);
}
/**
* Checks if another object is equal to this Manifest object.
* Another Object is equal to this Manifest object if it is an instance of
* Manifest and the main attributes and the entries of the other manifest
* are equal to this one.
*/
public boolean equals(Object o)
{
return (o instanceof Manifest) &&
(mainAttr.equals(((Manifest) o).mainAttr)) &&
(entries.equals(((Manifest) o).entries));
}
/**
* Calculates the hash code of the manifest. Implemented by a xor of the
* hash code of the main attributes with the hash code of the entries map.
*/
public int hashCode()
{
return mainAttr.hashCode() ^ entries.hashCode();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment