Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8196354

java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 11
    • core-libs
    • None
    • behavioral
    • minimal
    • Java API

    Description

      Summary

      The spec and its corresponding implementation of method java.util.Base64.getMimeEncoder(int, byte[]) do not specify/handle some corner cases (when "lineLength" is passed in as value 1, 2, 3) correctly.

      Problem

      The specification currently specifies that if the parameter "lineLength" is "<=0" the returned base64 encoder from the method will not be a mime type encoder (means the output will NOT be line separated). It fails to specify the design intention that this "<= 0" condition should be applied to the "lineLength" value after "rounded down to nearest multiple of 4" (to cover the corner cases of "lineLength is 1,2 or 3"). And the corresponding implementation also fails to handle these corner cases correctly as well. As the result the base64 encoder OutputStream (from java.util.Base64.getMimeEncoder(...).wrap(), with "lineLength" specified to 1, 2, or 3) incorrectly outputs a "newline" at the beginning of its output.

      Solution

      To clarify the specification to specify the expected behavior for these corner cases and update the corresponding implementation to follow the spec.

      Specification

      /**
       * Returns a {@link Encoder} that encodes using the
       * <a href="#mime">MIME</a> type base64 encoding scheme
       * with specified line length and line separators.
       *
       * @param   lineLength
       *          the length of each output line (rounded down to nearest multiple
       *          of 4). If the rounded down line length is not a positive value,
       *          the output will not be separated in lines
       * @param   lineSeparator
       *          the line separator for each output line
       *
       * @return  A Base64 encoder.
       *
       * @throws  IllegalArgumentException if {@code lineSeparator} includes any
       *          character of "The Base64 Alphabet" as specified in Table 1 of
       *          RFC 2045.
       */
      public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) 

      Diff:

      @@ -114,12 +114,12 @@
            * <a href="#mime">MIME</a> type base64 encoding scheme
            * with specified line length and line separators.
            *
            * @param   lineLength
            *          the length of each output line (rounded down to nearest multiple
      -     *          of 4). If {@code lineLength <= 0} the output will not be separated
      -     *          in lines
      +     *          of 4). If the rounded down line length is not a positive value,
      +     *          the output will not be separated in lines
            * @param   lineSeparator
            *          the line separator for each output line
            *
            * @return  A Base64 encoder.
            *
      

      Attachments

        Issue Links

          Activity

            People

              sherman Xueming Shen
              webbuggrp Webbug Group
              Alan Bateman, Paul Sandoz, Roger Riggs
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: