Example usage for io.netty.buffer ByteBufOutputStream writeInt

List of usage examples for io.netty.buffer ByteBufOutputStream writeInt

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufOutputStream writeInt.

Prototype

@Override
    public void writeInt(int v) throws IOException 

Source Link

Usage

From source file:org.apache.giraph.comm.netty.handler.ResponseEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("write(" + ctx + "," + msg);
    }/*from   w ww.j av a2  s  .c  om*/

    if (!(msg instanceof WritableRequest)) {
        throw new IllegalArgumentException("encode: cannot encode message of type " + msg.getClass()
                + " since it is not an instance of an implementation of " + " WritableRequest.");
    }
    @SuppressWarnings("unchecked")
    WritableRequest writableRequest = (WritableRequest) msg;

    ByteBuf buf = ctx.alloc().buffer(10);
    ByteBufOutputStream output = new ByteBufOutputStream(buf);

    if (LOG.isDebugEnabled()) {
        LOG.debug("encode: Encoding a message of type " + msg.getClass());
    }

    // Space is reserved now to be filled later by the serialize request size
    output.writeInt(0);
    // write type of object.
    output.writeByte(writableRequest.getType().ordinal());
    // write the object itself.
    writableRequest.write(output);

    output.flush();
    output.close();

    // Set the correct size at the end.
    buf.setInt(0, buf.writerIndex() - SIZE_OF_INT);

    if (LOG.isDebugEnabled()) {
        LOG.debug("encode: Encoding a message of type " + msg.getClass());
    }
    ctx.write(buf, promise);
    /*if[HADOOP_NON_SECURE]
    else[HADOOP_NON_SECURE]*/
    if (writableRequest.getType() == RequestType.SASL_COMPLETE_REQUEST) {
        // We are sending to the client a SASL_COMPLETE response (created by
        // the SaslServer handler). The SaslServer handler has removed itself
        // from the pipeline after creating this response, and now it's time for
        // the ResponseEncoder to remove itself also.
        if (LOG.isDebugEnabled()) {
            LOG.debug("encode: Removing RequestEncoder handler: no longer needed," + " since client: "
                    + ctx.channel().remoteAddress() + " has " + "completed authenticating.");
        }
        ctx.pipeline().remove(this);
    }
    /*end[HADOOP_NON_SECURE]*/
    ctx.write(buf, promise);
}