Example #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;
  }
Example #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;
  }
Example #3
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);
  }
Example #4
0
  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);
  }