src/share/classes/java/util/jar/Manifest.java

Print this page
rev 4802 : 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
Reviewed-by: alanb, dholmes, forax, sherman, smarks
Contributed-by: London Java Community and Michael Barker <mikeb01@gmail.com>


  34 import java.util.HashMap;
  35 import java.util.Iterator;
  36 
  37 /**
  38  * The Manifest class is used to maintain Manifest entry names and their
  39  * associated Attributes. There are main Manifest Attributes as well as
  40  * per-entry Attributes. For information on the Manifest format, please
  41  * see the
  42  * <a href="../../../../technotes/guides/jar/jar.html">
  43  * Manifest format specification</a>.
  44  *
  45  * @author  David Connelly
  46  * @see     Attributes
  47  * @since   1.2
  48  */
  49 public class Manifest implements Cloneable {
  50     // manifest main attributes
  51     private Attributes attr = new Attributes();
  52 
  53     // manifest entries
  54     private Map entries = new HashMap();
  55 
  56     /**
  57      * Constructs a new, empty Manifest.
  58      */
  59     public Manifest() {
  60     }
  61 
  62     /**
  63      * Constructs a new Manifest from the specified input stream.
  64      *
  65      * @param is the input stream containing manifest data
  66      * @throws IOException if an I/O error has occured
  67      */
  68     public Manifest(InputStream is) throws IOException {
  69         read(is);
  70     }
  71 
  72     /**
  73      * Constructs a new Manifest that is a copy of the specified Manifest.
  74      *


 131      */
 132     public void clear() {
 133         attr.clear();
 134         entries.clear();
 135     }
 136 
 137     /**
 138      * Writes the Manifest to the specified OutputStream.
 139      * Attributes.Name.MANIFEST_VERSION must be set in
 140      * MainAttributes prior to invoking this method.
 141      *
 142      * @param out the output stream
 143      * @exception IOException if an I/O error has occurred
 144      * @see #getMainAttributes
 145      */
 146     public void write(OutputStream out) throws IOException {
 147         DataOutputStream dos = new DataOutputStream(out);
 148         // Write out the main attributes for the manifest
 149         attr.writeMain(dos);
 150         // Now write out the pre-entry attributes
 151         Iterator it = entries.entrySet().iterator();
 152         while (it.hasNext()) {
 153             Map.Entry e = (Map.Entry)it.next();
 154             StringBuffer buffer = new StringBuffer("Name: ");
 155             String value = (String)e.getKey();
 156             if (value != null) {
 157                 byte[] vb = value.getBytes("UTF8");
 158                 value = new String(vb, 0, 0, vb.length);
 159             }
 160             buffer.append(value);
 161             buffer.append("\r\n");
 162             make72Safe(buffer);
 163             dos.writeBytes(buffer.toString());
 164             ((Attributes)e.getValue()).write(dos);
 165         }
 166         dos.flush();
 167     }
 168 
 169     /**
 170      * Adds line breaks to enforce a maximum 72 bytes per line.
 171      */
 172     static void make72Safe(StringBuffer line) {
 173         int length = line.length();
 174         if (length > 72) {
 175             int index = 70;
 176             while (index < length - 2) {
 177                 line.insert(index, "\r\n ");
 178                 index += 72;
 179                 length += 3;
 180             }
 181         }
 182         return;
 183     }
 184 




  34 import java.util.HashMap;
  35 import java.util.Iterator;
  36 
  37 /**
  38  * The Manifest class is used to maintain Manifest entry names and their
  39  * associated Attributes. There are main Manifest Attributes as well as
  40  * per-entry Attributes. For information on the Manifest format, please
  41  * see the
  42  * <a href="../../../../technotes/guides/jar/jar.html">
  43  * Manifest format specification</a>.
  44  *
  45  * @author  David Connelly
  46  * @see     Attributes
  47  * @since   1.2
  48  */
  49 public class Manifest implements Cloneable {
  50     // manifest main attributes
  51     private Attributes attr = new Attributes();
  52 
  53     // manifest entries
  54     private Map<String, Attributes> entries = new HashMap<>();
  55 
  56     /**
  57      * Constructs a new, empty Manifest.
  58      */
  59     public Manifest() {
  60     }
  61 
  62     /**
  63      * Constructs a new Manifest from the specified input stream.
  64      *
  65      * @param is the input stream containing manifest data
  66      * @throws IOException if an I/O error has occured
  67      */
  68     public Manifest(InputStream is) throws IOException {
  69         read(is);
  70     }
  71 
  72     /**
  73      * Constructs a new Manifest that is a copy of the specified Manifest.
  74      *


 131      */
 132     public void clear() {
 133         attr.clear();
 134         entries.clear();
 135     }
 136 
 137     /**
 138      * Writes the Manifest to the specified OutputStream.
 139      * Attributes.Name.MANIFEST_VERSION must be set in
 140      * MainAttributes prior to invoking this method.
 141      *
 142      * @param out the output stream
 143      * @exception IOException if an I/O error has occurred
 144      * @see #getMainAttributes
 145      */
 146     public void write(OutputStream out) throws IOException {
 147         DataOutputStream dos = new DataOutputStream(out);
 148         // Write out the main attributes for the manifest
 149         attr.writeMain(dos);
 150         // Now write out the pre-entry attributes
 151         Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator();
 152         while (it.hasNext()) {
 153             Map.Entry<String, Attributes> e = it.next();
 154             StringBuffer buffer = new StringBuffer("Name: ");
 155             String value = e.getKey();
 156             if (value != null) {
 157                 byte[] vb = value.getBytes("UTF8");
 158                 value = new String(vb, 0, 0, vb.length);
 159             }
 160             buffer.append(value);
 161             buffer.append("\r\n");
 162             make72Safe(buffer);
 163             dos.writeBytes(buffer.toString());
 164             e.getValue().write(dos);
 165         }
 166         dos.flush();
 167     }
 168 
 169     /**
 170      * Adds line breaks to enforce a maximum 72 bytes per line.
 171      */
 172     static void make72Safe(StringBuffer line) {
 173         int length = line.length();
 174         if (length > 72) {
 175             int index = 70;
 176             while (index < length - 2) {
 177                 line.insert(index, "\r\n ");
 178                 index += 72;
 179                 length += 3;
 180             }
 181         }
 182         return;
 183     }
 184