List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java
License:Apache License
/** * Writes a metric to the passed buffer// ww w. j av a2 s.com * @param buff The buffer to write to * @param valueType The value type * @param metricName The metric name * @param timestamp The metric timestamp * @param value The metric value * @param tags The metric tags * @return the number of bytes written */ public static int write(final ByteBuf buff, final ValueType valueType, final String metricName, final long timestamp, final long value, final Map<String, String> tags) { final int defSize = write(buff, valueType, metricName, timestamp, tags); buff.writeByte(1); buff.writeLong(value); return defSize + 9; }
From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java
License:Apache License
/** * Writes a metric to the passed buffer//from w ww . ja v a2s . c o m * @param buff The buffer to write to * @param valueType The value type * @param metricName The metric name * @param timestamp The metric timestamp * @param value The metric value * @param tags The metric tags * @return the number of bytes written */ public static int write(final ByteBuf buff, final ValueType valueType, final String metricName, final long timestamp, final double value, final Map<String, String> tags) { final int defSize = write(buff, valueType, metricName, timestamp, tags); buff.writeByte(0); buff.writeDouble(value); return defSize + 9; }
From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java
License:Apache License
/** * Returns a byte array containing the serialized streammetric * @return a byte array /*ww w . j a va2 s . c om*/ */ @Override public byte[] toByteArray() { final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize); try { buff.writeByte(TYPE_CODE); writeByteArray(buff); if (isDoubleValue) { buff.writeByte(0); buff.writeDouble(doubleValue); } else { buff.writeByte(1); buff.writeLong(longValue); } return ByteBufUtil.getBytes(buff, 0, buff.readableBytes()); } finally { try { buff.release(); } catch (Exception x) { /* No Op */} } }
From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java
License:Apache License
/** * Returns this streamed metric serialized into a byte buf * @return the byte buf/*from w ww .ja v a 2 s .co m*/ */ @Override public ByteBuf toByteBuff() { final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize); buff.writeByte(TYPE_CODE); writeByteArray(buff); if (isDoubleValue) { buff.writeByte(0); buff.writeDouble(doubleValue); } else { buff.writeByte(1); buff.writeLong(longValue); } return buff; }
From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java
License:Apache License
/** * Writes this metric into the passed buffer * @param buff The buffer to write this metric into *///from ww w.j a v a 2 s . c o m @Override public void intoByteBuf(final ByteBuf buff) { buff.writeByte(TYPE_CODE); writeByteArray(buff); if (isDoubleValue) { buff.writeByte(0); buff.writeDouble(doubleValue); } else { buff.writeByte(1); buff.writeLong(longValue); } }
From source file:com.hop.hhxx.example.udt.echo.message.MsgEchoClientHandler.java
License:Apache License
public MsgEchoClientHandler() { super(false); final ByteBuf byteBuf = Unpooled.buffer(io.netty.example.udt.echo.message.MsgEchoClient.SIZE); for (int i = 0; i < byteBuf.capacity(); i++) { byteBuf.writeByte((byte) i); }// w ww.ja v a 2 s .c o m message = new UdtMessage(byteBuf); }
From source file:com.hxr.javatone.concurrency.netty.official.factorial.NumberEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception { // Convert to a BigInteger first for easier implementation. BigInteger v;//ww w .jav a2 s.co m if (msg instanceof BigInteger) { v = (BigInteger) msg; } else { v = new BigInteger(String.valueOf(msg)); } // Convert the number into a byte array. byte[] data = v.toByteArray(); int dataLength = data.length; // Write a message. out.writeByte((byte) 'F'); // magic number out.writeInt(dataLength); // data length out.writeBytes(data); // data }
From source file:com.ibasco.agql.protocols.valve.source.query.SourceRconPacketBuilder.java
License:Open Source License
@Override public byte[] deconstruct(SourceRconPacket packet) { //1) size = int (4 bytes) //2) id = int (4 bytes) //3) type = int (4 bytes) //4) body = string (length + 1 null byte) //5) trailer = null byte int id = packet.getId(); int type = packet.getType(); final String body = StringUtils.defaultString(packet.getBody()); int packetSize = 10 + body.length(); final ByteBuf buf = createBuffer(packetSize); byte[] data;//w w w . j a v a 2s . c om try { buf.writeIntLE(packetSize); buf.writeIntLE(id); buf.writeIntLE(type); buf.writeBytes(body.getBytes()); buf.writeByte(0); buf.writeByte(0); data = new byte[buf.readableBytes()]; buf.readBytes(data); } finally { buf.release(); } return data; }
From source file:com.ibasco.agql.protocols.valve.steam.master.packets.MasterServerRequestPacket.java
License:Open Source License
@Override public byte[] getPayload() { String filterString = this.filter.toString(); int payloadSize = (3 + filterString.length() + (this.startIp.length())); final ByteBuf payload = PooledByteBufAllocator.DEFAULT.buffer(payloadSize); try {/*from w ww .ja v a2 s .co m*/ payload.writeByte(getRegion()); payload.writeBytes(getStartIp().getBytes()); payload.writeByte(0); //terminating byte payload.writeBytes(filterString.getBytes()); byte[] payloadBytes = new byte[payload.readableBytes()]; payload.readBytes(payloadBytes); return payloadBytes; } finally { payload.release(); } }
From source file:com.irh.material.basics.netty.chapter14_1.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) { throw new Exception("The encode message is null"); }//from w w w .j a va 2s . co m sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null; byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else sendBuf.writeInt(0); sendBuf.setInt(4, sendBuf.readableBytes() - 8); }