Thursday, June 10, 2010

How to convert an Object into Byte Array


This is a simple program to convert a java object into byte array, This program comes handy when we try to deep clone an object which is my next blog.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;


/**
 * 
 * @author shailesh.chandra
 *
 */
public class ObjectUtil {

 public static void main(String[] args) throws Exception {

  Object object = new Date();

  byte[] byteArray = convertByteArray(object);

  Object readObject = readByteArray(byteArray);

  System.out.println(readObject);

 }

 /**
  * 
  * @param object
  * @return
  * @throws Exception
  */
 private static byte[] convertByteArray(Object object) throws Exception {
  byte[] byteArray;

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  objectOutputStream.writeObject(object);

  byteArray = byteArrayOutputStream.toByteArray();
  return byteArray;
 }

 /**
  * 
  * @param byteArray
  * @throws IOException
  * @throws Exception
  */
 private static Object readByteArray(byte[] byteArray) throws Exception {
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
  ObjectInputStream objectOutputStream = new ObjectInputStream(byteArrayInputStream);
  Object readObject = objectOutputStream.readObject();

  objectOutputStream.close();
  byteArrayInputStream.close();

  return readObject;
 }

}

Deep Clone a object in Java

Below is the simple program to deep clone a object in java


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author shailesh.chandra
 *
 */
public class DeepCloneExample implements Cloneable, Serializable {

/**
*
*/
private static final long serialVersionUID = 6514927441919765349L;

public static void main(String[] args) throws CloneNotSupportedException {
DeepCloneExample deepCloneExample = new DeepCloneExample();
System.out.println(deepCloneExample);
DeepCloneExample cloneObject = (DeepCloneExample) deepCloneExample.clone();
System.out.println(cloneObject);
}

@Override
protected Object clone() throws CloneNotSupportedException {

Object object = deepClone();
return object;

}

/**
*
* @return
* @throws CloneNotSupportedException
*/
protected Object deepClone() throws CloneNotSupportedException {
DeepCloneExample clone;
try {
clone = null;
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(100);
ObjectOutputStream objectoutputstream = new ObjectOutputStream(bytearrayoutputstream);
objectoutputstream.writeObject(this);
byte abyte0[] = bytearrayoutputstream.toByteArray();
objectoutputstream.close();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
ObjectInputStream objectinputstream = new ObjectInputStream(bytearrayinputstream);
clone = (DeepCloneExample) objectinputstream.readObject();
objectinputstream.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new CloneNotSupportedException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
throw new CloneNotSupportedException(e.getMessage());
}
return clone;

}
}