1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32 private final static String HEX_DIGITS = "0123456789ABCDEF";
33
34
35 private static final HashMap sBinaryNameClassMap = new HashMap();
36 private static final HashMap sBinaryClassNameMap = new HashMap();
37
38
39 public static void registerBinaryClass(String key, Class binaryDataClass)
40 {
41
42
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
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
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 }