问题描述
我对清单规范的理解对于 line 的确切定义有点含糊.具体来说,读取的部分
My understanding of the Manifest specification is a little vague around the exact definition of a line. Specifically the part that reads
我不确定在这方面行是否包含EOL(CR LF | LF | CR
)字符.
I'm unsure as to whether a line in this regards includes the EOL (CR LF | LF | CR
) characters or not.
我有两种第三方的库实现,它们可以编写清单,一种可以生成内容,其中似乎包含EOL字符作为该行的一部分,而另一种则不包含.
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.
推荐答案
虽然这并不是严格回答规范说 line 是什么意思的问题,但它确实回答了以下问题: JDK如何围绕该规范实现其清单支持.您希望它们都来自同一个团队,因此可以通过实现的细节来澄清规范中的任何歧义,但是如果可以找到更规范的答案,我会暂时不接受.
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. 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.
通过阅读JDK源代码并使用JDK Manifest类运行一些测试以写出清单文件,我可以说JDK(版本1.7)写出了清单条目,其中行长包括EOL字节.
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.
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个字符+ 2个字符的EOL,即< =72.行内容为72个字符的Line-Property
被分成两行,其中第一行包含前70个字符+ CR LF
,后跟一个空格,其余2个字符.
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.
在这里值得注意的是,Manifest
读取方法对行长宽容,只要该行包括EOL标记在内的行长不超过512字节,那么它将很高兴地读取文件,如代码所示下方
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 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?
作为单个清单值输出.
Which happily outputs 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.
这篇关于jar MANIFEST.MF最大行长度是否为72包含EOL字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!