Search in sources :

Example 6 with UnsafeByteArrayOutputStream

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);
    }
}
Also used : SerializationException(com.alipay.remoting.exception.SerializationException) UnsafeByteArrayOutputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

UnsafeByteArrayOutputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream)6 IOException (java.io.IOException)4 ByteStreamWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf)3 Hessian2Output (com.caucho.hessian.io.Hessian2Output)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SerializationException (com.alipay.remoting.exception.SerializationException)1 CustomHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 Map (java.util.Map)1