示例#1
0
  @Override
  public int lengthOf(Object value) {
    Byte code = typeCode.get(value.getClass());
    if (code == null) throw new UnsupportedTypeException(value.toString());

    List<Object> datas = getDatas(code, (Function) value);
    int contentLength = 0;
    for (Object data : datas) {
      if (data instanceof String)
        contentLength += EncodedStringCache.getEncodedString((String) data).length();
      else contentLength += EncodingRule.lengthOf(data);
    }

    return 1 + EncodingRule.lengthOfRawNumber(int.class, contentLength) + contentLength;
  }
示例#2
0
  @Override
  public void encode(ByteBuffer bb, Object value) {
    Byte code = typeCode.get(value.getClass());
    if (code == null) throw new UnsupportedTypeException(value.toString());

    List<Object> datas = getDatas(code, (Function) value);
    int contentLength = 0;
    for (Object data : datas) {
      if (data instanceof String)
        contentLength += EncodedStringCache.getEncodedString((String) data).length();
      else contentLength += EncodingRule.lengthOf(data, SortCodec.instance);
    }

    bb.put(code.byteValue());
    EncodingRule.encodeRawNumber(bb, int.class, contentLength);
    for (Object data : datas) {
      if (data instanceof String) {
        EncodedStringCache c = EncodedStringCache.getEncodedString((String) data);
        bb.put(EncodingRule.STRING_TYPE);
        EncodingRule.encodeRawNumber(bb, int.class, c.value().length);
        bb.put(c.value());
      } else EncodingRule.encode(bb, data);
    }
  }