List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:com.dempe.chat.common.mqtt.codec.SubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) { if (message.subscriptions().isEmpty()) { throw new IllegalArgumentException("Found a subscribe message with empty topics"); }/*w w w.j a v a 2 s. c o m*/ if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (SubscribeMessage.Couple c : message.subscriptions()) { variableHeaderBuff.writeBytes(Utils.encodeString(c.topicFilter)); variableHeaderBuff.writeByte(c.qos); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.UnsubAckEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, UnsubAckMessage msg, ByteBuf out) { out.writeByte(AbstractMessage.UNSUBACK << 4).writeBytes(Utils.encodeRemainingLength(2)) .writeShort(msg.getMessageID()); }
From source file:com.dempe.chat.common.mqtt.codec.UnsubscribeEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, UnsubscribeMessage message, ByteBuf out) { if (message.topicFilters().isEmpty()) { throw new IllegalArgumentException("Found an unsubscribe message with empty topics"); }/*from w ww. jav a2 s . co m*/ if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); ByteBuf buff = null; try { variableHeaderBuff.writeShort(message.getMessageID()); for (String topic : message.topicFilters()) { variableHeaderBuff.writeBytes(Utils.encodeString(topic)); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.UNSUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); buff.release(); } }
From source file:com.dempe.chat.common.mqtt.codec.Utils.java
License:Open Source License
/** * Encode the value in the format defined in specification as variable length * array./*from w ww .j a v a 2 s . c om*/ * * @throws IllegalArgumentException if the value is not in the specification bounds * [0..268435455]. */ static ByteBuf encodeRemainingLength(int value) throws CorruptedFrameException { if (value > MAX_LENGTH_LIMIT || value < 0) { throw new CorruptedFrameException("Value should in range 0.." + MAX_LENGTH_LIMIT + " found " + value); } ByteBuf encoded = Unpooled.buffer(4); byte digit; do { digit = (byte) (value % 128); value = value / 128; // if there are more digits to encode, set the top bit of this digit if (value > 0) { digit = (byte) (digit | 0x80); } encoded.writeByte(digit); } while (value > 0); return encoded; }
From source file:com.dempe.chat.common.netty.PublishEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, PublishMessage message, ByteBuf out) { if (message.getQos() == AbstractMessage.QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); }/* ww w.j a va2 s . c o m*/ if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } ByteBuf variableHeaderBuff = ctx.alloc().buffer(2); ByteBuf buff = null; try { variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName())); if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } variableHeaderBuff.writeShort(message.getMessageID()); } variableHeaderBuff.writeBytes(message.getPayload()); int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); buff = ctx.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.PUBLISH << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); if (buff != null) { buff.release(); } } }
From source file:com.digitalpetri.modbus.codec.MbapHeader.java
License:Apache License
public static ByteBuf encode(MbapHeader header, ByteBuf buffer) { buffer.writeShort(header.transactionId); buffer.writeShort(header.protocolId); buffer.writeShort(header.length);//from w w w . ja va2 s.c om buffer.writeByte(header.unitId); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadCoils(ReadCoilsRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadDiscreteInputs(ReadDiscreteInputsRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadHoldingRegisters(ReadHoldingRegistersRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }
From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java
License:Apache License
public ByteBuf encodeReadInputRegisters(ReadInputRegistersRequest request, ByteBuf buffer) { buffer.writeByte(request.getFunctionCode().getCode()); buffer.writeShort(request.getAddress()); buffer.writeShort(request.getQuantity()); return buffer; }