Exemplo n.º 1
0
  /** Reads an arbitrary object from the input stream when the type is unknown. */
  public Object readObject() throws IOException {
    int tag = parseTag();

    switch (tag) {
      case TAG_NULL:
        expectTag(TAG_NULL_END);
        return null;

      case TAG_BOOLEAN:
        {
          int value = parseInt();
          expectTag(TAG_BOOLEAN_END);
          return new Boolean(value != 0);
        }

      case TAG_INT:
        {
          int value = parseInt();
          expectTag(TAG_INT_END);
          return new Integer(value);
        }

      case TAG_LONG:
        {
          long value = parseLong();
          expectTag(TAG_LONG_END);
          return new Long(value);
        }

      case TAG_DOUBLE:
        {
          double value = parseDouble();
          expectTag(TAG_DOUBLE_END);
          return new Double(value);
        }

      case TAG_DATE:
        {
          long value = parseDate();
          expectTag(TAG_DATE_END);
          return new Date(value);
        }

      case TAG_XML:
        {
          return parseXML();
        }

      case TAG_STRING:
        {
          _sbuf.setLength(0);

          String value = parseString(_sbuf).toString();

          expectTag(TAG_STRING_END);

          return value;
        }

      case TAG_BASE64:
        {
          byte[] data = parseBytes();

          expectTag(TAG_BASE64_END);

          return data;
        }

      case TAG_LIST:
        {
          String type = readType();
          int length = readLength();

          return _serializerFactory.readList(this, length, type);
        }

      case TAG_MAP:
        {
          String type = readType();
          Deserializer deserializer;
          deserializer = _serializerFactory.getObjectDeserializer(type);

          return deserializer.readMap(this);
        }

      case TAG_REF:
        {
          int ref = parseInt();

          expectTag(TAG_REF_END);

          return _refs.get(ref);
        }

      case TAG_REMOTE:
        {
          String type = readType();
          String url = readString();

          expectTag(TAG_REMOTE_END);

          return resolveRemote(type, url);
        }

      default:
        throw error("unknown code:" + tagName(tag));
    }
  }
Exemplo n.º 2
0
  /** Reads an object from the input stream with an expected type. */
  public Object readObject(Class cl) throws IOException {
    if (cl == null || cl.equals(Object.class)) {
      return readObject();
    }

    int tag = parseTag();

    switch (tag) {
      case TAG_NULL:
        expectTag(TAG_NULL_END);
        return null;

      case TAG_MAP:
        {
          String type = readType();
          Deserializer reader;

          reader = _serializerFactory.getObjectDeserializer(type, cl);

          return reader.readMap(this);
        }

      case TAG_LIST:
        {
          String type = readType();
          int length = readLength();

          Deserializer reader;
          reader = _serializerFactory.getObjectDeserializer(type, cl);

          return reader.readList(this, length);
        }

      case TAG_REF:
        {
          int ref = parseInt();

          expectTag(TAG_REF_END);

          return _refs.get(ref);
        }

      case TAG_REMOTE:
        {
          String type = readType();
          String url = readString();

          expectTag(TAG_REMOTE_END);

          Object remote = resolveRemote(type, url);

          return remote;
        }
    }

    _peekTag = tag;

    Object value = _serializerFactory.getDeserializer(cl).readObject(this);

    return value;
  }