List of usage examples for io.netty.buffer ByteBuf writeByte
public abstract ByteBuf writeByte(int value);
From source file:at.yawk.accordion.codec.unsafe.NamedObjectCodec.java
License:Mozilla Public License
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override// www .j av a 2s . c om public void encode(ByteBuf target, Object message) { if (message == null) { target.writeByte(0); } else { Class<?> clazz = message.getClass(); writeClassName(target, clazz.getName()); getCodec(clazz).encode(target, message); } }
From source file:at.yawk.accordion.codec.unsafe.NamedObjectCodec.java
License:Mozilla Public License
private static void writeClassName(ByteBuf buf, String className) { buf.writeBytes(className.getBytes(StandardCharsets.UTF_8)); buf.writeByte(0); }
From source file:at.yawk.accordion.distributed.InternalProtocol.java
License:Mozilla Public License
/** * Write a byte array. Maximum length 0xff bytes. *//*w ww . ja v a 2 s . c o m*/ static void writeByteArray(ByteBuf to, byte[] array) { to.writeByte(array.length); to.writeBytes(array); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
@Override protected void encode(ChannelHandlerContext ctx, MessageHeader msg, ByteBuf out) throws Exception { out = out.order(Local.OUTBOUND_ORDER); AlignableByteBuf alignedBuf = AlignableByteBuf.encoding(out); out.writeByte(Local.OUTBOUND_ORDER == ByteOrder.LITTLE_ENDIAN ? 'l' : 'B'); out.writeByte(msg.getMessageType().getId()); byte flags = 0; if (msg.isNoReplyExpected()) { flags |= NO_REPLY_EXPECTED;/* w w w . ja v a2 s . c om*/ } if (msg.isNoAutoStart()) { flags |= NO_AUTO_START; } if (msg.isAllowInteractiveAuthorization()) { flags |= ALLOW_INTERACTIVE_AUTHORIZATION; } out.writeByte(flags); byte protocolVersion = msg.getMajorProtocolVersion(); if (protocolVersion == 0) { protocolVersion = PROTOCOL_VERSION; } out.writeByte(protocolVersion); out.writeInt((int) msg.getMessageBodyLength()); int serial = msg.getSerial(); if (serial == 0) { serial = Local.generateSerial(ctx); } out.writeInt(serial); checkRequiredHeaderFieldsPresent(msg); ArrayObject headerObject = ArrayObject.create(HEADER_FIELD_LIST_TYPE, msg.getHeaderFields().entrySet().stream().map(entry -> { BasicObject id = BasicObject.createByte(entry.getKey().getId()); DbusObject value = entry.getValue(); return StructObject.create(HEADER_FIELD_TYPE, Arrays.asList(id, VariantObject.create(value))); }).collect(Collectors.toList())); headerObject.serialize(alignedBuf); alignedBuf.alignWrite(8); }
From source file:blazingcache.network.netty.DodoMessageUtils.java
License:Apache License
public static void encodeMessage(ByteBuf encoded, Message m) { encoded.writeByte(VERSION); encoded.writeInt(m.type);/*w w w.j a va 2 s .co m*/ writeUTF8String(encoded, m.messageId); if (m.replyMessageId != null) { encoded.writeByte(OPCODE_REPLYMESSAGEID); writeUTF8String(encoded, m.replyMessageId); } if (m.clientId != null) { encoded.writeByte(OPCODE_WORKERPROCESSID); writeUTF8String(encoded, m.clientId); } if (m.parameters != null) { encoded.writeByte(OPCODE_PARAMETERS); encoded.writeInt(m.parameters.size()); for (Map.Entry<String, Object> p : m.parameters.entrySet()) { writeEncodedSimpleValue(encoded, p.getKey()); writeEncodedSimpleValue(encoded, p.getValue()); } } }
From source file:blazingcache.network.netty.DodoMessageUtils.java
License:Apache License
private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) { if (o == null) { encoded.writeByte(OPCODE_NULL_VALUE); } else if (o instanceof String) { encoded.writeByte(OPCODE_STRING_VALUE); writeUTF8String(encoded, (String) o); } else if (o instanceof Integer) { encoded.writeByte(OPCODE_INT_VALUE); encoded.writeInt((Integer) o); } else if (o instanceof Long) { encoded.writeByte(OPCODE_LONG_VALUE); encoded.writeLong((Long) o); } else if (o instanceof Set) { Set set = (Set) o; encoded.writeByte(OPCODE_SET_VALUE); encoded.writeInt(set.size());/*from w w w .j a v a 2s . c om*/ for (Object o2 : set) { writeEncodedSimpleValue(encoded, o2); } } else if (o instanceof List) { List set = (List) o; encoded.writeByte(OPCODE_LIST_VALUE); encoded.writeInt(set.size()); for (Object o2 : set) { writeEncodedSimpleValue(encoded, o2); } } else if (o instanceof byte[]) { byte[] set = (byte[]) o; encoded.writeByte(OPCODE_BYTEARRAY_VALUE); encoded.writeInt(set.length); encoded.writeBytes(set); } else if (o instanceof Map) { Map set = (Map) o; encoded.writeByte(OPCODE_MAP_VALUE); encoded.writeInt(set.size()); for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) { writeEncodedSimpleValue(encoded, entry.getKey()); writeEncodedSimpleValue(encoded, entry.getValue()); } } else { throw new RuntimeException("unsupported class " + o.getClass()); } }
From source file:blazingcache.network.netty.MessageUtils.java
License:Apache License
private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) { if (o == null) { encoded.writeByte(OPCODE_NULL_VALUE); } else if (o instanceof String) { encoded.writeByte(OPCODE_STRING_VALUE); writeUTF8String(encoded, (String) o); } else if (o instanceof RawString) { encoded.writeByte(OPCODE_STRING_VALUE); writeUTF8String(encoded, ((RawString) o).toString()); } else if (o instanceof Integer) { encoded.writeByte(OPCODE_INT_VALUE); encoded.writeInt((Integer) o); } else if (o instanceof Long) { encoded.writeByte(OPCODE_LONG_VALUE); encoded.writeLong((Long) o); } else if (o instanceof Set) { Set set = (Set) o; encoded.writeByte(OPCODE_SET_VALUE); encoded.writeInt(set.size());/* w w w. j av a 2 s .co m*/ for (Object o2 : set) { writeEncodedSimpleValue(encoded, o2); } } else if (o instanceof List) { List set = (List) o; encoded.writeByte(OPCODE_LIST_VALUE); encoded.writeInt(set.size()); for (Object o2 : set) { writeEncodedSimpleValue(encoded, o2); } } else if (o instanceof byte[]) { byte[] set = (byte[]) o; encoded.writeByte(OPCODE_BYTEARRAY_VALUE); encoded.writeInt(set.length); encoded.writeBytes(set); } else if (o instanceof Map) { Map set = (Map) o; encoded.writeByte(OPCODE_MAP_VALUE); encoded.writeInt(set.size()); for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) { writeEncodedSimpleValue(encoded, entry.getKey()); writeEncodedSimpleValue(encoded, entry.getValue()); } } else { throw new RuntimeException("unsupported class " + o.getClass()); } }
From source file:blusunrize.immersiveengineering.common.util.network.MessageSpeedloaderSync.java
@Override public void toBytes(ByteBuf buf) { buf.writeByte(slot); buf.writeByte(hand.ordinal()); }
From source file:books.netty.protocol.netty.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"); 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;/*from w ww .jav a2 s . c o m*/ 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); }
From source file:books.netty.protocol.netty.codec.TestCodeC.java
License:Apache License
public ByteBuf encode(NettyMessage msg) throws Exception { ByteBuf sendBuf = Unpooled.buffer(); 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;// www .ja v a 2 s . c o m 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()); return sendBuf; }