Search in sources :

Example 16 with Base64

use of java.util.Base64 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 17 with Base64

use of java.util.Base64 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 18 with Base64

use of java.util.Base64 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 19 with Base64

use of java.util.Base64 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 20 with Base64

use of java.util.Base64 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)9 Base64 (java.util.Base64)8 ByteBuffer (java.nio.ByteBuffer)5 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ConsumerContextConfig (com.dell.cpsd.service.common.client.context.ConsumerContextConfig)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Unauthorized401Exception (com.nike.riposte.server.error.exception.Unauthorized401Exception)1 CompressionType (com.yahoo.pulsar.client.api.CompressionType)1 Message (com.yahoo.pulsar.client.api.Message)1 MessageBuilder (com.yahoo.pulsar.client.api.MessageBuilder)1 Producer (com.yahoo.pulsar.client.api.Producer)1 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)1 MessageRoutingMode (com.yahoo.pulsar.client.api.ProducerConfiguration.MessageRoutingMode)1 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)1 ObjectMapperFactory (com.yahoo.pulsar.common.util.ObjectMapperFactory)1 WebSocketError (com.yahoo.pulsar.websocket.WebSocketError)1