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

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

    Details

    • Subcomponent:
    • Resolved In Build:
      b01
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      If Base64.getMimeEncoder(lineLength, lineSeparator) is called with a lineLength value between 1-3 the resulting encoder is incorrectly initialized leading to different behavior between methods wrap and encodeXXX: Sequences encoded through the OutputStream from the wrap method contain a leading line separator.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The encoded result from the wrap method should not contain a leading line separator.
      ACTUAL -
      Leading line separator

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.Base64;

      public class Base64Bug {

        public static void main(String[] args) throws IOException {
          String input = "Man";
          int lineLength = 1; // values 1-3 will cause this error. Line 138 of Base64 should probably be 'if (lineLength / 4 <= 0) {'
          Base64.Encoder encoder = Base64.getMimeEncoder(lineLength, new byte[]{'@'});

          ByteArrayOutputStream result = new ByteArrayOutputStream();
          OutputStream out = encoder.wrap(result);

          out.write(input.getBytes());
          out.close();
          System.out.println(new String(result.toByteArray())); // @TWFu <- line separator at beginning of output
          System.out.println(new String(encoder.encode(input.getBytes()))); // TWFu
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Initialize the encoder with a line length value of 0 or >=4

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sherman Xueming Shen
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: