Java tutorial
package bigsidemodel; /* * Cloud9: A Hadoop toolkit for working with big data * * Licensed under the Apache License, Version 2.0 (the "License"); you * may not use this file except in compliance with the License. You may * obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import org.apache.hadoop.io.Writable; import edu.umd.cloud9.io.array.ArrayListOfFloatsWritable; import edu.umd.cloud9.io.array.ArrayListOfIntsWritable; public class DataNode implements Writable { private int n; private float[] data; public DataNode() { } public int getN() { return n; } public float[] getData() { return data; } public void setN(int n) { this.n = n; } public void setData(float[] data) { this.data = data; } /** * Deserializes this object. * * @param in source for raw byte representation */ @Override public void readFields(DataInput in) throws IOException { n = in.readInt(); data = new float[n]; for (int k = 0; k < n; k++) data[k] = in.readFloat(); } /** * Serializes this object. * * @param out where to write the raw byte representation */ @Override public void write(DataOutput out) throws IOException { out.writeInt(n); for (int k = 0; k < n; k++) out.writeFloat(data[k]); } @Override public String toString() { String output = ""; for (int k = 0; k < n; k++) { output = output + data[k] + " "; output = output + "\n"; } return output; } /** * Returns the serialized representation of this object as a byte array. * * @return byte array representing the serialized representation of this object * @throws IOException */ public byte[] serialize() throws IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(bytesOut); write(dataOut); return bytesOut.toByteArray(); } /** * Creates object from a <code>DataInput</code>. * * @param in source for reading the serialized representation * @return newly-created object * @throws IOException */ public static DataNode create(DataInput in) throws IOException { DataNode m = new DataNode(); m.readFields(in); return m; } /** * Creates object from a byte array. * * @param bytes raw serialized representation * @return newly-created object * @throws IOException */ public static DataNode create(byte[] bytes) throws IOException { return create(new DataInputStream(new ByteArrayInputStream(bytes))); } }