Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Save New Duplicate & Edit Just Text
- package io.netty.handler.codec;
- import de.melonbungee.javainteger.MelonBungee;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.ByteBufAllocator;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelOutboundHandlerAdapter;
- import io.netty.channel.ChannelPromise;
- import io.netty.handler.codec.EncoderException;
- import io.netty.util.ReferenceCountUtil;
- import io.netty.util.ReferenceCounted;
- import io.netty.util.internal.TypeParameterMatcher;
- public abstract class MessageToByteEncoder<I>
- extends ChannelOutboundHandlerAdapter {
- private final TypeParameterMatcher matcher;
- private final boolean preferDirect;
- private MelonBungee MelonBungee;
- protected MessageToByteEncoder() {
- this(true);
- }
- protected MessageToByteEncoder(Class<? extends I> outboundMessageType) {
- this(outboundMessageType, true);
- }
- protected MessageToByteEncoder(boolean preferDirect) {
- this.matcher = TypeParameterMatcher.find(this, MessageToByteEncoder.class, "I");
- this.preferDirect = preferDirect;
- }
- protected MessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect) {
- this.matcher = TypeParameterMatcher.get(outboundMessageType);
- this.preferDirect = preferDirect;
- }
- public boolean acceptOutboundMessage(Object msg) throws Exception {
- return this.matcher.match(msg);
- }
- @Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
- try {
- block14 : {
- ReferenceCounted buf = null;
- try {
- if (this.acceptOutboundMessage(msg)) {
- Object cast = msg;
- buf = this.allocateBuffer(ctx, (I) cast, this.preferDirect);
- try {
- this.encode(ctx, (I) cast, (ByteBuf)buf);
- }
- finally {
- ReferenceCountUtil.release(cast);
- }
- if (((ByteBuf)buf).isReadable()) {
- ctx.write(buf, promise);
- } else {
- buf.release();
- ctx.write(Unpooled.EMPTY_BUFFER, promise);
- }
- buf = null;
- break block14;
- }
- ctx.write(msg, promise);
- }
- catch (EncoderException e) {
- ctx.close();
- }
- catch (Throwable e) {
- ctx.close();
- }
- finally {
- if (buf != null) {
- buf.release();
- }
- }
- }
- } catch (Exception e) {
- MelonBungee.blockinquiry(null, ctx, ctx.channel().remoteAddress().toString());
- }
- }
- protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception {
- if (preferDirect) {
- return ctx.alloc().ioBuffer();
- }
- return ctx.alloc().heapBuffer();
- }
- protected abstract void encode(ChannelHandlerContext var1, I var2, ByteBuf var3) throws Exception;
- protected boolean isPreferDirect() {
- return this.preferDirect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement