* return (Attributes)getEntries().get(name) ** Though {@code null} is a valid {@code name}, when * {@code getAttributes(null)} is invoked on a {@code Manifest} * obtained from a jar file, {@code null} will be returned. While jar * files themselves do not allow {@code null}-named attributes, it is * possible to invoke {@link #getEntries} on a {@code Manifest}, and * on that result, invoke {@code put} with a null key and an * arbitrary value. Subsequent invocations of * {@code getAttributes(null)} will return the just-{@code put} * value. *
* Note that this method does not return the manifest's main attributes;
* see {@link #getMainAttributes}.
*
* @param name entry name
* @return the Attributes for the specified entry name
*/
public Attributes getAttributes(String name) {
return getEntries().get(name);
}
/**
* Returns the Attributes for the specified entry name, if trusted.
*
* @param name entry name
* @return returns the same result as {@link #getAttributes(String)}
* @throws SecurityException if the associated jar is signed but this entry
* has been modified after signing (i.e. the section in the manifest
* does not exist in SF files of all signers).
*/
Attributes getTrustedAttributes(String name) {
// Note: Before the verification of MANIFEST.MF/.SF/.RSA files is done,
// jv.isTrustedManifestEntry() isn't able to detect MANIFEST.MF change.
// Users of this method should call SharedSecrets.javaUtilJarAccess()
// .ensureInitialization() first.
Attributes result = getAttributes(name);
if (result != null && jv != null && ! jv.isTrustedManifestEntry(name)) {
throw new SecurityException("Untrusted manifest entry: " + name);
}
return result;
}
/**
* Clears the main Attributes as well as the entries in this Manifest.
*/
public void clear() {
attr.clear();
entries.clear();
}
/**
* Writes the Manifest to the specified OutputStream.
* Attributes.Name.MANIFEST_VERSION must be set in
* MainAttributes prior to invoking this method.
*
* @param out the output stream
* @throws IOException if an I/O error has occurred
* @see #getMainAttributes
*/
public void write(OutputStream out) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
// Write out the main attributes for the manifest
attr.writeMain(dos);
// Now write out the per-entry attributes
StringBuilder buffer = entries.isEmpty() ? null : new StringBuilder(72);
for (Map.Entry
* Combining diacritical marks may be separated from the associated base
* character or other combining diacritical marks of that base character
* by a continuation line break ("{@code \r\n }") if the whole sequence of
* base character and all the combining diacritical marks belonging to it
* exceed 71 bytes in their binary form encoded with UTF-8. This limit is
* only 71 bytes rather than 72 because continuation lines start with a
* space that uses the first byte of the 72 bytes each line can hold up to
* and the first line provides even less space for the value because it
* starts with the name.
*/
static void println72(OutputStream out, String line) throws IOException {
int linePos = 0; // number of bytes already put out on current line
BreakIterator boundary = BreakIterator.getCharacterInstance();
boundary.setText(line);
int start = boundary.first(), end;
while ((end = boundary.next()) != BreakIterator.DONE) {
String character = line.substring(start, end);
start = end;
byte[] characterBytes = character.getBytes(UTF_8.INSTANCE);
int characterLength = characterBytes.length;
// Put out a break onto a new line if the character does not fit on
// the current line anymore but fits on a whole new line.
// In other words, if the current character does not fit on one
// whole line alone, fill the current line first before breaking
// inside of the character onto a new line.
if (linePos + characterLength > 72 && characterLength < 72) {
println(out);
out.write(' ');
linePos = 1;
}
int characterPos = 0; // number of bytes of current character
// already put out
while (linePos + characterLength - characterPos > 72) {
int nextBreakPos = 72 - linePos;
while (isContinuation(
characterBytes[characterPos + nextBreakPos])) {
nextBreakPos--;
}
out.write(characterBytes, characterPos, nextBreakPos);
characterPos += nextBreakPos;
println(out);
out.write(' ');
linePos = 1;
}
out.write(characterBytes,
characterPos, characterLength - characterPos);
linePos += characterLength - characterPos;
}
println(out);
}
/**
* Writes a line break to {@code out}.
*/
static void println(OutputStream out) throws IOException {
out.write('\r');
out.write('\n');
}
static String getErrorPosition(String filename, final int lineNumber) {
if (filename == null ||
!SecurityProperties.INCLUDE_JAR_NAME_IN_EXCEPTIONS) {
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.
*
* @param is the input stream
* @throws IOException if an I/O error has occurred
*/
public void read(InputStream is) throws IOException {
read(is, null);
}
private void read(InputStream is, String jarFilename) throws IOException {
// 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
int len;
String name = null;
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 ("
+ getErrorPosition(jarFilename, lineNumber) + ")");
}
if (len > 0 && lbuf[len-1] == '\r') {
--len;
}
if (len == 0 && skipEmptyLines) {
continue;
}
skipEmptyLines = false;
if (name == null) {
name = parseName(lbuf, len);
if (name == null) {
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);
continue;
}
} else {
// continuation line
byte[] buf = new byte[lastline.length + len - 1];
System.arraycopy(lastline, 0, buf, 0, lastline.length);
System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
if (fis.peek() == ' ') {
// name is wrapped
lastline = buf;
continue;
}
name = new String(buf, 0, buf.length, UTF_8.INSTANCE);
lastline = null;
}
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.
asize = Math.max(2, acount / ecount);
name = null;
skipEmptyLines = true;
}
}
private String parseName(byte[] lbuf, int len) {
if (toLower(lbuf[0]) == 'n' && toLower(lbuf[1]) == 'a' &&
toLower(lbuf[2]) == 'm' && toLower(lbuf[3]) == 'e' &&
lbuf[4] == ':' && lbuf[5] == ' ') {
return new String(lbuf, 6, len - 6, UTF_8.INSTANCE);
}
return null;
}
private int toLower(int c) {
return (c >= 'A' && c <= 'Z') ? 'a' + (c - 'A') : c;
}
/**
* Returns true if the specified Object is also a Manifest and has
* the same main Attributes and entries.
*
* @param o the object to be compared
* @return true if the specified Object is also a Manifest and has
* the same main Attributes and entries
*/
public boolean equals(Object o) {
if (o instanceof Manifest) {
Manifest m = (Manifest)o;
return attr.equals(m.getMainAttributes()) &&
entries.equals(m.getEntries());
} else {
return false;
}
}
/**
* Returns the hash code for this Manifest.
*/
public int hashCode() {
return attr.hashCode() + entries.hashCode();
}
/**
* Returns a shallow copy of this Manifest. The shallow copy is
* implemented as follows:
*
* public Object clone() { return new Manifest(this); }
*
* @return a shallow copy of this Manifest
*/
public Object clone() {
return new Manifest(this);
}
/*
* A fast buffered input stream for parsing manifest files.
*/
static class FastInputStream extends FilterInputStream {
private byte buf[];
private int count = 0;
private int pos = 0;
FastInputStream(InputStream in) {
this(in, 8192);
}
FastInputStream(InputStream in, int size) {
super(in);
buf = new byte[size];
}
public int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count) {
return -1;
}
}
return Byte.toUnsignedInt(buf[pos++]);
}
public int read(byte[] b, int off, int len) throws IOException {
int avail = count - pos;
if (avail <= 0) {
if (len >= buf.length) {
return in.read(b, off, len);
}
fill();
avail = count - pos;
if (avail <= 0) {
return -1;
}
}
if (len > avail) {
len = avail;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
/*
* Reads 'len' bytes from the input stream, or until an end-of-line
* is reached. Returns the number of bytes read.
*/
public int readLine(byte[] b, int off, int len) throws IOException {
byte[] tbuf = this.buf;
int total = 0;
while (total < len) {
int avail = count - pos;
if (avail <= 0) {
fill();
avail = count - pos;
if (avail <= 0) {
return -1;
}
}
int n = len - total;
if (n > avail) {
n = avail;
}
int tpos = pos;
int maxpos = tpos + n;
byte c = 0;
// jar.spec.newline: CRLF | LF | CR (not followed by LF)
while (tpos < maxpos && (c = tbuf[tpos++]) != '\n' && c != '\r');
if (c == '\r' && tpos < maxpos && tbuf[tpos] == '\n') {
tpos++;
}
n = tpos - pos;
System.arraycopy(tbuf, pos, b, off, n);
off += n;
total += n;
pos = tpos;
c = tbuf[tpos-1];
if (c == '\n') {
break;
}
if (c == '\r') {
if (count == pos) {
// try to see if there is a trailing LF
fill();
if (pos < count && tbuf[pos] == '\n') {
if (total < len) {
b[off++] = '\n';
total++;
} else {
// we should always have big enough lbuf but
// just in case we don't, replace the last CR
// with LF.
b[off - 1] = '\n';
}
pos++;
}
}
break;
}
}
return total;
}
public byte peek() throws IOException {
if (pos == count)
fill();
if (pos == count)
return -1; // nothing left in buffer
return buf[pos];
}
public int readLine(byte[] b) throws IOException {
return readLine(b, 0, b.length);
}
public long skip(long n) throws IOException {
if (n <= 0) {
return 0;
}
long avail = count - pos;
if (avail <= 0) {
return in.skip(n);
}
if (n > avail) {
n = avail;
}
pos += n;
return n;
}
public int available() throws IOException {
return (count - pos) + in.available();
}
public void close() throws IOException {
if (in != null) {
in.close();
in = null;
buf = null;
}
}
private void fill() throws IOException {
count = pos = 0;
int n = in.read(buf, 0, buf.length);
if (n > 0) {
count = n;
}
}
}
}