Example usage for java.io DataInput readShort

List of usage examples for java.io DataInput readShort

Introduction

In this page you can find the example usage for java.io DataInput readShort.

Prototype

short readShort() throws IOException;

Source Link

Document

Reads two input bytes and returns a short value.

Usage

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

static INodeFileUnderConstruction readINodeUnderConstruction(DataInput in, FSNamesystem fsNamesys,
        int imgVersion) throws IOException {
    byte[] name = readBytes(in);
    long inodeId = LayoutVersion.supports(Feature.ADD_INODE_ID, imgVersion) ? in.readLong()
            : fsNamesys.allocateNewInodeId();
    short blockReplication = in.readShort();
    long modificationTime = in.readLong();
    long preferredBlockSize = in.readLong();

    int numBlocks = in.readInt();
    BlockInfo[] blocks = new BlockInfo[numBlocks];
    Block blk = new Block();
    int i = 0;//from   w w w  . j  av  a 2  s .c om
    for (; i < numBlocks - 1; i++) {
        blk.readFields(in);
        blocks[i] = new BlockInfo(blk, blockReplication);
    }
    // last block is UNDER_CONSTRUCTION
    if (numBlocks > 0) {
        blk.readFields(in);
        blocks[i] = new BlockInfoUnderConstruction(blk, blockReplication, BlockUCState.UNDER_CONSTRUCTION,
                null);
    }
    PermissionStatus perm = PermissionStatus.read(in);
    String clientName = readString(in);
    String clientMachine = readString(in);

    // We previously stored locations for the last block, now we
    // just record that there are none
    int numLocs = in.readInt();
    assert numLocs == 0 : "Unexpected block locations";

    return new INodeFileUnderConstruction(inodeId, name, blockReplication, modificationTime, preferredBlockSize,
            blocks, perm, clientName, clientMachine, null);
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

public static byte[] readLocalName(DataInput in) throws IOException {
    byte[] createdNodeName = new byte[in.readShort()];
    in.readFully(createdNodeName);/*from   ww w . ja  va  2  s .  c o m*/
    return createdNodeName;
}

From source file:org.apache.hawq.pxf.service.io.GPDBWritable.java

@Override
public void readFields(DataInput in) throws IOException {
    /*//from   w w  w.j a  va2 s .co m
     * 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");
    }
}

From source file:org.apache.pig.data.BinInterSedes.java

/**
 * Expects binInterSedes data types (NOT DataType types!)
 * <p>// www. j ava  2s .c o  m
 *
 * @see org.apache.pig.data.InterSedes#readDatum(java.io.DataInput, byte)
 */
@Override
public Object readDatum(DataInput in, byte type) throws IOException, ExecException {
    switch (type) {
    case TUPLE_0:
    case TUPLE_1:
    case TUPLE_2:
    case TUPLE_3:
    case TUPLE_4:
    case TUPLE_5:
    case TUPLE_6:
    case TUPLE_7:
    case TUPLE_8:
    case TUPLE_9:
    case TUPLE:
    case TINYTUPLE:
    case SMALLTUPLE:
        return SedesHelper.readGenericTuple(in, type);

    case BAG:
    case TINYBAG:
    case SMALLBAG:
        return readBag(in, type);

    case MAP:
    case TINYMAP:
    case SMALLMAP:
        return readMap(in, type);

    case INTERNALMAP:
        return readInternalMap(in);

    case INTEGER_0:
        return Integer.valueOf(0);
    case INTEGER_1:
        return Integer.valueOf(1);
    case INTEGER_INBYTE:
        return Integer.valueOf(in.readByte());
    case INTEGER_INSHORT:
        return Integer.valueOf(in.readShort());
    case INTEGER:
        return Integer.valueOf(in.readInt());

    case LONG_0:
        return Long.valueOf(0);
    case LONG_1:
        return Long.valueOf(1);
    case LONG_INBYTE:
        return Long.valueOf(in.readByte());
    case LONG_INSHORT:
        return Long.valueOf(in.readShort());
    case LONG_ININT:
        return Long.valueOf(in.readInt());
    case LONG:
        return Long.valueOf(in.readLong());

    case DATETIME:
        return new DateTime(in.readLong(), DateTimeZone.forOffsetMillis(in.readShort() * ONE_MINUTE));

    case FLOAT:
        return Float.valueOf(in.readFloat());

    case DOUBLE:
        return Double.valueOf(in.readDouble());

    case BIGINTEGER:
        return readBigInteger(in);

    case BIGDECIMAL:
        return readBigDecimal(in);

    case BOOLEAN_TRUE:
        return Boolean.valueOf(true);

    case BOOLEAN_FALSE:
        return Boolean.valueOf(false);

    case BYTE:
        return Byte.valueOf(in.readByte());

    case TINYBYTEARRAY:
    case SMALLBYTEARRAY:
    case BYTEARRAY:
        return new DataByteArray(SedesHelper.readBytes(in, type));

    case CHARARRAY:
    case SMALLCHARARRAY:
        return SedesHelper.readChararray(in, type);

    case GENERIC_WRITABLECOMPARABLE:
        return readWritable(in);

    case SCHEMA_TUPLE_BYTE_INDEX:
    case SCHEMA_TUPLE_SHORT_INDEX:
    case SCHEMA_TUPLE:
        return readSchemaTuple(in, type);

    case NULL:
        return null;

    default:
        throw new RuntimeException("Unexpected data type " + type + " found in stream.");
    }
}

From source file:org.apache.pig.data.SchemaTuple.java

protected static DateTime read(DataInput in, DateTime v) throws IOException {
    return new DateTime(in.readLong(), DateTimeZone.forOffsetMillis(in.readShort() * ONE_MINUTE));
}

From source file:org.cloudata.core.common.io.CObjectWritable.java

/** Read a {@link CWritable}, {@link String}, primitive type, or an array of
 * the preceding. *///from  w  ww.  j a v  a 2 s  . c o  m
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, CObjectWritable objectWritable, CloudataConf conf,
        boolean arrayComponent, Class componentClass) throws IOException {
    String className;
    if (arrayComponent) {
        className = componentClass.getName();
    } else {
        className = CUTF8.readString(in);
        //SANGCHUL
        //   System.out.println("SANGCHUL] className:" + className);
    }

    Class<?> declaredClass = PRIMITIVE_NAMES.get(className);
    if (declaredClass == null) {
        try {
            declaredClass = conf.getClassByName(className);
        } catch (ClassNotFoundException e) {
            //SANGCHUL
            e.printStackTrace();
            throw new RuntimeException("readObject can't find class[className=" + className + "]", e);
        }
    }

    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
        //System.out.println("SANGCHUL] is array");
        int length = in.readInt();
        //System.out.println("SANGCHUL] array length : " + length);
        //System.out.println("Read:in.readInt():" + length);
        if (declaredClass.getComponentType() == Byte.TYPE) {
            byte[] bytes = new byte[length];
            in.readFully(bytes);
            instance = bytes;
        } else if (declaredClass.getComponentType() == ColumnValue.class) {
            instance = readColumnValue(in, conf, declaredClass, length);
        } else {
            Class componentType = declaredClass.getComponentType();

            // SANGCHUL
            //System.out.println("SANGCHUL] componentType : " + componentType.getName());

            instance = Array.newInstance(componentType, length);
            for (int i = 0; i < length; i++) {
                Object arrayComponentInstance = readObject(in, null, conf, !componentType.isArray(),
                        componentType);
                Array.set(instance, i, arrayComponentInstance);
                //Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass == String.class) { // String
        instance = CUTF8.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, CUTF8.readString(in));
    } else if (declaredClass == ColumnValue.class) {
        //ColumnValue?  ?? ?? ?  ? ?.
        //? ?   ? ? ? ? . 
        Class instanceClass = null;
        try {
            short typeDiff = in.readShort();
            if (typeDiff == TYPE_DIFF) {
                instanceClass = conf.getClassByName(CUTF8.readString(in));
            } else {
                instanceClass = declaredClass;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("readObject can't find class", e);
        }
        ColumnValue columnValue = new ColumnValue();
        columnValue.readFields(in);
        instance = columnValue;
    } else { // Writable

        Class instanceClass = null;
        try {
            short typeDiff = in.readShort();
            // SANGCHUL
            //System.out.println("SANGCHUL] typeDiff : " + typeDiff);
            //System.out.println("Read:in.readShort():" + typeDiff);
            if (typeDiff == TYPE_DIFF) {
                // SANGCHUL
                String classNameTemp = CUTF8.readString(in);
                //System.out.println("SANGCHUL] typeDiff : " + classNameTemp);
                instanceClass = conf.getClassByName(classNameTemp);
                //System.out.println("Read:UTF8.readString(in):" + instanceClass.getClass());
            } else {
                instanceClass = declaredClass;
            }
        } catch (ClassNotFoundException e) {

            // SANGCHUL
            e.printStackTrace();
            throw new RuntimeException("readObject can't find class", e);
        }

        CWritable writable = CWritableFactories.newInstance(instanceClass, conf);
        writable.readFields(in);
        //System.out.println("Read: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;
}

From source file:org.gradoop.model.impl.properties.PropertyValueList.java

@Override
public void readFields(DataInput dataInput) throws IOException {
    bytes = new byte[dataInput.readShort()];
    dataInput.readFully(bytes);/* ww w . j av  a  2 s.  c  o  m*/
}

From source file:StorageEngineClient.MultiFormatStorageSplit.java

@Override
public void readFields(DataInput in) throws IOException {
    len = in.readInt();//from w ww.j a  v  a  2s  .  c  o m
    if (path == null) {
        path = new Path[(int) len];
    }

    for (int i = 0; i < len; i++) {
        short strLen = in.readShort();
        if (strLen > 0) {
            byte[] buf = new byte[strLen];
            in.readFully(buf, 0, strLen);

            String string = new String(buf);
            path[i] = new Path(string);
        }
    }
}

From source file:TVA.Hadoop.MapReduce.Historian.File.StandardPointFile.java

/**
 * Deserializes the point from the underlying data. 
 * //from ww w.  j  a  v  a  2  s .c  om
 * @param   in      A DataInput object to read the point from.
 * @see java.io.DataInput
 * @see org.apache.hadoop.io.Writable#readFields(java.io.DataInput)
 * 
 */
public void readFields(DataInput in) throws IOException {

    this.iTimeTag = in.readInt();
    this.Flags = in.readShort();
    this.Value = in.readFloat();
    this.iPointID = in.readInt();

}