예제 #1
0
  @SuppressWarnings("unchecked")
  public byte[] encode(EncContext ctx) {
    ArrayList<Object> list = (ArrayList<Object>) ctx.getEncObject();
    int listLength = (null != list ? list.size() : 0);
    Class<?> compomentClass = getCompomentClass(ctx.getField());

    byte[] bytes =
        ctx.getCodecOf(short.class)
            .encode(
                ctx.getEncContextFactory().createEncContext((short) listLength, short.class, null));

    if (listLength > 0) {

      ByteFieldCodec anyCodec = ctx.getCodecOf(FieldCodecCategory.ANY);

      for (int idx = 0; idx < listLength; idx++) {
        bytes =
            ArrayUtils.addAll(
                bytes,
                anyCodec.encode(
                    ctx.getEncContextFactory()
                        .createEncContext(list.get(idx), compomentClass, null)));
      }
    }
    return bytes;
  }
예제 #2
0
  @SuppressWarnings("unchecked")
  public byte[] encode(EncContext ctx) {
    ArrayList<Object> list = (ArrayList<Object>) ctx.getEncObject();
    int listLength = (null != list ? list.size() : 0);
    Class<?> compomentClass = getCompomentClass(ctx.getField());

    ByteFieldDesc desc = ctx.getFieldDesc();
    byte[] bytes = null;

    if (null == desc) {
      throw new RuntimeException("invalid array env.");
    } else if (!desc.hasLength()) {
      throw new RuntimeException("invalid array env.");
    } else {
      //  已经存在字段记录数组长度,不用自动写
    }
    if (listLength > 0) {

      ByteFieldCodec anyCodec = ctx.getCodecOf(FieldCodecCategory.ANY);

      for (int idx = 0; idx < listLength; idx++) {
        bytes =
            ArrayUtils.addAll(
                bytes,
                anyCodec.encode(
                    ctx.getEncContextFactory()
                        .createEncContext(list.get(idx), compomentClass, null)));
      }
    }
    return bytes;
  }