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

Base64.getEncoder(0, byte[]) returns an encoder that unexpectedly inserts line separators

XMLWordPrintable

    • b89
    • Verified

      Specification for the method
      java.util.Base64.getEncoder(int lineLength, byte[] lineSeparator)

      says:

      Parameters:
          lineLength - the length of each output line (rounded down to nearest multiple of 4). If lineLength <= 0 the output will not be separated in lines


      However if a zero line length is specified encoding methods wrap() and encode(ByteBuffer src, ByteBuffer dst, int bytesOut) return encoded string which starts from the given line separator.

      Please see the following example:


      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.Base64;

      import static java.nio.charset.StandardCharsets.*;

      public class NoLineSeparators {

          public static void main(String[] args) throws IOException {

              final Base64.Encoder encoder = Base64.getEncoder(0, "$$$".getBytes(US_ASCII));

              final byte[] bytesIn = "fo".getBytes(US_ASCII);

              ByteArrayOutputStream encodingStream = new ByteArrayOutputStream();
              OutputStream encoding = encoder.wrap(encodingStream);
              encoding.write(bytesIn);
              encoding.close();

              final byte[] encodedBytes = encodingStream.toByteArray();

              System.err.println("result = " + new String(encodedBytes, US_ASCII));
          }

      }

      for b75 the output will be

      result = $$$Zm8=


      The following testcase will fail due to this issue:
      api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]

            msheppar Mark Sheppard
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: