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

Base64.Decoder.wrap(java.io.InputStream) returns InputStream which throws unspecified IOException on attempt to decode invalid Base64 byte stream

XMLWordPrintable

    • b77
    • Verified

      Though method

      http://download.java.net/jdk8/docs/api/java/util/Base64.Decoder.html#wrap(java.io.InputStream)

      has different - lazy - nature than other decoding methods it still need to specify properties of the returned stream related to incorrect Base64 input handling.
      In case of an invalid Base64 scheme an exception is being thrown by the returned stream when one tries to read bytes from it.
      It's not an IllegalArgumentException as thrown by other decoding methods, it is java.io.IOException


      Please see the following code sample:

      ---
      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      import java.nio.charset.StandardCharsets;
      import java.util.Base64;

      public class WrappingNonValidBase64Stream {

          public static void main(String[] args) throws IOException {
              Base64.getDecoder().wrap(
                      new ByteArrayInputStream(
                              "@".getBytes(StandardCharsets.US_ASCII)
                      )).read();
          }
      }
      ---

      The output is:

      Exception in thread "main" java.io.IOException: Illegal base64 character -1
      at java.util.Base64$DecInputStream.read(Base64.java:1279)
      at java.util.Base64$DecInputStream.read(Base64.java:1224)
      at WrappingNonValidBase64Stream.main(WrappingNonValidBase64Stream.java:12)

      On attempt to decode a stream consisting of one-character string "A" the message of thrown exception is different:

      Exception in thread "main" java.io.IOException: Base64 stream has un-decoded dangling byte(s).



      The following test will fail due to this issue:
      api/java_util/Base64/index.html#DecodingNonValidBase64Scheme


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

              Created:
              Updated:
              Resolved: