/*************************************************************************************************
 * Util.class
 * Class of utility methods.
 *                                                       Copyright(c) 2000-2003 Mikio Hirabayashi
 *************************************************************************************************/


package qdbm;


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


/**
 * Class of utility methods.
 */
public class Util {
  //----------------------------------------------------------------
  // public static constants
  //----------------------------------------------------------------
  /** size of a long integer */
  private static final int LONGSIZE = 8;
  //----------------------------------------------------------------
  // public static methods
  //----------------------------------------------------------------
  /**
   * Serialize an object.
   * @param obj a serializable object.
   * @return a byte array of the serialized object or null if an error occurs.
   */
  public static byte[] serialize(Object obj){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
      oos = new ObjectOutputStream(baos);
      oos.writeObject(obj);
      oos.flush();
      baos.flush();
      return baos.toByteArray();
    } catch(IOException e){
      return null;
    } finally {
      try {
        if(oos != null) oos.close();
      } catch(IOException e){}
    }
  }
  /**
   * Deserialize a serialized object.
   * @param serial a byte array of the serialized object.
   * @return an original object or null if an error occurs.
   */
  public static Object deserialize(byte[] serial){
    ByteArrayInputStream bais = new ByteArrayInputStream(serial);
    ObjectInputStream ois = null;
    try {
      ois = new ObjectInputStream(bais);
      return ois.readObject();
    } catch(IOException e){
      return null;
    } catch(ClassNotFoundException e){
      return null;
    } finally {
      try {
        if(ois != null) ois.close();
      } catch(IOException e){}
    }
  }
  /**
   * Serialize a long integer.
   * @param num a long integer.
   * @return a byte array of the serialized long integer.
   */
  public static byte[] serializeLong(long num){
    byte[] serial = new byte[LONGSIZE];
    for(int i = 0; i < LONGSIZE; i++){
      serial[LONGSIZE-i-1] = (byte)(num & 0xff);
      num >>= 8;
    }
    return serial;
  }
  /**
   * Deserialize a serialized long integer.
   * @param serial a byte array of a serialized long integer.
   * @return the long value.
   * @throws IllegalArgumentException thrown if the size of the array is invalid.
   */
  public static long deserializeLong(byte[] serial) throws IllegalArgumentException {
    if(serial.length != LONGSIZE) throw new IllegalArgumentException();
    int num = 0;
    for(int i = 0; i < LONGSIZE; i++){
      num *= 256;
      num += serial[i] + ((serial[i] < 0) ? 256 : 0);
    }
    return num;
  }
  //----------------------------------------------------------------
  // private methods
  //----------------------------------------------------------------
  /* Default constructor.
   * This should not be used. */
  private Util() throws Exception {
    throw new Exception();
  }
}



/* END OF FILE */
