コード例 #1
0
  public DecResult decode(DecContext ctx) {
    DecResult ret =
        ctx.getCodecOf(short.class)
            .decode(
                ctx.getDecContextFactory()
                    .createDecContext(ctx.getDecBytes(), short.class, ctx.getDecOwner(), null));
    short listLength = (Short) ret.getValue();
    byte[] bytes = ret.getRemainBytes();
    Class<?> compomentClass = getCompomentClass(ctx.getField());

    ArrayList<Object> list = null;
    if (listLength > 0) {
      //            if (logger.isDebugEnabled()) {
      //                logger.debug( "bytes2Array: decode Array length [" +  arrayLength +"]" );
      //            }
      list = new ArrayList<Object>(listLength);
      ByteFieldCodec anyCodec = ctx.getCodecOf(FieldCodecCategory.ANY);

      for (int idx = 0; idx < listLength; idx++) {
        ret =
            anyCodec.decode(
                ctx.getDecContextFactory()
                    .createDecContext(bytes, compomentClass, ctx.getDecOwner(), null));
        list.add(ret.getValue());
        bytes = ret.getRemainBytes();
      }
    }

    return new DecResult(list, bytes);
  }
コード例 #2
0
ファイル: ArrayListCodec.java プロジェクト: zhangxiao918/z1
  public DecResult decode(DecContext ctx) {
    Class<?> compomentClass = getCompomentClass(ctx.getField());
    final ByteFieldDesc desc = ctx.getFieldDesc();
    int listLength = 0;

    if (null == desc) {
      throw new RuntimeException("invalid list length or fixedLength.");
    } else if (desc.hasLength()) {
      //  已经有字段记录数组长度了
      listLength = desc.getLength(ctx.getDecOwner());
    } else {
      throw new RuntimeException("invalid list length or fixedLength.");
    }

    ArrayList<Object> list = null;
    byte[] bytes = ctx.getDecBytes();
    if (listLength > 0 && bytes.length > 0) {
      list = new ArrayList<Object>(listLength);

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

      int idx = 0;
      while (idx < listLength && bytes.length > 0) {
        idx++;

        DecResult ret =
            anyCodec.decode(
                ctx.getDecContextFactory()
                    .createDecContext(bytes, compomentClass, ctx.getDecOwner(), null));
        list.add(ret.getValue());
        bytes = ret.getRemainBytes();
      }
    }

    return new DecResult(list, bytes);
  }