Exemple #1
0
  protected static Header readHeader(DataInput in) throws Exception {
    short magic_number = in.readShort();
    Class clazz = ClassConfigurator.get(magic_number);
    if (clazz == null)
      throw new IllegalArgumentException(
          "magic number " + magic_number + " is not available in magic map");

    Header hdr = (Header) clazz.newInstance();
    hdr.readFrom(in);
    return hdr;
  }
Exemple #2
0
  public void readFrom(DataInput in) throws Exception {

    // 1. read the leading byte first
    byte leading = in.readByte();

    // 2. the flags
    flags = in.readShort();

    // 3. dest_addr
    if (Util.isFlagSet(leading, DEST_SET)) dest_addr = Util.readAddress(in);

    // 4. src_addr
    if (Util.isFlagSet(leading, SRC_SET)) src_addr = Util.readAddress(in);

    // 5. buf
    if (Util.isFlagSet(leading, BUF_SET)) {
      int len = in.readInt();
      buf = new byte[len];
      in.readFully(buf, 0, len);
      length = len;
    }

    // 6. headers
    int len = in.readShort();
    headers = createHeaders(len);

    short[] ids = headers.getRawIDs();
    Header[] hdrs = headers.getRawHeaders();

    for (int i = 0; i < len; i++) {
      short id = in.readShort();
      Header hdr = readHeader(in);
      ids[i] = id;
      hdrs[i] = hdr;
    }
  }
Exemple #3
0
    public void readFrom(DataInput in) throws Exception {
      type = Type.values()[in.readByte()];
      // We can't use Util.readObject since it's size is limited to 2^15-1
      try {
        short first = in.readShort();
        if (first == -1) {
          object = Util.readGenericStreamable(in);
        } else {
          ByteBuffer bb = ByteBuffer.allocate(4);
          bb.putShort(first);
          bb.putShort(in.readShort());

          int size = bb.getInt(0);
          byte[] bytes = new byte[size];
          in.readFully(bytes, 0, size);
          object = Util.objectFromByteBuffer(bytes);
        }
      } catch (IOException e) {
        throw e;
      } catch (Exception e) {
        throw new IOException("Exception encountered while serializing execution request", e);
      }
      request = in.readLong();
    }
  public void readObject(DataInput in) throws IOException {

    boolean sgIO = in.readBoolean();
    int nodeID = in.readInt();

    int nodeClassID = in.readShort();

    nodeClassName = null;

    if (nodeClassID == -1) nodeClassName = in.readUTF();

    readConstructorParams(in);

    if (nodeClassID != -1) {
      node = createNode();
      nodeClassName = node.getClass().getName();
    } else node = createNode(nodeClassName);

    if (sgIO) {
      if (control.getCurrentFileVersion() == 1)
        ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO) node).readSceneGraphObject(in);
      else {
        int size = in.readInt();
        if (node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO) {
          byte[] bytes = new byte[size];
          in.readFully(bytes);
          ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
          DataInputStream tmpIn = new DataInputStream(byteStream);
          ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO) node).readSceneGraphObject(tmpIn);
          tmpIn.close();
        } else {
          in.skipBytes(size);
        }
      }
    }

    symbol = control.getSymbolTable().createSymbol(this, node, nodeID);
    readUserData(in);
    if (control.getCurrentFileVersion() > 2) {
      node.setName(readString(in));
    }

    readCapabilities(in);
  }
  /**
   * 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();
  }
Exemple #6
0
  public static ProteinSequence readProteinSequence(DataInput in) throws IOException {

    int size = in.readInt();
    char[] chars = new char[size];

    int completed = 0;
    while (completed < size) {
      int value = in.readShort();
      chars[completed++] = getUnserializedAminoAcid((short) (value >> 10));
      if (completed < size) {
        chars[completed++] =
            getUnserializedAminoAcid((short) ((value >> 5) - ((value >> 10) << 5)));
      }
      if (completed < size) {
        chars[completed++] = getUnserializedAminoAcid((short) ((value) - ((value >> 5) << 5)));
      }
    }

    return new ProteinSequence(new String(chars));
  }
  @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");
    }
  }
Exemple #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;
  }
Exemple #9
0
 public void readFrom(DataInput in) throws Exception {
   type = in.readByte();
   short len = in.readShort();
   version = new byte[len];
   in.readFully(version);
 }