KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > naming > lib > BasicStringCoder


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
30 import java.text.ParseException JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.math.BigInteger JavaDoc;
33 import java.math.BigDecimal JavaDoc;
34
35 /**
36  * The coding format supported by this coder is the following one:
37  * - char is coded as is
38  * (example 'A' <-> "A"
39  * - byte, short, int, long are coded to hexadecimal fixed size strings
40  * (example 395/short <-> "018B")
41  * - string is coded with its encoded short length followed by the string
42  * as is
43  * (example "Hello world!" <-> "000CHello World!"
44  * - date is coded with its encoded byte length followed by its string
45  * representation
46  * (example Date(837039928046) <-> "1407/10/96 4:5 PM, PDT"
47  * @author P. D?chamboux
48  */

49 public class BasicStringCoder implements PNCStringCoder {
50     private String JavaDoc toDecode = null;
51     private int nextToDecode = 0;
52     private StringBuffer JavaDoc toEncode = null;
53
54     public BasicStringCoder() {
55         toEncode = new StringBuffer JavaDoc(64);
56     }
57
58     public BasicStringCoder(String JavaDoc 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 JavaDoc getObyte() throws PExceptionNameCoding {
94         return new Byte JavaDoc(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 JavaDoc getOchar() throws PExceptionNameCoding {
107         return new Character JavaDoc(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 JavaDoc getOshort() throws PExceptionNameCoding {
124         return new Short JavaDoc(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 JavaDoc getOint() throws PExceptionNameCoding {
141         return new Integer JavaDoc(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 JavaDoc getOlong() throws PExceptionNameCoding {
158         return new Long JavaDoc(getLong());
159     }
160
161     public String JavaDoc 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 JavaDoc res = toDecode.substring(nextToDecode, nextToDecode + len);
168         nextToDecode += len;
169         return res;
170     }
171
172     public Date JavaDoc 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 JavaDoc res = DateFormat.getDateTimeInstance().parse(toDecode.substring(nextToDecode, nextToDecode + len));
180             nextToDecode += len;
181             return res;
182         } catch (ParseException JavaDoc pe) {
183             throw new PExceptionNameCoding(pe, "Cannot decode: wrong Date format.");
184         }
185     }
186
187     public BigInteger JavaDoc 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 JavaDoc res = new BigInteger JavaDoc(toDecode.substring(nextToDecode, nextToDecode + len));
194         nextToDecode += len;
195         return res;
196     }
197
198     public BigDecimal JavaDoc 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 JavaDoc res = new BigDecimal JavaDoc(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 //TODO
216
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 //TODO
226
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc val) throws PExceptionNameCoding {
276         putLong(val.longValue());
277     }
278
279     public void putString(String JavaDoc 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 JavaDoc val) throws PExceptionNameCoding {
289         if (toEncode == null)
290             throw new PExceptionNameCoding("Cannot encode: this is a decoder object.");
291         String JavaDoc 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 JavaDoc val) throws PExceptionNameCoding {
299         if (toEncode == null)
300             throw new PExceptionNameCoding("Cannot encode: this is a decoder object.");
301         String JavaDoc 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 JavaDoc val) throws PExceptionNameCoding {
309         if (toEncode == null)
310             throw new PExceptionNameCoding("Cannot encode: this is a decoder object.");
311         String JavaDoc 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 //TODO
322
}
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 //TODO
328
}
329
330     public String JavaDoc 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