/** * 向ByteBuffer中写入PropertyBag * * @param buf 采用mina的ByteBuffer,因为这个支持自动增长 */ public void writeBuffer(ByteBuffer buf) { short fieldCount = (short) valueMap.size(); buf.putShort(fieldCount); for (Map.Entry<Integer, Value<?>> entry : valueMap.entrySet()) { buf.putInt(entry.getKey()); Value<?> value = entry.getValue(); buf.put((byte) value.type.ordinal()); ProtocolUtils.putValueIntoPkt(buf, value.type, value.value); } }
private static void writeString(String str, ByteBuffer buffer, String charset) { try { if (str == null) { str = ""; } if (buffer != null) { byte[] bytes = str.getBytes(charset); buffer.putShort((short) bytes.length); buffer.put(bytes); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
public static void writeShort(int data, ByteBuffer buffer) { if (buffer != null) { buffer.putShort((short) data); } }