Search in sources :

Example 16 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testDecoder_extraChars_basic.

/**
     * Checks decoding of bytes containing a value outside of the allowed
     * {@link #TABLE_1 "basic" alphabet}.
     */
public void testDecoder_extraChars_basic() throws Exception {
    // uses Table 1
    Decoder basicDecoder = Base64.getDecoder();
    // Check failure cases common to both RFC4648 Table 1 and Table 2 decoding.
    checkDecoder_extraChars_common(basicDecoder);
    // Tests characters that are part of RFC4848 Table 2 but not Table 1.
    assertDecodeThrowsIAe(basicDecoder, "_aGVsbG8sIHdvcmx");
    assertDecodeThrowsIAe(basicDecoder, "aGV_sbG8sIHdvcmx");
    assertDecodeThrowsIAe(basicDecoder, "aGVsbG8sIHdvcmx_");
}
Also used : Decoder(java.util.Base64.Decoder)

Example 17 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testDecoder_decodeByteBuffer.

public void testDecoder_decodeByteBuffer() {
    Decoder decoder = Base64.getDecoder();
    byte[] emptyByteArray = new byte[0];
    ByteBuffer emptyByteBuffer = ByteBuffer.wrap(emptyByteArray);
    ByteBuffer emptyDecodedBuffer = decoder.decode(emptyByteBuffer);
    assertEquals(emptyByteBuffer, emptyDecodedBuffer);
    assertNotSame(emptyByteArray, emptyDecodedBuffer);
    // Test the two types of byte buffer.
    String inputString = "YWJjZWZnaGk=";
    byte[] input = inputString.getBytes(US_ASCII);
    String expectedString = "abcefghi";
    byte[] expectedBytes = expectedString.getBytes(US_ASCII);
    ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
    inputBuffer.put(input);
    inputBuffer.position(0);
    checkDecoder_decodeByteBuffer(decoder, inputBuffer, expectedBytes);
    inputBuffer = ByteBuffer.allocateDirect(input.length);
    inputBuffer.put(input);
    inputBuffer.position(0);
    checkDecoder_decodeByteBuffer(decoder, inputBuffer, expectedBytes);
}
Also used : Decoder(java.util.Base64.Decoder) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Decoder

use of java.util.Base64.Decoder in project j2objc by google.

the class Base64Test method testRoundTrip_allBytes_mime_lineLength.

/** check a range of possible line lengths */
public void testRoundTrip_allBytes_mime_lineLength() {
    Decoder decoder = Base64.getMimeDecoder();
    byte[] separator = new byte[] { '*' };
    checkRoundTrip_allBytes(Base64.getMimeEncoder(4, separator), decoder, wrapLines("*", ALL_BYTE_VALUES_ENCODED, 4));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(8, separator), decoder, wrapLines("*", ALL_BYTE_VALUES_ENCODED, 8));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(20, separator), decoder, wrapLines("*", ALL_BYTE_VALUES_ENCODED, 20));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(100, separator), decoder, wrapLines("*", ALL_BYTE_VALUES_ENCODED, 100));
    checkRoundTrip_allBytes(Base64.getMimeEncoder(Integer.MAX_VALUE & ~3, separator), decoder, wrapLines("*", ALL_BYTE_VALUES_ENCODED, Integer.MAX_VALUE & ~3));
}
Also used : Decoder(java.util.Base64.Decoder)

Example 19 with Decoder

use of java.util.Base64.Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64Golden method test1.

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();
    }
}
Also used : Encoder(java.util.Base64.Encoder) BASE64Encoder(sun.misc.BASE64Encoder) Decoder(java.util.Base64.Decoder) BASE64Decoder(sun.misc.BASE64Decoder)

Example 20 with Decoder

use of java.util.Base64.Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64 method testDecodeUnpadded.

private static void testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E' };
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 && ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 && ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
Also used : Base64(java.util.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

Decoder (java.util.Base64.Decoder)16 Encoder (java.util.Base64.Encoder)6 ByteBuffer (java.nio.ByteBuffer)4 Base64 (java.util.Base64)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Unauthorized401Exception (com.nike.riposte.server.error.exception.Unauthorized401Exception)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BASE64Decoder (sun.misc.BASE64Decoder)1 BASE64Encoder (sun.misc.BASE64Encoder)1