Example usage for io.netty.buffer ByteBuf writeMedium

List of usage examples for io.netty.buffer ByteBuf writeMedium

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeMedium.

Prototype

public abstract ByteBuf writeMedium(int value);

Source Link

Document

Sets the specified 24-bit medium integer at the current writerIndex and increases the writerIndex by 3 in this buffer.

Usage

From source file:com.tesora.dve.db.mysql.portal.protocol.Packet.java

License:Open Source License

public static int encodeFullPayload(int sequenceStart, ByteBuf payloadHolder, ByteBuf destination) {
    ByteBuf leBuf = destination.order(ByteOrder.LITTLE_ENDIAN);

    //TODO: this loop is identical to the one below, except it doesn't consume the source or update the destination.  Consolidate?
    //calculate the size of the final encoding, so we resize the destination at most once.
    int payloadRemaining = payloadHolder.readableBytes();
    int outputSize = 0;
    do {/*from   w w  w  .  j  ava  2 s.c  om*/
        outputSize += 4; //header
        int chunkLength = Math.min(payloadRemaining, MAX_PAYLOAD);
        outputSize += chunkLength;
        payloadRemaining -= chunkLength;
        if (chunkLength == MAX_PAYLOAD)
            outputSize += 4; //need one more packet if last fragment is exactly 0xFFFF long.
    } while (payloadRemaining > 0);

    leBuf.ensureWritable(outputSize);

    int sequenceIter = sequenceStart;
    boolean lastChunkWasMaximumLength;
    do {
        int initialSize = payloadHolder.readableBytes();
        int maxSlice = MAX_PAYLOAD;
        int sendingPayloadSize = Math.min(maxSlice, initialSize);
        lastChunkWasMaximumLength = (sendingPayloadSize == maxSlice); //need to send a zero length payload if last fragment was exactly 0xFFFF long.

        ByteBuf nextChunk = payloadHolder.readSlice(sendingPayloadSize);
        leBuf.writeMedium(sendingPayloadSize);
        leBuf.writeByte(sequenceIter);
        leBuf.writeBytes(nextChunk);

        sequenceIter++;
    } while (payloadHolder.readableBytes() > 0 || lastChunkWasMaximumLength);
    return sequenceIter; //returns the next usable/expected sequence number.
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyStatusVariables.java

License:Open Source License

public void writeStatusVariables(ByteBuf cb) {
    for (BaseQueryEvent qe : getSuppliedEventCodes()) {
        MyQueryEventCode code = qe.getCode();
        switch (code) {
        case Q_FLAGS2_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeInt(((QueryFlags2Event) qe).getFlags());
            break;

        case Q_SQL_MODE_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeLong(((QuerySQLModeEvent) qe).getSqlMode());
            break;

        case Q_CATALOG_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeByte(((QueryCatalogEvent) qe).getCatalog().length());
            cb.writeBytes(((QueryCatalogEvent) qe).getCatalog().getBytes());
            cb.writeByte(0); //for trailing 0
            break;

        case Q_AUTO_INCREMENT:
            cb.writeByte(code.getByteValue());
            cb.writeShort(((QueryAutoIncrementEvent) qe).getAutoIncrementIncrement());
            cb.writeShort(((QueryAutoIncrementEvent) qe).getAutoIncrementOffset());
            break;

        case Q_CHARSET_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeShort(((QueryCharSetCodeEvent) qe).getCharSetClient());
            cb.writeShort(((QueryCharSetCodeEvent) qe).getCollationConnection());
            cb.writeShort(((QueryCharSetCodeEvent) qe).getCollationServer());
            break;

        case Q_TIME_ZONE_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeByte(((QueryTimeZoneCodeEvent) qe).getTimeZone().length());
            cb.writeBytes(((QueryTimeZoneCodeEvent) qe).getTimeZone().getBytes());
            break;

        case Q_CATALOG_NZ_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeByte(((QueryCatalogNZEvent) qe).getCatalog().length());
            cb.writeBytes(((QueryCatalogNZEvent) qe).getCatalog().getBytes());
            break;

        case Q_LC_TIME_NAMES_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeShort(((QueryTimeNamesEvent) qe).getMonthDayNames());
            break;

        case Q_CHARSET_DATABASE_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeShort(((QueryCollationDatabaseEvent) qe).getCollationDatabase());
            break;

        case Q_TABLE_MAP_FOR_UPDATE_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeLong(((QueryTableMapEvent) qe).getTableMapForUpdate());
            break;

        case Q_MASTER_DATA_WRITTEN_CODE:
            cb.writeByte(code.getByteValue());
            cb.writeInt(((QueryMasterDataWrittenEvent) qe).getOriginalLength());
            break;

        case Q_INVOKER:
            cb.writeByte(code.getByteValue());
            cb.writeByte(((QueryInvokerEvent) qe).getUser().length());
            cb.writeBytes(((QueryInvokerEvent) qe).getUser().getBytes());
            cb.writeByte(((QueryInvokerEvent) qe).getHost().length());
            cb.writeBytes(((QueryInvokerEvent) qe).getHost().getBytes());
            break;

        case Q_UPDATED_DB_NAMES:
            cb.writeByte(code.getByteValue());
            List<String> dbs = ((QueryUpdatedDBNamesEvent) qe).getDbNames();
            cb.writeByte(dbs == null ? 0 : dbs.size());
            if (dbs.size() > 0) {
                for (String db : dbs) {
                    cb.writeByte(db.length());
                    cb.writeBytes(db.getBytes(CharsetUtil.UTF_8));
                    cb.writeByte(0); //for trailing 0
                }//from   ww w .  j  a  va2s  . c  o m
            }
            break;

        case Q_MICROSECONDS:
            cb.writeByte(code.getByteValue());
            cb.writeMedium(((QueryMicrosecondsEvent) qe).getMicroseconds());
            break;

        case Q_HRNOW:
            cb.writeMedium(((QueryHRNowEvent) qe).threeBytes);
            break;

        default:
            break;
        }
    }
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

private ByteBuf frame(int length, int type, byte flags, int streamId) {
    ByteBuf buffer = releaseLater(Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length));
    buffer.writeMedium(length);
    buffer.writeByte(type);//from ww w .j a v  a2  s .c  om
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
    return buffer;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

private void writeFrameHeader(ByteBuf buffer, int length, int type, byte flags, int streamId) {
    buffer.writeMedium(length);
    buffer.writeByte(type);//from   w  w  w. j a v  a 2s. c om
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
}

From source file:divconq.ctp.f.BlockCommand.java

License:Open Source License

@Override
public ByteBuf encode() {
    int size = 1 + 1; // code + CTP_F_BLOCK_TYPE_

    int type = 0;

    int hdrcnt = this.headers.size();

    if (hdrcnt > 0) {
        type |= CtpConstants.CTP_F_BLOCK_TYPE_HEADER;
        size += hdrcnt * 4 + 2; // each header is at least 4 bytes (2 attr, 2 len) + ATTR_END is 2 bytes

        for (byte[] val : this.headers.values())
            size += val.length;
    }/*from ww w.j ava2 s.com*/

    if (this.data != null) {
        type |= CtpConstants.CTP_F_BLOCK_TYPE_CONTENT;
        size += 3 + this.data.readableBytes();
    }

    if (this.eof)
        type |= CtpConstants.CTP_F_BLOCK_TYPE_EOF;

    // build buffer
    ByteBuf bb = Hub.instance.getBufferAllocator().buffer(size);

    bb.writeByte(this.cmdCode);
    bb.writeByte(type);

    // add header if any
    if (hdrcnt > 0) {
        for (Entry<Integer, byte[]> val : this.headers.entrySet()) {
            bb.writeShort(val.getKey());
            bb.writeShort(val.getValue().length);
            bb.writeBytes(val.getValue());
        }

        // end of list of headers
        bb.writeShort(CtpConstants.CTP_F_ATTR_END);
    }

    // add content if any
    if (this.data != null) {
        bb.writeLong(this.streamOffset);
        bb.writeMedium(this.data.readableBytes());
        bb.writeBytes(this.data);
    }

    return bb;
}

From source file:net.hasor.rsf.protocol.rsf.v1.RpcRequestProtocolV1.java

License:Apache License

/**encode Message to byte & write to network framework*/
public void encode(RequestBlock reqMsg, ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF
    buf.writeByte(reqMsg.getHead());/*from   w  ww  .ja  v a2s .  c om*/
    //* byte[8]  requestID                            ID
    buf.writeLong(reqMsg.getRequestID());
    //* byte[1]  keepData                             ?
    buf.writeByte(0);
    //* byte[3]  contentLength                        ?(max = 16MB)
    ByteBuf requestBody = this.encodeRequest(reqMsg);
    int bodyLength = requestBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215?
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(requestBody);
}

From source file:net.hasor.rsf.protocol.rsf.v1.RpcResponseProtocolV1.java

License:Apache License

/**encode Message to byte & write to network framework*/
public void encode(ResponseBlock resMsg, ByteBuf buf) throws IOException {
    ///*from  w w  w. java  2 s.co  m*/
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF(0x81)
    buf.writeByte(resMsg.getHead());
    //* byte[8]  requestID                            ID
    buf.writeLong(resMsg.getRequestID());
    //* byte[1]  keepData                             ?
    buf.writeByte(0);
    //* byte[3]  contentLength                        ?(max = 16MB)
    ByteBuf responseBody = this.encodeResponse(resMsg);
    int bodyLength = responseBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215?
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(responseBody);
    //
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcRequestProtocol.java

License:Apache License

/**encode Message to byte & write to network framework*/
public void encode(RequestSocketBlock reqMsg, ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF(0xC1)
    buf.writeByte(reqMsg.getVersion());/* w  ww.  j a va 2s  .  c o m*/
    //* byte[8]  requestID                            ID
    buf.writeLong(reqMsg.getRequestID());
    //* byte[1]  keepData                             ?
    buf.writeByte(0);
    //* byte[3]  contentLength                        ?(max = 16MB)
    ByteBuf requestBody = this.encodeRequest(reqMsg);
    int bodyLength = requestBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215?
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(requestBody);
}

From source file:net.hasor.rsf.remoting.transport.protocol.codec.RpcResponseProtocol.java

License:Apache License

/**encode Message to byte & write to network framework*/
public void encode(ResponseSocketBlock resMsg, ByteBuf buf) throws IOException {
    ////from   w ww .ja  v a 2s .co m
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF(0x81)
    buf.writeByte(resMsg.getVersion());
    //* byte[8]  requestID                            ID
    buf.writeLong(resMsg.getRequestID());
    //* byte[1]  keepData                             ?
    buf.writeByte(0);
    //* byte[3]  contentLength                        ?(max = 16MB)
    ByteBuf responseBody = this.encodeResponse(resMsg);
    int bodyLength = responseBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//8??8??16777215?
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(responseBody);
    //
}

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.InformationObjectAddress.java

License:Open Source License

public int[] toArray() {
    final ByteBuf buf = Unpooled.buffer(4);
    buf.writeMedium(this.address);
    return new int[] { buf.getUnsignedByte(0), buf.getUnsignedByte(1), buf.getUnsignedByte(2) };
}