View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core;
20  
21  import java.io.*;
22  import java.util.*;
23  
24  /***
25   *
26   * @author  administratör
27   * @version 
28   */
29  public abstract class BinaryData implements Serializable
30  {
31      // Constants ---------------------------------------------------------------
32      private final static String HEX_DIGITS = "0123456789ABCDEF"; 
33      
34      // Static data members -----------------------------------------------------
35      private static final HashMap sBinaryNameClassMap = new HashMap();
36      private static final HashMap sBinaryClassNameMap = new HashMap();
37          
38      // Static methods ----------------------------------------------------------
39      public static void registerBinaryClass(String key, Class binaryDataClass)
40      {
41  //        if(!binaryDataClass.isAssignableFrom(BinaryData.class))
42  //            throw new InvalidClassException("Class must be a BinaryData descendant!");
43          sBinaryNameClassMap.remove(key);
44          sBinaryNameClassMap.put(key, binaryDataClass);
45          
46          try
47          {
48              BinaryData binaryData = (BinaryData)binaryDataClass.newInstance();
49              sBinaryClassNameMap.remove(binaryData.getDataClass());
50              sBinaryClassNameMap.put(binaryData.getDataClass(), key);
51          }
52          catch(Exception e)
53          {
54          }
55      }
56      
57      public static String mapBinaryType(Class key)
58      {
59          return (String)sBinaryClassNameMap.get(key);
60      }
61      
62      public static BinaryData create(Object key)
63      {
64          try
65          {
66              return (BinaryData)((Class)sBinaryNameClassMap.get(key)).newInstance();
67          }
68          catch(Exception e)
69          {
70              return null;
71          }
72      }
73      
74      public static BinaryData create(Object key, InputStream in) throws IOException
75      {
76          BinaryData binData = create(key);
77          if(binData!=null)
78              binData.read(in);
79          return binData;
80      }
81      
82      public static BinaryData create(Object key, byte[] byteArray)
83      {
84          BinaryData binData = create(key);
85          if(binData!=null)
86              binData.setByteArray(byteArray);
87          return binData;
88      }
89      
90      // Abstract methods --------------------------------------------------------
91      
92      public abstract byte[] getByteArray();
93      public abstract void setByteArray(byte[] byteArray);
94          
95      public abstract Class getDataClass();
96      public abstract Object getData();
97      public abstract void setData(Object data);
98          
99      public abstract void read(InputStream in) throws IOException;
100     public abstract void write(OutputStream out) throws IOException;
101     
102     // Help methods ------------------------------------------------------------
103     
104     /*** By defaults returns a hex sequence starting with "0x" followed by two
105      * hex digits for each byte in the contained byte array.
106      */
107     public String toString()
108     {
109         byte[] byteArray = this.getByteArray();
110         if(byteArray==null || byteArray.length==0)
111             return null;
112         
113         StringBuffer buffer = new StringBuffer(byteArray.length*2+2);
114         buffer.append("0x");
115         for(int j=0; j<byteArray.length; j++)
116         {
117             buffer.append(HEX_DIGITS.charAt((byteArray[j] >>> 4) & 0x0F));
118             buffer.append(HEX_DIGITS.charAt(byteArray[j] & 0x0F));
119         }
120         return buffer.toString();
121     }
122     
123     /***
124      * Creates a hexadecmial string representation of a byte array
125      * 
126      * @param byteArray the byteArray that should be presented as a hexadecimal string
127      * @return returns a hexadecimal string representation of the byte array
128      */
129     public static String makeHexString(byte[] byteArray)
130     {
131         if(byteArray==null || byteArray.length==0)
132             return null;
133 
134         StringBuffer buffer = new StringBuffer(byteArray.length*2);
135         for(int j=0; j<byteArray.length; j++)
136         {
137             buffer.append(HEX_DIGITS.charAt((byteArray[j] >>> 4) & 0x0F));
138             buffer.append(HEX_DIGITS.charAt(byteArray[j] & 0x0F));
139         }
140         return buffer.toString();
141         
142     }
143 }