List of usage examples for io.netty.handler.codec.base64 Base64 encode
public static ByteBuf encode(ByteBuf src, boolean breakLines)
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 av a 2s . c o m 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.couchbase.client.core.endpoint.AbstractGenericHandler.java
License:Apache License
/** * Add basic authentication headers to a {@link HttpRequest}. * * The given information is Base64 encoded and the authorization header is set appropriately. Since this needs * to be done for every request, it is refactored out. * * @param ctx the handler context./*from ww w .j a v a 2s . c o m*/ * @param request the request where the header should be added. * @param user the username for auth. * @param password the password for auth. */ public static void addHttpBasicAuth(final ChannelHandlerContext ctx, final HttpRequest request, final String user, final String password) { final String pw = password == null ? "" : password; ByteBuf raw = ctx.alloc().buffer(user.length() + pw.length() + 1); raw.writeBytes((user + ":" + pw).getBytes(CHARSET)); ByteBuf encoded = Base64.encode(raw, false); request.headers().add(HttpHeaders.Names.AUTHORIZATION, "Basic " + encoded.toString(CHARSET)); encoded.release(); raw.release(); }
From source file:com.otcdlink.chiron.downend.Http11ProxyHandler.java
License:Apache License
public Http11ProxyHandler(SocketAddress proxyAddress, String username, String password) { super(proxyAddress); if (username == null) { throw new NullPointerException("username"); }//w ww . j a v a 2 s . c o m if (password == null) { throw new NullPointerException("password"); } this.username = username; this.password = password; ByteBuf authz = Unpooled.copiedBuffer(username + ':' + password, CharsetUtil.UTF_8); ByteBuf authzBase64 = Base64.encode(authz, false); authorization = new AsciiString("Basic " + authzBase64.toString(CharsetUtil.US_ASCII)); authz.release(); authzBase64.release(); }
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 . j av a 2 s. c om*/ encodedString.release(); return encodedUnpaddedString; }