private Object read(
      DataInput objBuffer,
      Type propertyType,
      boolean array,
      UnrealPackageReadOnly.ExportEntry arrayInner,
      String structName,
      UnrealPackageReadOnly up)
      throws IOException {
    switch (propertyType) {
      case NONE:
        return null;
      case BYTE:
        return objBuffer.readUnsignedByte();
      case INT:
        return objBuffer.readInt();
      case BOOL:
        return array;
      case FLOAT:
        return objBuffer.readFloat();
      case OBJECT:
        return objBuffer.readCompactInt();
      case NAME:
        return objBuffer.readCompactInt();
      case ARRAY:
        int arraySize = objBuffer.readCompactInt();
        List<Object> arrayList = new ArrayList<>(arraySize);

        String a = arrayInner.getObjectClass().getObjectName().getName();
        Property f = unrealClassLoader.getProperty(arrayInner.getObjectFullName());

        array = false;
        arrayInner = null;
        structName = null;
        propertyType = Type.valueOf(a.replace("Property", "").toUpperCase());
        if (propertyType == Type.STRUCT) {
          StructProperty structProperty = (StructProperty) f;
          structName = structProperty.getStructType().getObjectFullName();
        }
        if (propertyType == Type.ARRAY) {
          array = true;
          ArrayProperty arrayProperty = (ArrayProperty) f;
          arrayInner = (UnrealPackageReadOnly.ExportEntry) arrayProperty.getInner();
        }

        for (int i = 0; i < arraySize; i++) {
          arrayList.add(read(objBuffer, propertyType, array, arrayInner, structName, up));
        }
        return arrayList;
      case STRUCT:
        return readStruct(objBuffer, structName, up);
        /*case VECTOR:
            return readStruct(objBuffer, "Vector", up);
        case ROTATOR:
            return readStruct(objBuffer, "Rotator", up);*/
      case STR:
        return objBuffer.readLine();
      default:
        throw new IllegalStateException("Unk type(" + structName + "): " + propertyType);
    }
  }
  /**
   * Reads a primitive value of the specified class type from the stream.
   *
   * @param in A stream to read from.
   * @param cls A class type of the primitive.
   * @return A primitive.
   * @throws IOException If an I/O error occurs.
   */
  static Object readPrimitive(DataInput in, Class cls) throws IOException {
    if (cls == byte.class) return in.readByte();

    if (cls == short.class) return in.readShort();

    if (cls == int.class) return in.readInt();

    if (cls == long.class) return in.readLong();

    if (cls == float.class) return in.readFloat();

    if (cls == double.class) return in.readDouble();

    if (cls == boolean.class) return in.readBoolean();

    if (cls == char.class) return in.readChar();

    throw new IllegalArgumentException();
  }
  public void readFields(DataInput in) throws IOException {
    super.readFields(in);

    score = in.readFloat();
  }
  public List<L2Property> readStructBin(
      DataInput objBuffer, String structName, UnrealPackageReadOnly up) throws UnrealException {
    List<Property> properties = unrealClassLoader.getStructProperties(structName);

    try {
      switch (structName) {
        case "Core.Object.Vector":
          {
            L2Property x = new L2Property(properties.get(0), up);
            x.putAt(0, objBuffer.readFloat());
            L2Property y = new L2Property(properties.get(1), up);
            y.putAt(0, objBuffer.readFloat());
            L2Property z = new L2Property(properties.get(2), up);
            z.putAt(0, objBuffer.readFloat());
            return Arrays.asList(x, y, z);
          }
        case "Core.Object.Rotator":
          {
            L2Property pitch = new L2Property(properties.get(0), up);
            pitch.putAt(0, objBuffer.readInt());
            L2Property yaw = new L2Property(properties.get(1), up);
            yaw.putAt(0, objBuffer.readInt());
            L2Property roll = new L2Property(properties.get(2), up);
            roll.putAt(0, objBuffer.readInt());
            return Arrays.asList(pitch, yaw, roll);
          }
        case "Core.Object.Color":
          {
            L2Property b = new L2Property(properties.get(0), up);
            b.putAt(0, objBuffer.readUnsignedByte());
            L2Property g = new L2Property(properties.get(1), up);
            g.putAt(0, objBuffer.readUnsignedByte());
            L2Property r = new L2Property(properties.get(2), up);
            r.putAt(0, objBuffer.readUnsignedByte());
            L2Property a = new L2Property(properties.get(3), up);
            a.putAt(0, objBuffer.readUnsignedByte());
            return Arrays.asList(b, g, r, a);
          }
        case "Fire.FireTexture.Spark":
          {
            L2Property type = new L2Property(properties.get(0), up);
            type.putAt(0, objBuffer.readUnsignedByte());
            L2Property heat = new L2Property(properties.get(1), up);
            heat.putAt(0, objBuffer.readUnsignedByte());
            L2Property x = new L2Property(properties.get(2), up);
            x.putAt(0, objBuffer.readUnsignedByte());
            L2Property y = new L2Property(properties.get(3), up);
            y.putAt(0, objBuffer.readUnsignedByte());
            L2Property byteA = new L2Property(properties.get(4), up);
            byteA.putAt(0, objBuffer.readUnsignedByte());
            L2Property byteB = new L2Property(properties.get(5), up);
            byteB.putAt(0, objBuffer.readUnsignedByte());
            L2Property byteC = new L2Property(properties.get(6), up);
            byteC.putAt(0, objBuffer.readUnsignedByte());
            L2Property byteD = new L2Property(properties.get(7), up);
            byteD.putAt(0, objBuffer.readUnsignedByte());
            return Arrays.asList(type, heat, x, y, byteA, byteB, byteC, byteD);
          }
        default:
          throw new UnsupportedOperationException("Not implemented"); // TODO
      }
    } catch (IOException e) {
      throw new UnrealException(e);
    }
  }
 void readTagContents(DataInput datainput) throws IOException {
   floatValue = datainput.readFloat();
 }
 public void readObject(DataInput in) throws IOException {
   super.readObject(in);
   ((SpotLight) node).setDirection(control.readVector3f(in));
   ((SpotLight) node).setSpreadAngle(in.readFloat());
   ((SpotLight) node).setConcentration(in.readFloat());
 }
  @Override
  public void readFields(DataInput in) throws IOException {
    /*
     * extract pkt len.
     *
     * GPSQL-1107:
     * The DataInput might already be empty (EOF), but we can't check it beforehand.
     * If that's the case, pktlen is updated to -1, to mark that the object is still empty.
     * (can be checked with isEmpty()).
     */
    pktlen = readPktLen(in);
    if (isEmpty()) {
      return;
    }

    /* extract the version and col cnt */
    int version = in.readShort();
    int curOffset = 4 + 2;
    int colCnt;

    /* !!! Check VERSION !!! */
    if (version != GPDBWritable.VERSION && version != GPDBWritable.PREV_VERSION) {
      throw new IOException(
          "Current GPDBWritable version("
              + GPDBWritable.VERSION
              + ") does not match input version("
              + version
              + ")");
    }

    if (version == GPDBWritable.VERSION) {
      errorFlag = in.readByte();
      curOffset += 1;
    }

    colCnt = in.readShort();
    curOffset += 2;

    /* Extract Column Type */
    colType = new int[colCnt];
    DBType[] coldbtype = new DBType[colCnt];
    for (int i = 0; i < colCnt; i++) {
      int enumType = (in.readByte());
      curOffset += 1;
      if (enumType == DBType.BIGINT.ordinal()) {
        colType[i] = BIGINT.getOID();
        coldbtype[i] = DBType.BIGINT;
      } else if (enumType == DBType.BOOLEAN.ordinal()) {
        colType[i] = BOOLEAN.getOID();
        coldbtype[i] = DBType.BOOLEAN;
      } else if (enumType == DBType.FLOAT8.ordinal()) {
        colType[i] = FLOAT8.getOID();
        coldbtype[i] = DBType.FLOAT8;
      } else if (enumType == DBType.INTEGER.ordinal()) {
        colType[i] = INTEGER.getOID();
        coldbtype[i] = DBType.INTEGER;
      } else if (enumType == DBType.REAL.ordinal()) {
        colType[i] = REAL.getOID();
        coldbtype[i] = DBType.REAL;
      } else if (enumType == DBType.SMALLINT.ordinal()) {
        colType[i] = SMALLINT.getOID();
        coldbtype[i] = DBType.SMALLINT;
      } else if (enumType == DBType.BYTEA.ordinal()) {
        colType[i] = BYTEA.getOID();
        coldbtype[i] = DBType.BYTEA;
      } else if (enumType == DBType.TEXT.ordinal()) {
        colType[i] = TEXT.getOID();
        coldbtype[i] = DBType.TEXT;
      } else {
        throw new IOException("Unknown GPDBWritable.DBType ordinal value");
      }
    }

    /* Extract null bit array */
    byte[] nullbytes = new byte[getNullByteArraySize(colCnt)];
    in.readFully(nullbytes);
    curOffset += nullbytes.length;
    boolean[] colIsNull = byteArrayToBooleanArray(nullbytes, colCnt);

    /* extract column value */
    colValue = new Object[colCnt];
    for (int i = 0; i < colCnt; i++) {
      if (!colIsNull[i]) {
        /* Skip the alignment padding */
        int skipbytes = roundUpAlignment(curOffset, coldbtype[i].getAlignment()) - curOffset;
        for (int j = 0; j < skipbytes; j++) {
          in.readByte();
        }
        curOffset += skipbytes;

        /* For fixed length type, increment the offset according to type type length here.
         * For var length type (BYTEA, TEXT), we'll read 4 byte length header and the
         * actual payload.
         */
        int varcollen = -1;
        if (coldbtype[i].isVarLength()) {
          varcollen = in.readInt();
          curOffset += 4 + varcollen;
        } else {
          curOffset += coldbtype[i].getTypeLength();
        }

        switch (DataType.get(colType[i])) {
          case BIGINT:
            {
              colValue[i] = in.readLong();
              break;
            }
          case BOOLEAN:
            {
              colValue[i] = in.readBoolean();
              break;
            }
          case FLOAT8:
            {
              colValue[i] = in.readDouble();
              break;
            }
          case INTEGER:
            {
              colValue[i] = in.readInt();
              break;
            }
          case REAL:
            {
              colValue[i] = in.readFloat();
              break;
            }
          case SMALLINT:
            {
              colValue[i] = in.readShort();
              break;
            }

            /* For BYTEA column, it has a 4 byte var length header. */
          case BYTEA:
            {
              colValue[i] = new byte[varcollen];
              in.readFully((byte[]) colValue[i]);
              break;
            }
            /* For text formatted column, it has a 4 byte var length header
             * and it's always null terminated string.
             * So, we can remove the last "\0" when constructing the string.
             */
          case TEXT:
            {
              byte[] data = new byte[varcollen];
              in.readFully(data, 0, varcollen);
              colValue[i] = new String(data, 0, varcollen - 1, CHARSET);
              break;
            }

          default:
            throw new IOException("Unknown GPDBWritable ColType");
        }
      }
    }

    /* Skip the ending alignment padding */
    int skipbytes = roundUpAlignment(curOffset, 8) - curOffset;
    for (int j = 0; j < skipbytes; j++) {
      in.readByte();
    }
    curOffset += skipbytes;

    if (errorFlag != 0) {
      throw new IOException("Received error value " + errorFlag + " from format");
    }
  }
