< prev index next >
src/java.base/share/classes/java/util/jar/Manifest.java
Print this page
rev 51517 : 8205525: Improve exception messages during manifest parsing of jar archives
@@ -30,11 +30,11 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
-import java.util.Iterator;
+import sun.security.util.SecurityProperties;
/**
* The Manifest class is used to maintain Manifest entry names and their
* associated Attributes. There are main Manifest Attributes as well as
* per-entry Attributes. For information on the Manifest format, please
@@ -45,16 +45,23 @@
* @author David Connelly
* @see Attributes
* @since 1.2
*/
public class Manifest implements Cloneable {
+
+ private static final boolean jarPathInExceptionText =
+ SecurityProperties.includedInExceptions("jarPath");
+
// manifest main attributes
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;
+
/**
* Constructs a new, empty Manifest.
*/
public Manifest() {
}
@@ -68,10 +75,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 {
+ read(is);
+ this.jarFilename = jarFilename;
+ }
+
+ /**
* Constructs a new Manifest that is a copy of the specified Manifest.
*
* @param man the Manifest to copy
*/
public Manifest(Manifest man) {
@@ -177,10 +196,18 @@
length += 3; // + line break ("\r\n") and space
}
return;
}
+ static String getErrorPosition(String filename, final int lineNumber) {
+ if (filename == null || !jarPathInExceptionText) {
+ return "line " + lineNumber;
+ }
+
+ return "manifest of " + filename + ":" + lineNumber;
+ }
+
/**
* Reads the Manifest from the specified InputStream. The entry
* names and attributes read will be merged in with the current
* manifest entries.
*
@@ -191,11 +218,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 +231,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 ("
+ + getErrorPosition(jarFilename, lineNumber) + ")");
}
if (len > 0 && lbuf[len-1] == '\r') {
--len;
}
if (len == 0 && skipEmptyLines) {
@@ -218,11 +248,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"
+ + getErrorPosition(jarFilename, lineNumber) + ")");
}
if (fis.peek() == ' ') {
// name is wrapped
lastline = new byte[len - 6];
System.arraycopy(lbuf, 6, lastline, 0, len - 6);
@@ -244,11 +275,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 >