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 );
0 commit comments