/** * Creates a new embedder whose pipeline is composed of the specified * handlers. * * @param bufferFactory the {@link ChannelBufferFactory} to be used when * creating a new buffer. */ protected AbstractCodecEmbedder(ChannelBufferFactory bufferFactory, ChannelHandler... handlers) { this(handlers); getChannel().getConfig().setBufferFactory(bufferFactory); }
/** * Create a new {@link ChannelBuffer} which is used for the cumulation. * Sub-classes may override this. * * @param ctx {@link ChannelHandlerContext} for this handler * @return buffer the {@link ChannelBuffer} which is used for cumulation */ protected ChannelBuffer newCumulationBuffer( ChannelHandlerContext ctx, int minimumCapacity) { ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory(); return factory.getBuffer(Math.max(minimumCapacity, 256)); }
private ChannelBuffer buffer(ChannelHandlerContext ctx) throws Exception { ChannelBuffer buf = buffer.get(); if (buf == null) { ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory(); buf = ChannelBuffers.dynamicBuffer(factory); if (buffer.compareAndSet(null, buf)) { boolean success = false; try { oout = newObjectOutputStream(new ChannelBufferOutputStream(buf)); success = true; } finally { if (!success) { oout = null; } } } else { buf = buffer.get(); } } return buf; } }
@Override protected ChannelBuffer newCumulationBuffer(ChannelHandlerContext ctx, int minimumCapacity) { ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory(); if (allocateFullBuffer) { return factory.getBuffer(frameLength); } return super.newCumulationBuffer(ctx, minimumCapacity); } }
@Override protected Object encode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg instanceof String) { return copiedBuffer( ctx.getChannel().getConfig().getBufferFactory().getDefaultOrder(), (String) msg, charset); } return msg; } }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(dynamicBuffer( estimatedLength, ctx.getChannel().getConfig().getBufferFactory())); bout.write(LENGTH_PLACEHOLDER); ObjectOutputStream oout = new CompactObjectOutputStream(bout); oout.writeObject(msg); oout.flush(); oout.close(); ChannelBuffer encoded = bout.buffer(); encoded.setInt(0, encoded.writerIndex() - 4); return encoded; } }
@Override protected Object encode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (msg instanceof MessageLite) { byte[] array = ((MessageLite) msg).toByteArray(); return ctx.getChannel().getConfig().getBufferFactory().getBuffer(array, 0, array.length); } if (msg instanceof MessageLite.Builder) { byte[] array = ((MessageLite.Builder) msg).build().toByteArray(); return ctx.getChannel().getConfig().getBufferFactory().getBuffer(array, 0, array.length); } return msg; } }
@Override public void childChannelOpen( ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { // Apply child options. try { e.getChildChannel().getConfig().setOptions(childOptions); } catch (Throwable t) { fireExceptionCaught(e.getChildChannel(), t); } ctx.sendUpstream(e); }
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (!(msg instanceof ChannelBuffer) || finished.get()) { return msg; } final ChannelBuffer uncompressed = (ChannelBuffer) msg; final int uncompressedLen = uncompressed.readableBytes(); if (uncompressedLen == 0) { return uncompressed; } final byte[] in = new byte[uncompressedLen]; uncompressed.readBytes(in); final int sizeEstimate = estimateCompressedSize(uncompressedLen); final ChannelBuffer compressed = ChannelBuffers.dynamicBuffer(sizeEstimate, channel.getConfig().getBufferFactory()); synchronized (deflater) { if (isGzip()) { crc.update(in); if (writeHeader) { compressed.writeBytes(gzipHeader); writeHeader = false; } } deflater.setInput(in); while (!deflater.needsInput()) { deflate(compressed); } } return compressed; }
ChannelBuffer header = channel.getConfig().getBufferFactory().getBuffer(body.order(), lengthFieldLength);
result = ctx.getChannel().getConfig().getBufferFactory().getBuffer( uncompressed.order(), out, 0, z.next_out_index); } else {
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { Marshaller marshaller = provider.getMarshaller(ctx); ChannelBufferByteOutput output = new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), 256); marshaller.start(output); marshaller.writeObject(msg); marshaller.finish(); marshaller.close(); return output.getBuffer(); } }
boolean success = false; try { ch.getConfig().setOptions(getOptions()); success = true; } finally {
boolean success = false; try { ch.getConfig().setOptions(getOptions()); success = true; } finally {
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (!(msg instanceof ChannelBuffer)) { return msg; } ChannelBuffer body = (ChannelBuffer) msg; int length = body.readableBytes(); ChannelBuffer header = channel.getConfig().getBufferFactory().getBuffer( body.order(), CodedOutputStream.computeRawVarint32Size(length)); CodedOutputStream codedOutputStream = CodedOutputStream .newInstance(new ChannelBufferOutputStream(header)); codedOutputStream.writeRawVarint32(length); codedOutputStream.flush(); return wrappedBuffer(header, body); }
final ChannelBuffer footer = ChannelBuffers.dynamicBuffer(ctx.getChannel().getConfig().getBufferFactory()); final boolean gzip = isGzip(); synchronized (deflater) {
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { Marshaller marshaller = provider.getMarshaller(ctx); ChannelBufferByteOutput output = new ChannelBufferByteOutput( ctx.getChannel().getConfig().getBufferFactory(), estimatedLength); output.getBuffer().writeBytes(LENGTH_PLACEHOLDER); marshaller.start(output); marshaller.writeObject(msg); marshaller.finish(); marshaller.close(); ChannelBuffer encoded = output.getBuffer(); encoded.setInt(0, encoded.writerIndex() - 4); return encoded; }
ch.getConfig().setOptions(getOptions()); success = true; } finally {
ChannelBuffer encoded = channel.getConfig().getBufferFactory() .getBuffer(data.order(), data.readableBytes() + 2); encoded.writeByte((byte) 0x00); ChannelBuffer encoded = channel.getConfig().getBufferFactory().getBuffer(data.order(), 2); encoded.writeByte((byte) 0xFF); encoded.writeByte((byte) 0x00); ChannelBuffer encoded = channel.getConfig().getBufferFactory().getBuffer(data.order(), dataLen + 5);