2015-10-15 79 views
3

我對Manifest specification的理解有點含糊地圍繞着線條的確切定義。具體來說,讀取的部分爲罐子MANIFEST.MF最大線長度是否包含EOL字節

以UTF8編碼形式,沒有行可能不超過72個字節(不是字符)。

我不能確定作爲對在這點上是否包括EOL(CR LF | LF | CR)字符或沒有。

我有兩個庫的第三方實現寫清單,一個產生的內容看起來包含EOL字符作爲行的一部分,一個沒有。

+0

我應該指出,我相信沒有_not_包含EOL字節的lib(bnd)在計算行長度時比我相信這樣做的lib更符合我的理解,但有些確認將不勝感激。 – Matt

+0

可能會有助於http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/jar/Manifest.java#Manifest.read%28java。 io.InputStream%29 –

+0

感謝指針@ KonstantinV.Salikhov在閱讀源代碼後,我發佈了一個基於代碼的_fuzzy_回答,位於http://stackoverflow.com/a/33144934/317404,而不是任何額外的說明規範。 – Matt

回答

3

儘管這並不是嚴格回答了規範說明時是什麼意思,但它確實回答了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裝配到一個線作爲線含量EOL的70個字符+ 2個字符,其是< = 72,其具有線的Line-Property 72個字符的內容被分成兩行,其中第一行包含前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?作爲單個清單值。

相關問題