Example usage for java.io DataInput readInt

List of usage examples for java.io DataInput readInt

Introduction

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

Prototype

int readInt() throws IOException;

Source Link

Document

Reads four input bytes and returns an int value.

Usage

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

public static void skipCompressedByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length != -1) {
        skipFully(in, length);//from   w  w  w .  j a  v a2  s. c o m
    }
}

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

public static String[] readCompressedStringArray(DataInput in) throws IOException {
    int len = in.readInt();
    if (len == -1)
        return null;
    String[] s = new String[len];
    for (int i = 0; i < len; i++) {
        s[i] = readCompressedString(in);
    }/*w w w .j a  va  2 s. co  m*/
    return s;
}

From source file:hivemall.fm.FFMPredictionModel.java

@Nonnull
static void readStates(@Nonnull final DataInput in, @Nonnull final byte[] status) throws IOException {
    // read non-empty states differentially
    final int cardinarity = in.readInt();
    Arrays.fill(status, IntOpenHashTable.FULL);
    int prev = 0;
    for (int j = 0; j < cardinarity; j++) {
        int i = VariableByteCodec.decodeUnsignedInt(in) + prev;
        status[i] = IntOpenHashTable.FREE;
        prev = i;// w  w w . ja  v a  2s  .  com
    }
}

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

public static byte[] readCompressedByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length == -1)
        return null;
    byte[] buffer = new byte[length];
    in.readFully(buffer); // could/should use readFully(buffer,0,length)?
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length));
    byte[] outbuf = new byte[length];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;//from  www. j av  a  2s.c  o m
    while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) {
        bos.write(outbuf, 0, len);
    }
    byte[] decompressed = bos.toByteArray();
    bos.close();
    gzi.close();
    return decompressed;
}

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

public static String readString(DataInput in) throws IOException {
    int length = in.readInt();
    if (length > 1024 * 1024 * 5) {
        LOG.error("too long String length: " + length);
        Thread.dumpStack();/*  w  w w.  ja v a  2s  .co m*/
        throw new IOException("WritableUtils.readString: too long String length: " + length);
    }
    if (length == -1)
        return null;
    byte[] buffer = new byte[length];
    //long time1 = System.currentTimeMillis();
    in.readFully(buffer); // could/should use readFully(buffer,0,length)?
    //long time2 = System.currentTimeMillis();
    String result = new String(buffer, "UTF-8");
    //long time3 = System.currentTimeMillis();
    //System.out.println("readString:" + (time2-time1) + "," + (time3-time1));
    return result;
}

From source file:org.apache.cassandra.utils.ByteBufferUtil.java

public static ByteBuffer readWithLength(DataInput in) throws IOException {
    int length = in.readInt();
    if (length < 0) {
        throw new IOException("Corrupt (negative) value length encountered");
    }//  w w  w  .  j  a  v a2  s.c o  m

    return ByteBufferUtil.read(in, length);
}

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

public static String readString(DataInput in, int maxLength) throws IOException {
    int length = in.readInt();
    if (length > maxLength) {
        Thread.dumpStack();/*from   ww w .ja v a 2 s.co m*/
        throw new IOException("WritableUtils.readString:too long String length: " + length);
    }
    if (length == -1)
        return null;
    byte[] buffer = new byte[length];
    in.readFully(buffer); // could/should use readFully(buffer,0,length)?
    return new String(buffer, "UTF-8");
}

From source file:com.aliyun.odps.io.TupleReaderWriter.java

@SuppressWarnings("unchecked")
private static Writable readDatum(DataInput in, byte type) throws IOException {
    switch (type) {
    case TUPLE://from  www  . j av  a 2 s  .  co m
        int sz = in.readInt();
        // if sz == 0, we construct an "empty" tuple -
        // presumably the writer wrote an empty tuple!
        if (sz < 0) {
            throw new IOException("Invalid size " + sz + " for a tuple");
        }
        Tuple tp = new Tuple(sz);
        for (int i = 0; i < sz; i++) {
            byte b = in.readByte();
            tp.set(i, readDatum(in, b));
        }

        return tp;

    case NULL:
        return null;

    case INTWRITABLE:
        IntWritable iw = new IntWritable();
        iw.readFields(in);
        return iw;

    case LONGWRITABLE:
        LongWritable lw = new LongWritable();
        lw.readFields(in);
        return lw;

    case DATETIMEWRITABLE:
        DatetimeWritable dtw = new DatetimeWritable();
        dtw.readFields(in);
        return dtw;

    case DOUBLEWRITABLE:
        DoubleWritable dw = new DoubleWritable();
        dw.readFields(in);
        return dw;

    case BOOLEANWRITABLE:
        BooleanWritable bw = new BooleanWritable();
        bw.readFields(in);
        return bw;

    case BYTESWRITABLE:
        BytesWritable bsw = new BytesWritable();
        bsw.readFields(in);
        return bsw;

    case TEXT:
        Text t = new Text();
        t.readFields(in);
        return t;

    case NULLWRITABLE:
        NullWritable nw = NullWritable.get();
        nw.readFields(in);
        return nw;

    case UNKNOWN:
        String clsName = in.readUTF();
        try {
            Class<? extends Writable> cls = (Class<? extends Writable>) Class.forName(clsName);
            Writable w = (Writable) ReflectionUtils.newInstance(cls, null);
            w.readFields(in);
            return w;
        } catch (RuntimeException re) {
            LOG.info(re.getMessage());
            throw new IOException(re);
        } catch (ClassNotFoundException cnfe) {
            throw new IOException(cnfe);
        }

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

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

private static Object readColumnValue(DataInput in, CloudataConf conf, Class<?> declaredClass, int length)
        throws IOException {
    long startTime = System.currentTimeMillis();

    int totalByteSize = in.readInt();
    byte[] buf = new byte[totalByteSize];
    in.readFully(buf);//from   ww w . j a  v  a2 s. c om
    long endTime = System.currentTimeMillis();
    //LOG.fatal("readColumnValue1:length=" + length + ",bytes=" + totalByteSize + ",time=" + (endTime - startTime));

    DataInputStream byteDataIn = new DataInputStream(new ByteArrayInputStream(buf));

    ColumnValue[] instance = (ColumnValue[]) Array.newInstance(declaredClass.getComponentType(), length);
    //startTime = System.currentTimeMillis();
    Class componentClass = declaredClass.getComponentType();
    for (int i = 0; i < length; i++) {
        //Array.set(instance, i, readObject(byteDataIn, null, conf, true, declaredClass.getComponentType()));
        instance[i] = (ColumnValue) readObject(byteDataIn, null, conf, true, componentClass);
    }
    byteDataIn.close();
    byteDataIn = null;
    buf = null;
    //endTime = System.currentTimeMillis();
    //LOG.fatal("readColumnValue2:time=" + (endTime - startTime));
    return instance;
}

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

public static Tuple bytesToTuple(DataInput in) throws IOException {
    // Don't use Tuple.readFields, because it requires you to
    // create a tuple with no size and then append fields.
    // That's less efficient than allocating the tuple size up
    // front and then filling in the spaces.
    // Read the size.
    int sz = in.readInt();
    // if sz == 0, we construct an "empty" tuple -
    // presumably the writer wrote an empty tuple!
    if (sz < 0) {
        throw new IOException("Invalid size " + sz + " for a tuple");
    }//w  ww  .j  a  v  a  2  s. c  om
    Tuple t = mTupleFactory.newTuple(sz);
    for (int i = 0; i < sz; i++) {
        t.set(i, readDatum(in));
    }
    return t;

}