47
47
* Specifically, a manifest element consists of
48
48
* a set of attributes and sections. These sections in turn may contain
49
49
* 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.
53
53
*
54
54
* @since Ant 1.4
55
55
*/
@@ -337,7 +337,7 @@ private void writeValue( Writer writer, String value )
337
337
}
338
338
339
339
/**
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
341
341
*
342
342
* @param writer the Writer to which the attribute is written
343
343
* @param line the manifest line to be written
@@ -346,12 +346,15 @@ private void writeValue( Writer writer, String value )
346
346
private void writeLine ( Writer writer , String line )
347
347
throws IOException
348
348
{
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 )
350
351
{
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 ) ;
353
356
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 )
355
358
{
356
359
breakIndex --;
357
360
section = line .substring ( 0 , breakIndex );
0 commit comments