Search in sources :

Example 21 with Base64

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

the class Base64Test method testEncoder_nonPrintableBytes.

public void testEncoder_nonPrintableBytes() throws Exception {
    Encoder encoder = Base64.getUrlEncoder();
    assertEquals("", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 0)));
    assertEquals("_w==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 1)));
    assertEquals("_-4=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 2)));
    assertEquals("_-7d", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 3)));
    assertEquals("_-7dzA==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 4)));
    assertEquals("_-7dzLs=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 5)));
    assertEquals("_-7dzLuq", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 6)));
    assertEquals("_-7dzLuqmQ==", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 7)));
    assertEquals("_-7dzLuqmYg=", encoder.encodeToString(copyOfRange(SAMPLE_NON_ASCII_BYTES, 0, 8)));
}
Also used : Encoder(java.util.Base64.Encoder)

Example 22 with Base64

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

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

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

use of java.util.Base64 in project pulsar by yahoo.

the class ProducerHandler method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    ProducerMessage sendRequest;
    byte[] rawPayload = null;
    String requestContext = null;
    try {
        sendRequest = ObjectMapperFactory.getThreadLocal().readValue(message, ProducerMessage.class);
        requestContext = sendRequest.context;
        rawPayload = Base64.getDecoder().decode(sendRequest.payload);
    } catch (IOException e) {
        sendAckResponse(new ProducerAck(FailedToDeserializeFromJSON, e.getMessage(), null, null));
        return;
    } catch (IllegalArgumentException e) {
        String msg = format("Invalid Base64 message-payload error=%s", e.getMessage());
        sendAckResponse(new ProducerAck(PayloadEncodingError, msg, null, requestContext));
        return;
    }
    MessageBuilder builder = MessageBuilder.create().setContent(rawPayload);
    if (sendRequest.properties != null) {
        builder.setProperties(sendRequest.properties);
    }
    if (sendRequest.key != null) {
        builder.setKey(sendRequest.key);
    }
    if (sendRequest.replicationClusters != null) {
        builder.setReplicationClusters(sendRequest.replicationClusters);
    }
    Message msg = builder.build();
    producer.sendAsync(msg).thenAccept(msgId -> {
        if (isConnected()) {
            String messageId = Base64.getEncoder().encodeToString(msgId.toByteArray());
            sendAckResponse(new ProducerAck(messageId, sendRequest.context));
        }
    }).exceptionally(exception -> {
        sendAckResponse(new ProducerAck(UnknownError, exception.getMessage(), null, sendRequest.context));
        return null;
    });
}
Also used : ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) WebSocketError(com.yahoo.pulsar.websocket.WebSocketError) Logger(org.slf4j.Logger) Producer(com.yahoo.pulsar.client.api.Producer) LoggerFactory(org.slf4j.LoggerFactory) MessageRoutingMode(com.yahoo.pulsar.client.api.ProducerConfiguration.MessageRoutingMode) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) String.format(java.lang.String.format) ObjectMapperFactory(com.yahoo.pulsar.common.util.ObjectMapperFactory) TimeUnit(java.util.concurrent.TimeUnit) MessageBuilder(com.yahoo.pulsar.client.api.MessageBuilder) Base64(java.util.Base64) HttpServletRequest(javax.servlet.http.HttpServletRequest) CompressionType(com.yahoo.pulsar.client.api.CompressionType) Session(org.eclipse.jetty.websocket.api.Session) Message(com.yahoo.pulsar.client.api.Message) ProducerAck(com.yahoo.pulsar.websocket.data.ProducerAck) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) MessageBuilder(com.yahoo.pulsar.client.api.MessageBuilder) ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) Message(com.yahoo.pulsar.client.api.Message) ProducerMessage(com.yahoo.pulsar.websocket.data.ProducerMessage) IOException(java.io.IOException) ProducerAck(com.yahoo.pulsar.websocket.data.ProducerAck)

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