Example #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);
  }
Example #2
0
  public DecResult decode(DecContext ctx) {
    byte[] bytes = ctx.getDecBytes();
    int byteLength = ctx.getByteSize();
    NumberCodec numberCodec = ctx.getNumberCodec();

    if (byteLength > bytes.length) {
      String errmsg =
          "ShortCodec: not enough bytes for decode, need ["
              + byteLength
              + "], actually ["
              + bytes.length
              + "].";
      if (null != ctx.getField()) {
        errmsg += "/ cause field is [" + ctx.getField() + "]";
      }
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
      // return makeDecResult( retShort, bytes );
    }
    return new DecResult(
        numberCodec.bytes2Short(bytes, byteLength),
        ArrayUtils.subarray(bytes, byteLength, bytes.length));
  }
Example #3
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);
  }