Пример #1
0
  public Object read(Buffer buf) {
    HeaderInfo info = parseHeader(buf);
    Object obj = null;

    switch (info.type) {
      case TYPE_INTEGER:
        obj = New.i8((byte) info.value);
        break;
      case TYPE_I8:
        obj = New.i8(buf.readI8());
        break;
      case TYPE_I16:
        obj = New.i16(buf.readI16());
        break;
      case TYPE_I32:
        obj = New.i32(buf.readI32());
        break;
      case TYPE_I64:
        obj = New.i64(buf.readI64());
        break;
      case TYPE_F32:
        obj = New.f32(buf.readF32());
        break;
      case TYPE_F64:
        obj = New.f64(buf.readF64());
        break;
      case TYPE_STRING:
        obj = readString(buf, info.length);
        break;
      case TYPE_TRUE:
        obj = New.bool(true);
        break;
      case TYPE_FALSE:
        obj = New.bool(false);
        break;
      case TYPE_NULL:
        obj = null;
        break;
      case TYPE_LIST:
        obj = readList(buf, info.length);
        break;
      case TYPE_MAP:
        obj = readMap(buf, info.length);
        break;
      default:
        if (W) Log.warn("read variant: unknown type, returning null.");
        obj = null;
    }

    New.release(info);
    return obj;
  }
Пример #2
0
  public void scan(Buffer buf) {
    HeaderInfo info = parseHeader(buf);
    int len = info.length;

    switch (info.type) {
      case TYPE_LIST:
        while (len-- > 0) {
          scan(buf);
        }
        break;
      case TYPE_MAP:
        while (len-- > 0) {
          scan(buf); // scan key
          scan(buf); // scan value
        }
        break;
      default:
        buf.drop(info.length);
        break;
    }

    New.release(info);
  }
Пример #3
0
  // return type, length
  private HeaderInfo parseHeader(Buffer buf) throws RemoteException {
    byte value = buf.readI8();
    int h = value & 0xff;
    int h1 = h & ~MASK_LENGTH_1;
    int h2 = h & ~MASK_LENGTH_2;
    int v1 = h & MASK_LENGTH_1;
    int v2 = h & MASK_LENGTH_2;

    HeaderInfo info = (HeaderInfo) New.get(HeaderInfo.class);

    if (value >= -32) {
      info.type = TYPE_INTEGER;
      info.value = value;
      info.length = 0;
      return info;
    }

    switch (h1) {
      case TYPE_STRING:
      case TYPE_LIST:
        info.type = h1;
        if (v1 < MASK_LENGTH_1) {
          info.length = v1;
        } else {
          info.length = buf.readI32();
        }
        return info;
    }

    switch (h2) {
      case TYPE_MAP:
        info.type = h2;
        if (v2 < MASK_LENGTH_2) {
          info.length = v2;
        } else {
          info.length = buf.readI32();
        }
        return info;
    }

    switch (h) {
      case TYPE_NULL:
      case TYPE_FALSE:
      case TYPE_TRUE:
        info.type = h;
        info.length = 0;
        break;
      case TYPE_I8:
        info.type = h;
        info.length = 1;
        break;
      case TYPE_I16:
        info.type = h;
        info.length = 2;
        break;
      case TYPE_I32:
      case TYPE_F32:
        info.type = h;
        info.length = 4;
        break;
      case TYPE_I64:
      case TYPE_F64:
        info.type = h;
        info.length = 8;
        break;
      default:
        if (W) Log.warn("parsed variant: unknown type, parsed to NULL.");
        info.type = TYPE_NULL;
        info.length = 0;
    }

    return info;
  }