Java tutorial
/**************************************************************** * Licensed to the AOS Community (AOS) under one or more * * contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The AOS licenses this file * * to you 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. * ****************************************************************/ package io.aos.hdfs; // == WritableTestBase // == WritableTestBase-Deserialize import java.io.*; import org.apache.hadoop.io.Writable; import org.apache.hadoop.util.StringUtils; public class WritableTestBase { // vv WritableTestBase public static byte[] serialize(Writable writable) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(out); writable.write(dataOut); dataOut.close(); return out.toByteArray(); } // ^^ WritableTestBase // vv WritableTestBase-Deserialize public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(bytes); DataInputStream dataIn = new DataInputStream(in); writable.readFields(dataIn); dataIn.close(); return bytes; } // ^^ WritableTestBase-Deserialize public static String serializeToString(Writable src) throws IOException { return StringUtils.byteToHexString(serialize(src)); } public static String writeTo(Writable src, Writable dest) throws IOException { byte[] data = deserialize(dest, serialize(src)); return StringUtils.byteToHexString(data); } }