Beispiel #8
0
  /** Read a {@link Writable}, {@link String}, primitive type, or an array of the preceding. */
  @SuppressWarnings("unchecked")
  public static Object readObject(DataInput in, ObjectWritable objectWritable, Configuration conf)
      throws IOException {
    String className = UTF8.readString(in);
    Class<?> declaredClass = PRIMITIVE_NAMES.get(className);
    if (declaredClass == null) {
      declaredClass = loadClass(conf, className);
    }

    Object instance;

    if (declaredClass.isPrimitive()) { // primitive types

      if (declaredClass == Boolean.TYPE) { // boolean
        instance = Boolean.valueOf(in.readBoolean());
      } else if (declaredClass == Character.TYPE) { // char
        instance = Character.valueOf(in.readChar());
      } else if (declaredClass == Byte.TYPE) { // byte
        instance = Byte.valueOf(in.readByte());
      } else if (declaredClass == Short.TYPE) { // short
        instance = Short.valueOf(in.readShort());
      } else if (declaredClass == Integer.TYPE) { // int
        instance = Integer.valueOf(in.readInt());
      } else if (declaredClass == Long.TYPE) { // long
        instance = Long.valueOf(in.readLong());
      } else if (declaredClass == Float.TYPE) { // float
        instance = Float.valueOf(in.readFloat());
      } else if (declaredClass == Double.TYPE) { // double
        instance = Double.valueOf(in.readDouble());
      } else if (declaredClass == Void.TYPE) { // void
        instance = null;
      } else {
        throw new IllegalArgumentException("Not a primitive: " + declaredClass);
      }

    } else if (declaredClass.isArray()) { // array
      int length = in.readInt();
      instance = Array.newInstance(declaredClass.getComponentType(), length);
      for (int i = 0; i < length; i++) {
        Array.set(instance, i, readObject(in, conf));
      }

    } else if (declaredClass == String.class) { // String
      instance = UTF8.readString(in);
    } else if (declaredClass.isEnum()) { // enum
      instance = Enum.valueOf((Class<? extends Enum>) declaredClass, UTF8.readString(in));
    } else { // Writable
      Class instanceClass = null;
      String str = UTF8.readString(in);
      instanceClass = loadClass(conf, str);

      Writable writable = WritableFactories.newInstance(instanceClass, conf);
      writable.readFields(in);
      instance = writable;

      if (instanceClass == NullInstance.class) { // null
        declaredClass = ((NullInstance) instance).declaredClass;
        instance = null;
      }
    }

    if (objectWritable != null) { // store values
      objectWritable.declaredClass = declaredClass;
      objectWritable.instance = instance;
    }

    return instance;
  }