code: @Override
    public byte[] encode(final NSMessage<T> obj) {
      if (isStreamingCodec) {
        final StreamingCodec<T> streamingCodec = (StreamingCodec<T>) codec;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
          try (DataOutputStream daos = new DataOutputStream(baos)) {
            daos.writeUTF(obj.getSrcId().toString());
            daos.writeUTF(obj.getDestId().toString());
            daos.writeInt(obj.getData().size());
            for (final T rec : obj.getData()) {
              streamingCodec.encodeToStream(rec, daos);
            }
          }
          return baos.toByteArray();
        } catch (final IOException e) {
          throw new RuntimeException("IOException", e);
        }
      } else {
        final NSMessagePBuf.Builder pbuf = NSMessagePBuf.newBuilder();
        pbuf.setSrcid(obj.getSrcId().toString());
        pbuf.setDestid(obj.getDestId().toString());
        for (final T rec : obj.getData()) {
          final NSRecordPBuf.Builder rbuf = NSRecordPBuf.newBuilder();
          rbuf.setData(ByteString.copyFrom(codec.encode(rec)));
          pbuf.addMsgs(rbuf);
        }
        return pbuf.build().toByteArray();
      }
    }
docs: Encodes a network service message to bytes.
  
  @param obj a message
  @return bytes