< 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,60 **** --- 51,63 ---- 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,77 **** --- 71,92 ---- 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,201 **** // 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); // Total number of entries, attributes read int ecount = 0, acount = 0; // Average size of entry attributes int asize = 2; // Now parse the manifest entries --- 206,216 ---- // 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 ! 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,215 **** boolean skipEmptyLines = true; byte[] lastline = null; while ((len = fis.readLine(lbuf)) != -1) { byte c = lbuf[--len]; if (c != '\n' && c != '\r') { ! throw new IOException("manifest line too long"); } if (len > 0 && lbuf[len-1] == '\r') { --len; } if (len == 0 && skipEmptyLines) { --- 219,233 ---- 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 (" ! + Attributes.getErrorPosition(jarFilename, lineNumber) + ")"); } if (len > 0 && lbuf[len-1] == '\r') { --len; } if (len == 0 && skipEmptyLines) {
*** 218,228 **** skipEmptyLines = false; if (name == null) { name = parseName(lbuf, len); if (name == null) { ! throw new IOException("invalid manifest format"); } if (fis.peek() == ' ') { // name is wrapped lastline = new byte[len - 6]; System.arraycopy(lbuf, 6, lastline, 0, len - 6); --- 236,247 ---- skipEmptyLines = false; if (name == null) { name = parseName(lbuf, len); if (name == null) { ! 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,254 **** Attributes attr = getAttributes(name); if (attr == null) { attr = new Attributes(asize); entries.put(name, attr); } ! attr.read(fis, lbuf); 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. --- 263,273 ---- Attributes attr = getAttributes(name); if (attr == null) { attr = new Attributes(asize); entries.put(name, attr); } ! 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 >