protected Object readObjectImpl(Class cl)
    throws IOException
  {
    try {
      Object obj = cl.newInstance();

      if (_refs == null)
        _refs = new ArrayList();
      _refs.add(obj);

      HashMap fieldMap = getFieldMap(cl);

      int code = read();
      for (; code >= 0 && code != 'z'; code = read()) {
        _peek = code;
        
        Object key = readObject();
        
        Field field = (Field) fieldMap.get(key);

        if (field != null) {
          Object value = readObject(field.getType());
          field.set(obj, value);
        }
        else {
          Object value = readObject();
        }
      }
      
      if (code != 'z')
        throw expect("map", code);

      // if there's a readResolve method, call it
      try {
        Method method = cl.getMethod("readResolve", new Class[0]);
        return method.invoke(obj, new Object[0]);
      } catch (Exception e) {
      }

      return obj;
    } catch (IOException e) {
      throw e;
    } catch (Exception e) {
      throw new IOExceptionWrapper(e);
    }
  }