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

Mat*_*att 4 java jar manifest.mf

我对Manifest规范的理解对于一条线的确切定义有点模糊.特别是读取的部分

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

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

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

Mat*_*att 5

虽然这并不是严格地回答了规范在说明行时的意思,但它确实回答了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));
  }
}
Run Code Online (Sandbox Code Playgroud)

得到以下输出

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
Run Code Online (Sandbox Code Playgroud)

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

值得注意的是,Manifestread方法对行长度很宽松,只要行长度不超过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"));
Run Code Online (Sandbox Code Playgroud)

其中愉快地输出This line is longer than 72 characters, does the Manifest class correctly handle parsing of it even over multiple lines?作为单一的清单价值.