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

Base64.Decoder decoding methods are not consistent in treating non-padded data

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2
    • 8
    • None
    • core-libs
    • b77
    • Verified

    Description

      For two-character and three-character Base64 strings IAE is not thrown by "eager" decoding methods.
      RFC 4648 says that an implementation must by default provide padding when encoding (if not specified otherwise) so this makes me think that by default a string with a dropped padding is not a valid Base64 scheme and IAE should be thrown by decoding methods.
      Or this aspect needs to be specified.


      However if an encoded stream containing not padded data is wrapped and then decoded
      then an exception is thrown. Seems that there is a behavioral inconsistency here.

      Please see the following code sample:


      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      import java.util.Base64;

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

      public class WrapDecodeNoPaddingStream {

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

              final Base64.Decoder decoder = Base64.getDecoder();

              // decodes to "A"
              System.err.println(
                      new String(decoder.decode("QQ"), US_ASCII)
              );

              // throws java.io.IOException: Base64 stream has un-decoded dangling byte(s).
              decoder.wrap(
                      new ByteArrayInputStream(
                              "QQ".getBytes(US_ASCII)
                      )).read();
          }

      }

      The output will be:

      A
      Exception in thread "main" java.io.IOException: Base64 stream has un-decoded dangling byte(s).
      at java.util.Base64$DecInputStream.read(Base64.java:1251)
      at java.util.Base64$DecInputStream.read(Base64.java:1224)
      at WrapDecodeNoPaddingStream.main(WrapDecodeNoPaddingStream.java:22)

      Attachments

        Activity

          People

            sherman Xueming Shen
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: