當前位置: 首頁>>代碼示例>>Java>>正文


Java Base64.getMimeDecoder方法代碼示例

本文整理匯總了Java中java.util.Base64.getMimeDecoder方法的典型用法代碼示例。如果您正苦於以下問題:Java Base64.getMimeDecoder方法的具體用法?Java Base64.getMimeDecoder怎麽用?Java Base64.getMimeDecoder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.Base64的用法示例。


在下文中一共展示了Base64.getMimeDecoder方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test1

import java.util.Base64; //導入方法依賴的package包/類
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:14,代碼來源:TestBase64Golden.java

示例2: testDecodeIgnoredAfterPadding

import java.util.Base64; //導入方法依賴的package包/類
private static void testDecodeIgnoredAfterPadding() throws Throwable {
    for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
        byte[][] src = new byte[][] {
            "A".getBytes("ascii"),
            "AB".getBytes("ascii"),
            "ABC".getBytes("ascii"),
            "ABCD".getBytes("ascii"),
            "ABCDE".getBytes("ascii")
        };
        Base64.Encoder encM = Base64.getMimeEncoder();
        Base64.Decoder decM = Base64.getMimeDecoder();
        Base64.Encoder enc = Base64.getEncoder();
        Base64.Decoder dec = Base64.getDecoder();
        for (int i = 0; i < src.length; i++) {
            // decode(byte[])
            byte[] encoded = encM.encode(src[i]);
            encoded = Arrays.copyOf(encoded, encoded.length + 1);
            encoded[encoded.length - 1] = nonBase64;
            checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
            byte[] decoded = new byte[src[i].length];
            decM.decode(encoded, decoded);
            checkEqual(decoded, src[i], "Non-base64 char is not ignored");

            try {
                dec.decode(encoded);
                throw new RuntimeException("No IAE for non-base64 char");
            } catch (IllegalArgumentException iae) {}
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:31,代碼來源:TestBase64.java

示例3: testMalformedPadding

import java.util.Base64; //導入方法依賴的package包/類
private static void testMalformedPadding() throws Throwable {
    Object[] data = new Object[] {
        "$=#",       "",      0,      // illegal ending unit
        "A",         "",      0,      // dangling single byte
        "A=",        "",      0,
        "A==",       "",      0,
        "QUJDA",     "ABC",   4,
        "QUJDA=",    "ABC",   4,
        "QUJDA==",   "ABC",   4,

        "=",         "",      0,      // unnecessary padding
        "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"

        "AA=",       "",      0,      // incomplete padding
        "QQ=",       "",      0,
        "QQ=N",      "",      0,      // incorrect padding
        "QQ=?",      "",      0,
        "QUJDQQ=",   "ABC",   4,
        "QUJDQQ=N",  "ABC",   4,
        "QUJDQQ=?",  "ABC",   4,
    };

    Base64.Decoder[] decs = new Base64.Decoder[] {
        Base64.getDecoder(),
        Base64.getUrlDecoder(),
        Base64.getMimeDecoder()
    };

    for (Base64.Decoder dec : decs) {
        for (int i = 0; i < data.length; i += 3) {
            final String srcStr = (String)data[i];
            final byte[] srcBytes = srcStr.getBytes("ASCII");
            final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
            byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
            int pos = (Integer)data[i + 2];

            // decode(byte[])
            checkIAE(() -> dec.decode(srcBytes));

            // decode(String)
            checkIAE(() -> dec.decode(srcStr));

            // decode(ByteBuffer)
            checkIAE(() -> dec.decode(srcBB));

            // wrap stream
            checkIOE(new Testable() {
                public void test() throws IOException {
                    try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                        while (is.read() != -1);
                    }
            }});
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:56,代碼來源:TestBase64.java

示例4: testMalformedPadding

import java.util.Base64; //導入方法依賴的package包/類
private static void testMalformedPadding() throws Throwable {
   Object[] data = new Object[] {
       "$=#",       "",      0,      // illegal ending unit
       "A",         "",      0,      // dangling single byte
       "A=",        "",      0,
       "A==",       "",      0,
       "QUJDA",     "ABC",   4,
       "QUJDA=",    "ABC",   4,
       "QUJDA==",   "ABC",   4,

       "=",         "",      0,      // unnecessary padding
       "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"

       "AA=",       "",      0,      // incomplete padding
       "QQ=",       "",      0,
       "QQ=N",      "",      0,      // incorrect padding
       "QQ=?",      "",      0,
       "QUJDQQ=",   "ABC",   4,
       "QUJDQQ=N",  "ABC",   4,
       "QUJDQQ=?",  "ABC",   4,
   };

   Base64.Decoder[] decs = new Base64.Decoder[] {
       Base64.getDecoder(),
       Base64.getUrlDecoder(),
       Base64.getMimeDecoder()
   };

   for (Base64.Decoder dec : decs) {
       for (int i = 0; i < data.length; i += 3) {
           final String srcStr = (String)data[i];
           final byte[] srcBytes = srcStr.getBytes("ASCII");
           final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
           byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
           int pos = (Integer)data[i + 2];

           // decode(byte[])
           checkIAE(() -> dec.decode(srcBytes));

           // decode(String)
           checkIAE(() -> dec.decode(srcStr));

           // decode(ByteBuffer)
           checkIAE(() -> dec.decode(srcBB));

           // wrap stream
           checkIOE(new Testable() {
               public void test() throws IOException {
                   try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                       while (is.read() != -1);
                   }
           }});
       }
   }

   // anything left after padding is "invalid"/IAE, if
   // not MIME. In case of MIME, non-base64 character(s)
   // is ignored.
   checkIAE(() -> Base64.getDecoder().decode("AA==\u00D2"));
   checkIAE(() -> Base64.getUrlDecoder().decode("AA==\u00D2"));
   Base64.getMimeDecoder().decode("AA==\u00D2");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:63,代碼來源:TestBase64.java


注:本文中的java.util.Base64.getMimeDecoder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。