简体   繁体   English

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

[英]Does the jar MANIFEST.MF max line length of 72 include the EOL bytes

My understanding of the Manifest specification is a little vague around the exact definition of a line . 我对Manifest规范的理解对于一条线的确切定义有点模糊。 Specifically the part that reads 特别是读取的部分

No line may be longer than 72 bytes (not characters), in its UTF8-encoded form. 任何行都不能超过72字节(不是字符),采用UTF8编码形式。

I'm unsure as to whether a line in this regards includes the EOL ( CR LF | LF | CR ) characters or not. 我不确定这方面的一行是否包括EOL( CR LF | LF | CR )字符。

I have two third party implementations of libraries that write manifests and one produces content that appears to include the EOL characters as part of the line and one that does not. 我有两个编写清单的库的第三方实现,一个生成的内容似乎包含EOL字符作为行的一部分而另一个不包含。

While this isn't strictly answering the question of what does the specification mean when it says line it does answer it in terms of how the JDK has implemented its Manifest support around that specification. 虽然这并不是严格地回答了规范在说明行时的意思,但它确实回答了JDK如何在该规范中实现其Manifest支持。 You'd hope that they both came from the same team and hence any ambiguity in the specification can be clarified by the details of the implementation, but I'll leave this as unaccepted for a while in case a more canonical answer can be found. 你希望他们都来自同一个团队,因此规范中的任何含糊之处都可以通过实现的细节来澄清,但是如果能找到更规范的答案,我会暂时不接受这一点。

From reading through the JDK source and running a few tests using the JDK Manifest class to write out a manifest file I can say that the JDK (at version 1.7) writes out manifest entries where the line length includes the EOL bytes . 通过阅读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));
  }
}

results in the following output 得到以下输出

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

Note that the Long-Property fits onto one line as the line content is 70 characters + 2 characters of EOL which is <= 72. The Line-Property which has line content of 72 characters gets split into two lines where the first line contains the first 70 characters + CR LF followed by a space and the remaining 2 characters. 请注意, Long-Property适合一行,因为行内容为70个字符+ EOL的2个字符<= 72.行内容为72个字符的Line-Property被分成两行,其中第一行包含前70个字符+ CR LF后跟空格和剩余的2个字符。

It's worth noting here that the Manifest read method is lenient about the line length, so long as the line isn't greater than 512 bytes long including the EOL markers then it will happily read the file as shown by the code below 值得注意的是, Manifest read方法对行长度很宽松,只要行长度不超过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"));

Which happily outputs This line is longer than 72 characters, does the Manifest class correctly handle parsing of it even over multiple lines? 哪个愉快地输出This line is longer than 72 characters, does the Manifest class correctly handle parsing of it even over multiple lines? as a single manifest value. 作为单一的清单价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM