Search in sources :

Example 11 with Decoder

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

the class Base64Test method testDecoder_decodeArrayToArray.

/**
     * Tests {@link Decoder#decode(byte[], byte[])} for correctness as well as
     * for consistency with other methods tested elsewhere.
     */
public void testDecoder_decodeArrayToArray() {
    Decoder decoder = Base64.getDecoder();
    // Empty input
    assertEquals(0, decoder.decode(new byte[0], new byte[0]));
    // Test data for non-empty input
    String inputString = "YWJjZWZnaGk=";
    byte[] input = inputString.getBytes(US_ASCII);
    String expectedString = "abcefghi";
    byte[] decodedBytes = expectedString.getBytes(US_ASCII);
    // check test data consistency with other methods that are tested elsewhere
    assertRoundTrip(Base64.getEncoder(), decoder, inputString, decodedBytes);
    // Non-empty input: output array too short
    byte[] tooShort = new byte[decodedBytes.length - 1];
    try {
        decoder.decode(input, tooShort);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    // Non-empty input: output array longer than required
    byte[] tooLong = new byte[decodedBytes.length + 1];
    int tooLongBytesDecoded = decoder.decode(input, tooLong);
    assertEquals(decodedBytes.length, tooLongBytesDecoded);
    assertEquals(0, tooLong[tooLong.length - 1]);
    assertArrayPrefixEquals(tooLong, decodedBytes.length, decodedBytes);
    // Non-empty input: output array has exact minimum required size
    byte[] justRight = new byte[decodedBytes.length];
    int justRightBytesDecoded = decoder.decode(input, justRight);
    assertEquals(decodedBytes.length, justRightBytesDecoded);
    assertArrayEquals(decodedBytes, justRight);
}
Also used : Decoder(java.util.Base64.Decoder)

Example 12 with Decoder

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

the class Base64Test method testRoundtrip_wrap_basic.

public void testRoundtrip_wrap_basic() throws Exception {
    Encoder encoder = Base64.getEncoder();
    Decoder decoder = Base64.getDecoder();
    checkRoundTrip_wrapInputStream(encoder, decoder);
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Example 13 with Decoder

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

the class Base64Test method testEncoder_lineLength.

public void testEncoder_lineLength() throws Exception {
    String in_56 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd";
    String in_57 = in_56 + "e";
    String in_58 = in_56 + "ef";
    String in_59 = in_56 + "efg";
    String in_60 = in_56 + "efgh";
    String in_61 = in_56 + "efghi";
    String prefix = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFi";
    String out_56 = prefix + "Y2Q=";
    String out_57 = prefix + "Y2Rl";
    String out_58 = prefix + "Y2Rl\r\nZg==";
    String out_59 = prefix + "Y2Rl\r\nZmc=";
    String out_60 = prefix + "Y2Rl\r\nZmdo";
    String out_61 = prefix + "Y2Rl\r\nZmdoaQ==";
    Encoder encoder = Base64.getMimeEncoder();
    Decoder decoder = Base64.getMimeDecoder();
    assertEquals("", encodeFromAscii(encoder, decoder, ""));
    assertEquals(out_56, encodeFromAscii(encoder, decoder, in_56));
    assertEquals(out_57, encodeFromAscii(encoder, decoder, in_57));
    assertEquals(out_58, encodeFromAscii(encoder, decoder, in_58));
    assertEquals(out_59, encodeFromAscii(encoder, decoder, in_59));
    assertEquals(out_60, encodeFromAscii(encoder, decoder, in_60));
    assertEquals(out_61, encodeFromAscii(encoder, decoder, in_61));
    encoder = Base64.getUrlEncoder();
    decoder = Base64.getUrlDecoder();
    assertEquals(out_56.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_56));
    assertEquals(out_57.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_57));
    assertEquals(out_58.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_58));
    assertEquals(out_59.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_59));
    assertEquals(out_60.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_60));
    assertEquals(out_61.replaceAll("\r\n", ""), encodeFromAscii(encoder, decoder, in_61));
}
Also used : Encoder(java.util.Base64.Encoder) Decoder(java.util.Base64.Decoder)

Example 14 with Decoder

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

the class Base64Test method testDecoder_extraChars_url.

/**
     * Checks decoding of bytes containing a value outside of the allowed
     * {@link #TABLE_2 url alphabet}.
     */
public void testDecoder_extraChars_url() throws Exception {
    // uses Table 2
    Decoder urlDecoder = Base64.getUrlDecoder();
    // Check failure cases common to both RFC4648 table 1 and table 2 decoding.
    checkDecoder_extraChars_common(urlDecoder);
    // Tests characters that are part of RFC4848 Table 1 but not Table 2.
    assertDecodeThrowsIAe(urlDecoder, "/aGVsbG8sIHdvcmx");
    assertDecodeThrowsIAe(urlDecoder, "aGV/sbG8sIHdvcmx");
    assertDecodeThrowsIAe(urlDecoder, "aGVsbG8sIHdvcmx/");
}
Also used : Decoder(java.util.Base64.Decoder)

Example 15 with Decoder

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

the class Base64Test method testDecoder_decodeByteBuffer_invalidData.

public void testDecoder_decodeByteBuffer_invalidData() {
    Decoder decoder = Base64.getDecoder();
    // Test the two types of byte buffer.
    String inputString = "AAAA AAAA";
    byte[] input = inputString.getBytes(US_ASCII);
    ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
    inputBuffer.put(input);
    inputBuffer.position(0);
    checkDecoder_decodeByteBuffer_invalidData(decoder, inputBuffer);
    inputBuffer = ByteBuffer.allocateDirect(input.length);
    inputBuffer.put(input);
    inputBuffer.position(0);
    checkDecoder_decodeByteBuffer_invalidData(decoder, inputBuffer);
}
Also used : Decoder(java.util.Base64.Decoder) ByteBuffer(java.nio.ByteBuffer)

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