de.dentrassi.varlink.internal.NullByteEncoder.java Source code

Java tutorial

Introduction

Here is the source code for de.dentrassi.varlink.internal.NullByteEncoder.java

Source

/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jens Reimann - initial API and implementation
 *******************************************************************************/
package de.dentrassi.varlink.internal;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

@Sharable
public class NullByteEncoder extends ChannelOutboundHandlerAdapter {

    public static final NullByteEncoder INSTANCE = new NullByteEncoder();

    private NullByteEncoder() {
    }

    @Override
    public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
            throws Exception {

        if (msg instanceof ByteBuf) {
            ((ByteBuf) msg).writeByte(0);
        }
        ctx.write(msg);
    }

}