Example usage for org.apache.hadoop.io Writable readFields

List of usage examples for org.apache.hadoop.io Writable readFields

Introduction

In this page you can find the example usage for org.apache.hadoop.io Writable readFields.

Prototype

void readFields(DataInput in) throws IOException;

Source Link

Document

Deserialize the fields of this object from in.

Usage

From source file:org.goldenorb.zookeeper.ZookeeperUtils.java

License:Apache License

/**
 * // w w  w .jav a2  s .  c  o  m
 * @param byteArray
 *          - byte[]
 * @param w
 *          - Writable
 * @returns Writable
 */
public static Writable byteArrayToWritable(byte[] byteArray, Writable w) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
    DataInput in = new DataInputStream(bais);
    w.readFields(in);
    return w;
}

From source file:org.gridgain.grid.kernal.processors.hadoop.v2.GridHadoopWritableSerialization.java

License:Open Source License

/** {@inheritDoc} */
@Override//from   w  w w.  ja v a 2  s.  c o  m
public Object read(DataInput in, @Nullable Object obj) throws GridException {
    Writable w = obj == null ? U.newInstance(cls) : cls.cast(obj);

    try {
        w.readFields(in);
    } catch (IOException e) {
        throw new GridException(e);
    }

    return w;
}

From source file:org.megalon.MArrayWritable.java

License:Apache License

public void readFields(DataInput in) throws IOException {
    // values = new Writable[in.readInt()]; // construct values
    values = (Writable[]) Array.newInstance(valueClass, in.readInt());
    for (int i = 0; i < values.length; i++) {
        Writable value = WritableFactories.newInstance(valueClass);
        value.readFields(in); // read a value
        values[i] = value; // store it in values
    }// w  w w  .  jav a 2s . c o m
}

From source file:org.opencloudengine.flamingo.mapreduce.util.WritableUtils.java

License:Apache License

/**
 * Read fields from byteArray to a Writeable object.
 *
 * @param byteArray      Byte array to find the fields in.
 * @param writableObject Object to fill in the fields.
 */// w  w  w  .j  a  v  a2  s.co m
public static void readFieldsFromByteArray(byte[] byteArray, Writable writableObject) {
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(byteArray));
    try {
        writableObject.readFields(inputStream);
    } catch (IOException e) {
        throw new IllegalStateException("readFieldsFromByteArray: IOException", e);
    }
}

From source file:org.opencloudengine.flamingo.mapreduce.util.WritableUtils.java

License:Apache License

/**
 * Read fields from byteArray to a list of Writeable objects.
 *
 * @param byteArray     Byte array to find the fields in.
 * @param writableClass Class of the objects to instantiate.
 * @param conf          Configuration used for instantiation (i.e Configurable)
 * @return List of writable objects.//from w w w.  j a  v  a 2s  . co  m
 */
public static List<? extends Writable> readListFieldsFromByteArray(byte[] byteArray,
        Class<? extends Writable> writableClass, Configuration conf) {
    try {
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(byteArray));
        int size = inputStream.readInt();
        List<Writable> writableList = new ArrayList<Writable>(size);
        for (int i = 0; i < size; ++i) {
            Writable writable = ReflectionUtils.newInstance(writableClass, conf);
            writable.readFields(inputStream);
            writableList.add(writable);
        }
        return writableList;
    } catch (IOException e) {
        throw new IllegalStateException("readListFieldsFromZnode: IOException", e);
    }
}

From source file:org.qcri.algebra.DummyRecordWriter.java

License:Apache License

private void cloneWritable(Writable from, Writable to) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    from.write(dos);/*from   w ww.  j a v a  2 s.c  o m*/
    dos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    to.readFields(dis);
}

From source file:org.qcri.pca.MahoutCompatibilityTest.java

License:Apache License

private static void writeAndRead(Writable toWrite, Writable toRead) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {//from ww  w .  j  a  va2  s  .  c  o m
        toWrite.write(dos);
    } finally {
        Closeables.close(dos, true);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    try {
        toRead.readFields(dis);
    } finally {
        Closeables.close(dos, true);
    }
}

From source file:org.sf.xrime.model.edge.EdgeSet.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//  ww  w .ja v a 2s. c o m
public void readFields(DataInput in) throws IOException {
    // Clear the container.
    _edges.clear();
    // Determine the size.
    int size = in.readInt();
    if (size > 0) {
        // All edges in the set should have the same type.
        String className = Text.readString(in);
        try {
            Class instanceClass;
            instanceClass = Class.forName(className);
            for (int i = 0; i < size; i++) {
                Writable writable = WritableFactories.newInstance(instanceClass, null);
                writable.readFields(in);
                if (writable instanceof Edge) {
                    addEdge((Edge) writable);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.sf.xrime.model.label.Labels.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void readFields(DataInput in) throws IOException {
    // Clear the container.
    labels.clear();/* w  ww  .  j a v a  2  s.  c o m*/
    // Determine the size of container.
    int size = in.readInt();
    while (size-- > 0) {
        // Each key is a string, but the label value may be of different types.
        String key = Text.readString(in);
        String valueClassName = Text.readString(in);

        try {
            Class instanceClass;
            instanceClass = Class.forName(valueClassName);
            Writable writable = WritableFactories.newInstance(instanceClass, null);
            writable.readFields(in);
            labels.put(key, writable);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    }
}

From source file:org.sf.xrime.model.path.PathAsVertexesList.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*  ww w. j a  va  2  s .  c  o  m*/
public void readFields(DataInput in) throws IOException {
    // Clear the container.
    _vertexes.clear();
    // Determine the size.
    int size = in.readInt();
    if (size > 0) {
        // All vertexes in the set should have the same type.
        String className = Text.readString(in);
        try {
            Class instanceClass;
            instanceClass = Class.forName(className);
            for (int i = 0; i < size; i++) {
                Writable writable = WritableFactories.newInstance(instanceClass, null);
                writable.readFields(in);
                if (writable instanceof Vertex) {
                    addVertex((Vertex) writable);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}