< prev index next >

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

Print this page
rev 51096 : 8205525: Improve exception messages during manifest parsing of jar archives

@@ -51,10 +51,13 @@
     private Attributes attr = new Attributes();
 
     // manifest entries
     private Map<String, Attributes> entries = new HashMap<>();
 
+    // name of the corresponding jar archive if available.
+    private String jarFilename = null;
+
     /**
      * Constructs a new, empty Manifest.
      */
     public Manifest() {
     }

@@ -68,10 +71,22 @@
     public Manifest(InputStream is) throws IOException {
         read(is);
     }
 
     /**
+     * Constructs a new Manifest from the specified input stream.
+     *
+     * @param is the input stream containing manifest data
+     * @param jarFilename the name of the corresponding jar archive if available
+     * @throws IOException if an I/O error has occured
+     */
+    Manifest(InputStream is, String jarFilename) throws IOException {
+        this.jarFilename = jarFilename;
+        read(is);
+    }
+
+    /**
      * Constructs a new Manifest that is a copy of the specified Manifest.
      *
      * @param man the Manifest to copy
      */
     public Manifest(Manifest man) {

@@ -191,11 +206,11 @@
         // Buffered input stream for reading manifest data
         FastInputStream fis = new FastInputStream(is);
         // Line buffer
         byte[] lbuf = new byte[512];
         // Read the main attributes for the manifest
-        attr.read(fis, lbuf);
+        int lineNumber = attr.read(fis, lbuf, jarFilename, 0);
         // Total number of entries, attributes read
         int ecount = 0, acount = 0;
         // Average size of entry attributes
         int asize = 2;
         // Now parse the manifest entries

@@ -204,12 +219,15 @@
         boolean skipEmptyLines = true;
         byte[] lastline = null;
 
         while ((len = fis.readLine(lbuf)) != -1) {
             byte c = lbuf[--len];
+            lineNumber++;
+
             if (c != '\n' && c != '\r') {
-                throw new IOException("manifest line too long");
+                throw new IOException("manifest line too long ("
+                           + Attributes.getErrorPosition(jarFilename, lineNumber) + ")");
             }
             if (len > 0 && lbuf[len-1] == '\r') {
                 --len;
             }
             if (len == 0 && skipEmptyLines) {

@@ -218,11 +236,12 @@
             skipEmptyLines = false;
 
             if (name == null) {
                 name = parseName(lbuf, len);
                 if (name == null) {
-                    throw new IOException("invalid manifest format");
+                    throw new IOException("invalid manifest format"
+                              + Attributes.getErrorPosition(jarFilename, lineNumber) + ")");
                 }
                 if (fis.peek() == ' ') {
                     // name is wrapped
                     lastline = new byte[len - 6];
                     System.arraycopy(lbuf, 6, lastline, 0, len - 6);

@@ -244,11 +263,11 @@
             Attributes attr = getAttributes(name);
             if (attr == null) {
                 attr = new Attributes(asize);
                 entries.put(name, attr);
             }
-            attr.read(fis, lbuf);
+            lineNumber = attr.read(fis, lbuf, jarFilename, lineNumber);
             ecount++;
             acount += attr.size();
             //XXX: Fix for when the average is 0. When it is 0,
             // you get an Attributes object with an initial
             // capacity of 0, which tickles a bug in HashMap.
< prev index next >