Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

JCOGenericBytes.java

00001 package edu.virtualschool.jco;
00002 import java.io.ByteArrayOutputStream;
00003 import java.io.IOException;
00004 import java.io.ObjectOutputStream;
00005 import java.io.Serializable;
00006 
00012 public abstract class JCOGenericBytes implements Serializable
00013 {
00015   final byte[] bytes;
00016   
00021   public JCOGenericBytes(byte[] bytes)
00022   {
00023     this.bytes = bytes;
00024   }
00030   JCOGenericBytes(Serializable object) throws JCOFault
00031   {
00032     try
00033     {
00034       ByteArrayOutputStream bos = new ByteArrayOutputStream();
00035       ObjectOutputStream oos = new ObjectOutputStream(bos);
00036       oos.writeObject(object);
00037       oos.close();
00038       this.bytes = bos.toByteArray();
00039     }
00040     catch (IOException e)
00041     {
00042       throw new JCOFault(e, e);
00043     }
00044   }
00049   public boolean equals(Object o)
00050   {
00051     if (o instanceof JCOGenericBytes)
00052     {
00053         JCOGenericBytes that = (JCOGenericBytes)o;
00054         return isEqualBytes(bytes, that.bytes);
00055     }
00056     else
00057       return o == this;
00058   }
00065   public final static boolean isEqualBytes(byte[] b1, byte[] b2)
00066   {
00067     if (b1.length != b2.length)
00068       return false;
00069       
00070     for (int i = 0; i < b1.length; i++)
00071       if (b1[i] != b2[i])
00072         return false;
00073     
00074     return true;
00075   }
00080   public int hashCode()
00081   {
00082     int hashCode = 0;
00083     for (int i = 0; i < bytes.length; i++)
00084         hashCode = 31*hashCode + bytes[i];
00085     return hashCode;
00086   }
00091   public final byte[] getBytes()
00092   {
00093     return bytes;
00094   }
00095   static final long serialVersionUID = -3017285087236141065L;
00096 }