Example usage for java.io DataInput readByte

List of usage examples for java.io DataInput readByte

Introduction

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

Prototype

byte readByte() throws IOException;

Source Link

Document

Reads and returns one input byte.

Usage

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

public Tuple readTuple(DataInput in) throws IOException {
    return readTuple(in, in.readByte());
}

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

public static Object readDatum(DataInput in) throws IOException, ExecException {
    // Read the data type
    byte b = in.readByte();
    return readDatum(in, b);
}

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

public static Object readDatum(DataInput in, byte type) throws IOException, ExecException {
    switch (type) {
    case DataType.TUPLE:
        return bytesToTuple(in);

    case DataType.BAG:
        return bytesToBag(in);

    case DataType.MAP:
        return bytesToMap(in);

    case DataType.INTERNALMAP:
        return bytesToInternalMap(in);

    case DataType.INTEGER:
        return Integer.valueOf(in.readInt());

    case DataType.LONG:
        return Long.valueOf(in.readLong());

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

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

    case DataType.BOOLEAN:
        return Boolean.valueOf(in.readBoolean());

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

    case DataType.BYTEARRAY: {
        int size = in.readInt();
        byte[] ba = new byte[size];
        in.readFully(ba);/*from   w w w . j  a v a  2  s  .c  om*/
        return new DataByteArray(ba);
    }

    case DataType.BIGCHARARRAY:
        return bytesToBigCharArray(in);

    case DataType.CHARARRAY:
        return bytesToCharArray(in);

    case DataType.GENERIC_WRITABLECOMPARABLE:
        return bytesToWritable(in);

    case DataType.NULL:
        return null;

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

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

@Override
public void readFields(DataInput in) throws IOException {
    // Clear our fields, in case we're being reused.
    mFields.clear();/* w w w. j a v a 2s  . co m*/

    // Make sure it's a tuple.
    byte b = in.readByte();
    if (b != DataType.TUPLE) {
        int errCode = 2112;
        String msg = "Unexpected data while reading tuple " + "from binary file.";
        throw new ExecException(msg, errCode, PigException.BUG);
    }
    // Read the number of fields
    int sz = in.readInt();
    for (int i = 0; i < sz; i++) {
        try {
            append(DataReaderWriter.readDatum(in));
        } catch (ExecException ee) {
            throw ee;
        }
    }
}

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

protected static String read(DataInput in, String v) throws IOException {
    return SedesHelper.readChararray(in, in.readByte());
}

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

protected static byte[] read(DataInput in, byte[] v) throws IOException {
    return SedesHelper.readBytes(in, in.readByte());
}

From source file:org.apache.sysml.runtime.compress.CompressedMatrixBlock.java

@Override
public void readFields(DataInput in) throws IOException {
    boolean compressed = in.readBoolean();

    //deserialize uncompressed block
    if (!compressed) {
        super.readFields(in);
        return;/*w  w  w . j  a  v a 2s.  c o m*/
    }

    //deserialize compressed block
    rlen = in.readInt();
    clen = in.readInt();
    nonZeros = in.readLong();
    int ncolGroups = in.readInt();

    _colGroups = new ArrayList<ColGroup>(ncolGroups);
    for (int i = 0; i < ncolGroups; i++) {
        CompressionType ctype = CompressionType.values()[in.readByte()];
        ColGroup grp = null;

        //create instance of column group
        switch (ctype) {
        case UNCOMPRESSED:
            grp = new ColGroupUncompressed();
            break;
        case OLE_BITMAP:
            grp = new ColGroupOLE();
            break;
        case RLE_BITMAP:
            grp = new ColGroupRLE();
            break;
        }

        //deserialize and add column group
        grp.readFields(in);
        _colGroups.add(grp);
    }
}

From source file:org.apache.sysml.runtime.matrix.data.FrameBlock.java

@Override
public void readFields(DataInput in) throws IOException {
    //read head (rows, cols)
    _numRows = in.readInt();//  w  w w  .  java2s . co  m
    int numCols = in.readInt();
    boolean isDefaultMeta = in.readBoolean();
    //allocate schema/meta data arrays
    _schema = (_schema != null && _schema.length == numCols) ? _schema : new ValueType[numCols];
    _colnames = (_colnames != null && _colnames.length == numCols) ? _colnames : new String[numCols];
    _colmeta = (_colmeta != null && _colmeta.length == numCols) ? _colmeta : new ColumnMetadata[numCols];
    _coldata = (_coldata != null && _coldata.length == numCols) ? _coldata : new Array[numCols];
    //read columns (value type, meta, data)
    for (int j = 0; j < numCols; j++) {
        ValueType vt = ValueType.values()[in.readByte()];
        String name = isDefaultMeta ? createColName(j) : in.readUTF();
        long ndistinct = isDefaultMeta ? 0 : in.readLong();
        String mvvalue = isDefaultMeta ? null : in.readUTF();
        Array arr = null;
        switch (vt) {
        case STRING:
            arr = new StringArray(new String[_numRows]);
            break;
        case BOOLEAN:
            arr = new BooleanArray(new boolean[_numRows]);
            break;
        case INT:
            arr = new LongArray(new long[_numRows]);
            break;
        case DOUBLE:
            arr = new DoubleArray(new double[_numRows]);
            break;
        default:
            throw new IOException("Unsupported value type: " + vt);
        }
        arr.readFields(in);
        _schema[j] = vt;
        _colnames[j] = name;
        _colmeta[j] = new ColumnMetadata(ndistinct, (mvvalue == null || mvvalue.isEmpty()) ? null : mvvalue);
        _coldata[j] = arr;
    }
}

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 va 2  s  . co 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.cloudata.core.common.io.CWritableUtils.java

/**
 * Reads a zero-compressed encoded long from input stream and returns it.
 * @param stream Binary input stream/*from   w  w  w  .  j a v  a  2s  . c  o m*/
 * @throws java.io.IOException 
 * @return deserialized long from stream.
 */
public static long readVLong(DataInput stream) throws IOException {
    int len = stream.readByte();
    if (len >= -112) {
        return len;
    }
    boolean isNegative = (len < -120);
    len = isNegative ? -(len + 120) : -(len + 112);
    long i = 0;
    for (int idx = 0; idx < len; idx++) {
        byte b = stream.readByte();
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (isNegative ? (i ^ -1L) : i);
}