Example usage for io.netty.handler.codec.base64 Base64Dialect URL_SAFE

List of usage examples for io.netty.handler.codec.base64 Base64Dialect URL_SAFE

Introduction

In this page you can find the example usage for io.netty.handler.codec.base64 Base64Dialect URL_SAFE.

Prototype

Base64Dialect URL_SAFE

To view the source code for io.netty.handler.codec.base64 Base64Dialect URL_SAFE.

Click Source Link

Document

Base64-like encoding that is URL-safe as described in the Section 4 of <a href="http://www.faqs.org/rfcs/rfc3548.html">RFC3548</a>.

Usage

From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java

License:Apache License

public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out, ByteBufAllocator allocator,
        int limit) throws IOException {
    boolean jsonpMode = jsonpIndex != null;

    ByteBuf buf = allocateBuffer(allocator);

    int i = 0;/*from w w  w . j  a  v  a2 s. com*/
    while (true) {
        Packet packet = packets.poll();
        if (packet == null || i == limit) {
            break;
        }

        ByteBuf packetBuf = allocateBuffer(allocator);
        encodePacket(packet, packetBuf, allocator, true);

        int packetSize = packetBuf.writerIndex();
        buf.writeBytes(toChars(packetSize));
        buf.writeBytes(B64_DELIMITER);
        buf.writeBytes(packetBuf);

        packetBuf.release();

        i++;

        for (ByteBuf attachment : packet.getAttachments()) {
            ByteBuf encodedBuf = Base64.encode(attachment, Base64Dialect.URL_SAFE);
            buf.writeBytes(toChars(encodedBuf.readableBytes() + 2));
            buf.writeBytes(B64_DELIMITER);
            buf.writeBytes(BINARY_HEADER);
            buf.writeBytes(encodedBuf);
        }
    }

    if (jsonpMode) {
        out.writeBytes(JSONP_HEAD);
        out.writeBytes(toChars(jsonpIndex));
        out.writeBytes(JSONP_START);
    }

    processUtf8(buf, out, jsonpMode);
    buf.release();

    if (jsonpMode) {
        out.writeBytes(JSONP_END);
    }
}

From source file:com.turo.pushy.apns.auth.AuthenticationToken.java

License:Open Source License

static String encodeUnpaddedBase64UrlString(final byte[] data) {
    final ByteBuf wrappedString = Unpooled.wrappedBuffer(data);
    final ByteBuf encodedString = Base64.encode(wrappedString, Base64Dialect.URL_SAFE);

    final String encodedUnpaddedString = encodedString.toString(StandardCharsets.US_ASCII).replace("=", "");

    wrappedString.release();/*from w w w  .jav a  2s  .  c o m*/
    encodedString.release();

    return encodedUnpaddedString;
}

From source file:com.turo.pushy.apns.auth.AuthenticationToken.java

License:Open Source License

static byte[] decodeBase64UrlEncodedString(final String base64UrlEncodedString) {
    final String paddedBase64UrlEncodedString;

    switch (base64UrlEncodedString.length() % 4) {
    case 2: {/*from w  w w .jav a  2  s. c o  m*/
        paddedBase64UrlEncodedString = base64UrlEncodedString + "==";
        break;
    }

    case 3: {
        paddedBase64UrlEncodedString = base64UrlEncodedString + "=";
        break;
    }

    default: {
        paddedBase64UrlEncodedString = base64UrlEncodedString;
    }
    }

    final ByteBuf base64EncodedByteBuf = Unpooled
            .wrappedBuffer(paddedBase64UrlEncodedString.getBytes(StandardCharsets.US_ASCII));

    final ByteBuf decodedByteBuf = Base64.decode(base64EncodedByteBuf, Base64Dialect.URL_SAFE);
    final byte[] decodedBytes = new byte[decodedByteBuf.readableBytes()];

    decodedByteBuf.readBytes(decodedBytes);

    base64EncodedByteBuf.release();
    decodedByteBuf.release();

    return decodedBytes;
}