罐子MANIFEST.MF最大行长度是否包括EOL字节

时间:2015-10-15 08:25:31

标签: java jar manifest.mf

我对Manifest specification的理解对于的确切定义有点模糊。特别是读取的部分

  

任何行都不能超过72个字节(不是字符),采用UTF8编码格式。

我不确定这方面的是否包含EOL(CR LF | LF | CR)字符。

我有两个编写清单的库的第三方实现,一个生成的内容似乎包含EOL字符作为行的一部分而另一个不包含。

1 个答案:

答案 0 :(得分:5)

虽然这并没有严格地回答说明 line 时规范的含义,但它确实回答了JDK如何在该规范中实现其Manifest支持。您希望他们都来自同一个团队,因此规范中的任何含糊之处都可以通过实施的细节来澄清,但是如果有更多规范的答案,我会暂时不接受这一点。可以找到。

从阅读JDK源代码并使用JDK Manifest类运行一些测试来写出清单文件,我可以说JDK(版本1.7)写出清单条目,其中行长度包含EOL字节

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.jar.Attributes.Name.MANIFEST_VERSION;

public class ManifestTest {
  public static void main(String[] args) throws IOException {
    Manifest m = new Manifest();
    Attributes a = m.getMainAttributes();
    a.put(MANIFEST_VERSION, "1.0"); // required or the file doesn't get written

    // Long-Property: This line has 72 characters without eol, it's hard to get
    a.putValue("Line-Property", "This line has 72 characters without eol, it's hard to get");
    // Long-Property: This line has 72 characters with eol, it is hard to get
    a.putValue("Long-Property", "This line has 72 characters with eol, it is hard to get");
    a.putValue("Massive-Property", "This line wraps around always as it has a lot of characters in it");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    m.write(out);
    System.out.println(new String(out.toByteArray(), UTF_8));
  }
}

产生以下输出

Manifest-Version: 1.0
Long-Property: This line has 72 characters with eol, it is hard to get
Massive-Property: This line wraps around always as it has a lot of cha
 racters in it
Line-Property: This line has 72 characters without eol, it's hard to g
 et

请注意,Long-Property适合一行,因为行内容为70个字符+ EOL的2个字符,其中< = 72.具有72个字符的行内容的Line-Property被拆分为两行,其中第一行包含前70个字符+ CR LF后跟空格和剩余的2个字符。

值得注意的是,Manifest读取方法对于行长度是宽松的,只要该行长度不超过512字节(包括EOL标记),那么它将很乐意阅读文件,如下面的代码所示

Manifest m = new Manifest();
String longManifest = "Manifest-Version: 1.0\r\n" +
    "Too-Long: This line is longer than 72 characters, does the Manifest class correctly \r\n" +
    " handle parsing of it even over multiple lines?\r\n";
m.read(new ByteArrayInputStream(longManifest.getBytes(UTF_8)));
System.out.println(m.getMainAttributes().getValue("Too-Long"));

愉快地将This line is longer than 72 characters, does the Manifest class correctly handle parsing of it even over multiple lines?输出为单个清单值。