@Override protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception { ByteBuf buffer = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), charset, lineSeparator.length); buffer.writeBytes(lineSeparator); out.add(buffer); } }
@Override protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception { // find codec Class<? extends Message> clazz = msg.getClass(); CodecRegistration reg = protocol.getCodecRegistration(clazz); if (reg == null) { throw new EncoderException("Unknown message type: " + clazz + "."); } // write header ByteBuf headerBuf = ctx.alloc().buffer(8); ByteBufUtils.writeVarInt(headerBuf, reg.getOpcode()); // write body ByteBuf messageBuf = ctx.alloc().buffer(); messageBuf = reg.getCodec().encode(messageBuf, msg); out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf)); }
out.add(aggregated); return; CompositeByteBuf content = ctx.alloc().compositeBuffer(maxCumulationBufferComponents); if (m instanceof ByteBufHolder) { appendPartialContent(content, ((ByteBufHolder) m).content()); final C m = (C) msg; if (content.readableBytes() > maxContentLength - m.content().readableBytes()) { out.add(currentMessage); currentMessage = null;
/** * Creates an HTTP2-Settings header with the given payload. The payload buffer is released. */ private static ByteBuf createSettingsFrame(ChannelHandlerContext ctx, ByteBuf payload) { ByteBuf frame = ctx.alloc().buffer(FRAME_HEADER_LENGTH + payload.readableBytes()); writeFrameHeader(frame, payload.readableBytes(), SETTINGS, new Http2Flags(), 0); frame.writeBytes(payload); payload.release(); return frame; } }
@Override protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) { // This is a work around on https://github.com/netty/netty/commit/55fbf007f04fbba7bf50028f3c8b35d6c5ea5947 // Right now we need a copy when sending a message on the server otherwise messages won't be resent to the client ByteBuf frame = ctx.alloc().buffer(length); frame.writeBytes(buffer, index, length); return frame.skipBytes(DataConstants.SIZE_INT); } }
@Override protected final ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception { int sizeEstimate = (int) Math.ceil(msg.readableBytes() * 1.001) + 12; if (writeHeader) { switch (wrapper) { case GZIP: sizeEstimate += gzipHeader.length; break; case ZLIB: sizeEstimate += 2; // first two magic bytes break; default: // no op } } return ctx.alloc().heapBuffer(sizeEstimate); }
private void write(XHRPostMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) { ByteBuf out = encoder.allocateBuffer(ctx.alloc()); out.writeBytes(OK); sendMessage(msg, ctx.channel(), out, "text/html", promise, HttpResponseStatus.OK); }
final ByteBuf out = encoder.allocateBuffer(ctx.alloc()); encoder.encodePacket(packet, out, ctx.alloc(), true); writeFutureList.add(ctx.channel().writeAndFlush(res)); } else { out.release(); ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc()); outBuf.writeByte(4); outBuf.writeBytes(buf); if (log.isTraceEnabled()) { log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId());
private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException { ByteBuf out = null; ChannelPromise promise = null; ByteBufAllocator alloc = ctx.alloc(); boolean needUnwrap = false; ByteBuf buf = null; out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.nioBufferCount()); buf.release(); buf = null; promise.tryFailure(SSLENGINE_CLOSED); buf.release(); buf.release();
@Override protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception { ByteBuf buffer = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), charset, lineSeparator.length); buffer.writeBytes(lineSeparator); out.add(buffer); } }
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { int length = msg.readableBytes() + lengthAdjustment; if (lengthIncludesLengthFieldLength) { length += lengthFieldLength; "length does not fit into a byte: " + length); out.add(ctx.alloc().buffer(1).order(byteOrder).writeByte((byte) length)); break; case 2: "length does not fit into a short integer: " + length); out.add(ctx.alloc().buffer(2).order(byteOrder).writeShort((short) length)); break; case 3: "length does not fit into a medium integer: " + length); out.add(ctx.alloc().buffer(3).order(byteOrder).writeMedium(length)); break; case 4: out.add(ctx.alloc().buffer(4).order(byteOrder).writeInt(length)); break; case 8: out.add(ctx.alloc().buffer(8).order(byteOrder).writeLong(length)); break; default:
@Override public void write(byte[] b, int off, int len) throws IOException { ByteBuf buffer = ctx.alloc().buffer(len); buffer.writeBytes(b, off, len); ctx.writeAndFlush(new DefaultHttp2DataFrame(buffer, false)); }
@Override protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf in, boolean preferDirect) throws Exception { final int length = in.readableBytes(); final int maxOutputLength = maxOutputBufferLength(length); return ctx.alloc().ioBuffer(maxOutputLength); }
ByteBuf footer = ctx.alloc().heapBuffer(); if (writeHeader && wrapper == ZlibWrapper.GZIP) { footer.writeBytes(gzipHeader); footer = ctx.alloc().heapBuffer();
/** * Decodes the settings header and returns a {@link Http2Settings} object. */ private Http2Settings decodeSettingsHeader(ChannelHandlerContext ctx, CharSequence settingsHeader) throws Http2Exception { ByteBuf header = ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(settingsHeader), CharsetUtil.UTF_8); try { // Decode the SETTINGS payload. ByteBuf payload = Base64.decode(header, URL_SAFE); // Create an HTTP/2 frame for the settings. ByteBuf frame = createSettingsFrame(ctx, payload); // Decode the SETTINGS frame and return the settings object. return decodeSettings(ctx, frame); } finally { header.release(); } }