Skip to content

Commit 61bd142

Browse files
committed
#18: Manifest#Attribute#writeLine does not properly calculate max line length
This closes #18
1 parent a01612f commit 61bd142

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/main/java/org/codehaus/plexus/archiver/jar/Manifest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
* Specifically, a manifest element consists of
4848
* a set of attributes and sections. These sections in turn may contain
4949
* attributes. Note in particular that this may result in manifest lines
50-
* greater than 72 bytes being wrapped and continued on the next
51-
* line. If an application can not handle the continuation mechanism, it
52-
* is a defect in the application, not this task.
50+
* greater than 72 bytes (including line break) being wrapped and continued
51+
* on the next line. If an application can not handle the continuation
52+
* mechanism, it is a defect in the application, not this task.
5353
*
5454
* @since Ant 1.4
5555
*/
@@ -337,7 +337,7 @@ private void writeValue( Writer writer, String value )
337337
}
338338

339339
/**
340-
* Write a single Manifest line. Should handle more than 72 characters of line
340+
* Write a single Manifest line. Should handle more than 72 bytes of line
341341
*
342342
* @param writer the Writer to which the attribute is written
343343
* @param line the manifest line to be written
@@ -346,12 +346,15 @@ private void writeValue( Writer writer, String value )
346346
private void writeLine( Writer writer, String line )
347347
throws IOException
348348
{
349-
while ( line.getBytes().length > MAX_LINE_LENGTH )
349+
// Make sure we have at most 70 bytes in UTF-8 as specified excluding line break
350+
while ( line.getBytes("UTF-8").length > MAX_SECTION_LENGTH )
350351
{
351-
// try to find a MAX_LINE_LENGTH byte section
352-
int breakIndex = MAX_SECTION_LENGTH;
352+
// Try to find a MAX_SECTION_LENGTH
353+
// Use the minimum because we operate on at most chars and not bytes here otherwise
354+
// if we have more bytes than chars we will die in an IndexOutOfBoundsException.
355+
int breakIndex = Math.min( line.length(), MAX_SECTION_LENGTH ) ;
353356
String section = line.substring( 0, breakIndex );
354-
while ( section.getBytes().length > MAX_SECTION_LENGTH && breakIndex > 0 )
357+
while ( section.getBytes("UTF-8").length > MAX_SECTION_LENGTH && breakIndex > 0 )
355358
{
356359
breakIndex--;
357360
section = line.substring( 0, breakIndex );

src/test/java/org/codehaus/plexus/archiver/jar/ManifestTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ public void testAttributeLongLineWrite()
108108
writer.toString() );
109109
}
110110

111+
public void testAttributeLongLineWriteNonAscii()
112+
throws Exception
113+
{
114+
StringWriter writer = new StringWriter();
115+
Manifest.Attribute attr = new Manifest.Attribute();
116+
String longLineOfChars =
117+
"Ед докэндё форынчйбюж зкрипторэм векж, льабятюр ыкжпэтэндяз мэль ут, квюо ут модо "
118+
+ "либриз такематыш. Ыюм йн лаборамюз компльыктётюр, векж ыпикурэи дежпютатионй ед,"
119+
+ " ыам ты хабымуч мальюизчыт. Но вим алёэнюм вюльпутаты, ад нощтыр трётанё льаборэж"
120+
+ " вэл, кевёбюж атоморюм кончюлату векж экз. Ку щольыат вёртюты ёнэрмйщ ыюм.";
121+
attr.setName( "test" );
122+
attr.setValue( longLineOfChars );
123+
attr.write( writer );
124+
writer.flush();
125+
assertEquals( "should be multiline",
126+
"test: Ед докэндё форынчйбюж зкрипторэм в"
127+
+ Manifest.EOL +
128+
" екж, льабятюр ыкжпэтэндяз мэль ут, квю"
129+
+ Manifest.EOL +
130+
" о ут модо либриз такематыш. Ыюм йн лаб"
131+
+ Manifest.EOL +
132+
" орамюз компльыктётюр, векж ыпикурэи д"
133+
+ Manifest.EOL +
134+
" ежпютатионй ед, ыам ты хабымуч мальюи"
135+
+ Manifest.EOL +
136+
" зчыт. Но вим алёэнюм вюльпутаты, ад но"
137+
+ Manifest.EOL +
138+
" щтыр трётанё льаборэж вэл, кевёбюж ат"
139+
+ Manifest.EOL +
140+
" оморюм кончюлату векж экз. Ку щольыат "
141+
+ Manifest.EOL +
142+
" вёртюты ёнэрмйщ ыюм."
143+
+ Manifest.EOL,
144+
writer.toString() );
145+
}
111146

112147
public void testDualClassPath()
113148
throws ManifestException, IOException

0 commit comments

Comments
 (0)