Example usage for java.io DataInput readFully

List of usage examples for java.io DataInput readFully

Introduction

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

Prototype

void readFully(byte b[]) throws IOException;

Source Link

Document

Reads some bytes from an input stream and stores them into the buffer array b .

Usage

From source file:org.cloudata.core.client.CellValueMatcherInfo.java

@Override
public void readFields(DataInput in) throws IOException {
    this.version = in.readInt();

    int classCount = in.readInt();

    for (int i = 0; i < classCount; i++) {
        String className = CWritableUtils.readString(in);
        int length = in.readInt();

        if (length > 0) {
            byte[] classByte = new byte[length];
            in.readFully(classByte);

            classNames.add(className);//from   w  ww.ja  v  a  2s  . co  m
            classBytes.add(classByte);
        }
    }
}

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);
    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);
    }/*  w w  w.j a va  2s. c  o m*/
    byteDataIn.close();
    byteDataIn = null;
    buf = null;
    //endTime = System.currentTimeMillis();
    //LOG.fatal("readColumnValue2:time=" + (endTime - startTime));
    return instance;
}

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

/** Read a {@link CWritable}, {@link String}, primitive type, or an array of
 * the preceding. *//* w w  w  .j a  va 2  s  .com*/
@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

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;//w ww. ja va  2 s .  co  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();/*from  ww w  .  j a v  a  2s  . c  o  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.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 w  w  w  .  ja v a 2  s . c  om*/
        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:org.cloudata.core.tablet.ColumnValue.java

public void readFields(DataInput in) throws IOException {
    //long startNano = System.nanoTime();
    rowKey.readFields(in);/*from www  .j a va2s.co  m*/
    cellKey.readFields(in);
    int opCode = in.readInt();
    if (opCode == Constants.DELETEED) {
        deleted = true;
    } else if (opCode == Constants.INSERTED) {
        deleted = false;
    } else {
        throw new IOException("Wrong record operation code(DELETEED or INSERTED): " + opCode);
    }
    timestamp = in.readLong();
    numOfValues = in.readInt();
    int valueLength = in.readInt();
    //System.out.println(">>>>>" + valueLength);
    value = valueLength < 0 ? null : new byte[valueLength];
    if (value != null) {
        in.readFully(value);
    }
}

From source file:org.cloudata.core.tabletserver.CommitLog.java

public void readFields(DataInput in) throws IOException {
    operation = in.readInt();/*from ww  w  . j  a  v a2 s. c om*/
    rowKey.readFields(in);
    columnName = CWritableUtils.readString(in);

    columnKey.readFields(in);
    timestamp = in.readLong();
    int length = in.readInt();

    if (length > MAX_VALUE_SIZE) {
        LOG.error("number of bytes in commitlog exceeds CommitLog.MAX_VALUE_SIZE[" + MAX_VALUE_SIZE + "]");
        throw new IOException("data size is too long.[" + length + "]");
    }

    if (length >= 0) {
        value = new byte[length];
        in.readFully(value);
    }
}

From source file:org.deephacks.confit.internal.hbase.MultiKeyValueComparisonFilter.java

@Override
public void readFields(DataInput input) throws IOException {
    int sidSize = WritableUtils.readVInt(input);
    sid = new byte[sidSize];
    input.readFully(sid);
    maxResults = WritableUtils.readVInt(input);
    int restrictionLength = WritableUtils.readVInt(input);
    for (int i = 0; i < restrictionLength; i++) {
        QualifierRestriction restriction = RestrictionType.values()[WritableUtils.readVInt(input)]
                .newInstance();//from   ww  w .  jav a2 s  .co  m
        restriction.readFields(input);
        restrictions.add(restriction);
    }
}

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);
}