use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream in project sofa-rpc by sofastack.
the class SimpleMapSerializer method encode.
/**
* 简单 map 的序列化过程, 用来序列化 bolt 的 header
*
* @param map bolt header
* @return 序列化后的 byte 数组
* @throws SerializationException SerializationException
*/
public byte[] encode(Map<String, String> map) throws SerializationException {
if (map == null || map.isEmpty()) {
return null;
}
UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(64);
try {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
/**
* 排除不写null作为key
*/
if (key != null && value != null) {
writeString(out, key);
writeString(out, value);
}
}
return out.toByteArray();
} catch (IOException ex) {
throw new SerializationException(ex.getMessage(), ex);
}
}
Aggregations