1 23 24 package org.objectweb.jorm.naming.lib; 25 26 import org.objectweb.jorm.naming.api.PExceptionNameCoding; 27 import org.objectweb.jorm.naming.api.PNCStringCoder; 28 29 import java.text.DateFormat ; 30 import java.text.ParseException ; 31 import java.util.Date ; 32 import java.math.BigInteger ; 33 import java.math.BigDecimal ; 34 35 49 public class BasicStringCoder implements PNCStringCoder { 50 private String toDecode = null; 51 private int nextToDecode = 0; 52 private StringBuffer toEncode = null; 53 54 public BasicStringCoder() { 55 toEncode = new StringBuffer (64); 56 } 57 58 public BasicStringCoder(String en) { 59 toDecode = en; 60 } 61 62 private byte hex2int(char c) throws PExceptionNameCoding { 63 if ((c >= '0') && (c <= '9')) 64 return (byte) (c - '0'); 65 if ((c >= 'A') && (c <= 'F')) 66 return (byte) (c - 'A' + 10); 67 throw new PExceptionNameCoding("Cannot decode: wrong hexadecimal code."); 68 } 69 70 private void long2hexIntoSB(long l, char[] ev) { 71 for (int i = ev.length - 1; i >= 0; i--) { 72 byte b = (byte) (l & 0xF); 73 if (b < 10) 74 ev[i] = (char) (b + '0'); 75 else 76 ev[i] = (char) (b - 10 + 'A'); 77 l >>= 4; 78 } 79 for (int i = 0; i < ev.length; i++) 80 toEncode.append(ev[i]); 81 } 82 83 public byte getByte() throws PExceptionNameCoding { 84 if (toDecode == null) 85 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 86 if ((nextToDecode + 2) > toDecode.length()) 87 throw new PExceptionNameCoding("Cannot decode: no more data."); 88 byte res = (byte) (hex2int(toDecode.charAt(nextToDecode++)) << 4); 89 res += hex2int(toDecode.charAt(nextToDecode++)); 90 return res; 91 } 92 93 public Byte getObyte() throws PExceptionNameCoding { 94 return new Byte (getByte()); 95 } 96 97 public char getChar() throws PExceptionNameCoding { 98 if (toDecode == null) 99 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 100 if ((nextToDecode + 1) > toDecode.length()) 101 throw new PExceptionNameCoding("Cannot decode: no more data."); 102 char res = toDecode.charAt(nextToDecode++); 103 return res; 104 } 105 106 public Character getOchar() throws PExceptionNameCoding { 107 return new Character (getChar()); 108 } 109 110 public short getShort() throws PExceptionNameCoding { 111 if (toDecode == null) 112 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 113 if ((nextToDecode + 4) > toDecode.length()) 114 throw new PExceptionNameCoding("Cannot decode: no more data."); 115 short res = 0; 116 for (int i = 0; i < 4; i++) { 117 res <<= 4; 118 res += hex2int(toDecode.charAt(nextToDecode++)); 119 } 120 return res; 121 } 122 123 public Short getOshort() throws PExceptionNameCoding { 124 return new Short (getShort()); 125 } 126 127 public int getInt() throws PExceptionNameCoding { 128 if (toDecode == null) 129 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 130 if ((nextToDecode + 8) > toDecode.length()) 131 throw new PExceptionNameCoding("Cannot decode: no more data."); 132 int res = 0; 133 for (int i = 0; i < 8; i++) { 134 res <<= 4; 135 res += hex2int(toDecode.charAt(nextToDecode++)); 136 } 137 return res; 138 } 139 140 public Integer getOint() throws PExceptionNameCoding { 141 return new Integer (getInt()); 142 } 143 144 public long getLong() throws PExceptionNameCoding { 145 if (toDecode == null) 146 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 147 if ((nextToDecode + 16) > toDecode.length()) 148 throw new PExceptionNameCoding("Cannot decode: no more data."); 149 long res = 0; 150 for (int i = 0; i < 16; i++) { 151 res <<= 4; 152 res += hex2int(toDecode.charAt(nextToDecode++)); 153 } 154 return res; 155 } 156 157 public Long getOlong() throws PExceptionNameCoding { 158 return new Long (getLong()); 159 } 160 161 public String getString() throws PExceptionNameCoding { 162 if (toDecode == null) 163 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 164 int len = getShort(); 165 if ((nextToDecode + len) > toDecode.length()) 166 throw new PExceptionNameCoding("Cannot decode: no more data."); 167 String res = toDecode.substring(nextToDecode, nextToDecode + len); 168 nextToDecode += len; 169 return res; 170 } 171 172 public Date getDate() throws PExceptionNameCoding { 173 if (toDecode == null) 174 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 175 int len = getByte(); 176 if ((nextToDecode + len) > toDecode.length()) 177 throw new PExceptionNameCoding("Cannot decode: no more data."); 178 try { 179 Date res = DateFormat.getDateTimeInstance().parse(toDecode.substring(nextToDecode, nextToDecode + len)); 180 nextToDecode += len; 181 return res; 182 } catch (ParseException pe) { 183 throw new PExceptionNameCoding(pe, "Cannot decode: wrong Date format."); 184 } 185 } 186 187 public BigInteger getBigInteger() throws PExceptionNameCoding { 188 if (toDecode == null) 189 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 190 int len = getByte(); 191 if ((nextToDecode + len) > toDecode.length()) 192 throw new PExceptionNameCoding("Cannot decode: no more data."); 193 BigInteger res = new BigInteger (toDecode.substring(nextToDecode, nextToDecode + len)); 194 nextToDecode += len; 195 return res; 196 } 197 198 public BigDecimal getBigDecimal() throws PExceptionNameCoding { 199 if (toDecode == null) 200 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 201 int len = getByte(); 202 if ((nextToDecode + len) > toDecode.length()) 203 throw new PExceptionNameCoding("Cannot decode: no more data."); 204 BigDecimal res = new BigDecimal (toDecode.substring(nextToDecode, nextToDecode + len)); 205 nextToDecode += len; 206 return res; 207 } 208 209 public byte[] getByteArray() throws PExceptionNameCoding { 210 if (toDecode == null) 211 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 212 int len = getByte(); 213 if ((nextToDecode + len) > toDecode.length()) 214 throw new PExceptionNameCoding("Cannot decode: no more data."); 215 return new byte[0]; 217 } 218 219 public char[] getCharArray() throws PExceptionNameCoding { 220 if (toDecode == null) 221 throw new PExceptionNameCoding("Cannot decode: this is an encoder object."); 222 int len = getByte(); 223 if ((nextToDecode + len) > toDecode.length()) 224 throw new PExceptionNameCoding("Cannot decode: no more data."); 225 return new char[0]; 227 } 228 229 public void putByte(byte val) throws PExceptionNameCoding { 230 if (toEncode == null) 231 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 232 long2hexIntoSB(val, new char[2]); 233 } 234 235 public void putObyte(Byte val) throws PExceptionNameCoding { 236 putByte(val.byteValue()); 237 } 238 239 public void putChar(char val) throws PExceptionNameCoding { 240 if (toEncode == null) 241 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 242 toEncode.append(val); 243 } 244 245 public void putOchar(Character val) throws PExceptionNameCoding { 246 putChar(val.charValue()); 247 } 248 249 public void putShort(short val) throws PExceptionNameCoding { 250 if (toEncode == null) 251 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 252 long2hexIntoSB(val, new char[4]); 253 } 254 255 public void putOshort(Short val) throws PExceptionNameCoding { 256 putShort(val.shortValue()); 257 } 258 259 public void putInt(int val) throws PExceptionNameCoding { 260 if (toEncode == null) 261 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 262 long2hexIntoSB(val, new char[8]); 263 } 264 265 public void putOint(Integer val) throws PExceptionNameCoding { 266 putInt(val.intValue()); 267 } 268 269 public void putLong(long val) throws PExceptionNameCoding { 270 if (toEncode == null) 271 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 272 long2hexIntoSB(val, new char[16]); 273 } 274 275 public void putOlong(Long val) throws PExceptionNameCoding { 276 putLong(val.longValue()); 277 } 278 279 public void putString(String val) throws PExceptionNameCoding { 280 if (toEncode == null) 281 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 282 if (val.length() > Short.MAX_VALUE) 283 throw new PExceptionNameCoding("Cannot encode: String field too long."); 284 putShort((short) val.length()); 285 toEncode.append(val); 286 } 287 288 public void putDate(Date val) throws PExceptionNameCoding { 289 if (toEncode == null) 290 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 291 String ds = DateFormat.getDateTimeInstance().format(val); 292 if (ds.length() > Byte.MAX_VALUE) 293 throw new PExceptionNameCoding("Cannot encode: Date field too long."); 294 putByte((byte) ds.length()); 295 toEncode.append(ds); 296 } 297 298 public void putBigInteger(BigInteger val) throws PExceptionNameCoding { 299 if (toEncode == null) 300 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 301 String ds = val.toString(); 302 if (ds.length() > Byte.MAX_VALUE) 303 throw new PExceptionNameCoding("Cannot encode: BigInteger field too long."); 304 putByte((byte) ds.length()); 305 toEncode.append(ds); 306 } 307 308 public void putBigDecimal(BigDecimal val) throws PExceptionNameCoding { 309 if (toEncode == null) 310 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 311 String ds = val.toString(); 312 if (ds.length() > Byte.MAX_VALUE) 313 throw new PExceptionNameCoding("Cannot encode: BigDecimal field too long."); 314 putByte((byte) ds.length()); 315 toEncode.append(ds); 316 } 317 318 public void putByteArray(byte[] va) throws PExceptionNameCoding { 319 if (toEncode == null) 320 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 321 } 323 324 public void putCharArray(char[] val) throws PExceptionNameCoding { 325 if (toEncode == null) 326 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 327 } 329 330 public String getStringCode() throws PExceptionNameCoding { 331 if (toEncode == null) 332 throw new PExceptionNameCoding("Cannot encode: this is a decoder object."); 333 return toEncode.toString(); 334 } 335 } 336 | Popular Tags |