KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > ValueOctets


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.Externalizable JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.ObjectInput JavaDoc;
46 import java.io.ObjectOutput JavaDoc;
47
48 import com.quadcap.sql.io.ExternalizeProxy;
49
50 import com.quadcap.util.Debug;
51 import com.quadcap.util.Util;
52
53 /**
54  * A <b>byte array</b> value.
55  *
56  * @author Stan Bailes
57  */

58 public class ValueOctets extends Value implements Externalizable JavaDoc, ExternalizeProxy {
59     /** length in bits */
60     int length;
61     byte[] val;
62
63     /**
64      * Default constructor
65      */

66     public ValueOctets() {}
67
68     /**
69      * Constructor from string. A little SQL parsing in here...
70      */

71     public ValueOctets(String JavaDoc s) {
72     int slen = s.length() - 3;
73     if (s.charAt(0) == 'X') {
74         this.length = slen * 4;
75         this.val = new byte[(this.length + 7)/ 8];
76             int i = 0;
77             int p = 2;
78             if ((slen & 1) == 1) {
79                 val[i++] = (byte)Character.digit(s.charAt(p++), 16);
80             }
81             while (i < val.length) {
82                 val[i] = (byte)(Character.digit(s.charAt(p++), 16) << 4);
83                 val[i] += (byte)Character.digit(s.charAt(p++), 16);
84                 i++;
85             }
86     } else {
87         this.length = slen * 1;
88         this.val = new byte[(this.length + 7)/ 8];
89         for (int i = 0; i < slen; i++) {
90         int t = (i % 8);
91         val[i/8] |= Character.digit(s.charAt(i+2), 2) << t;
92         }
93     }
94     }
95
96     /**
97      * Constructor from byte array
98      */

99     public ValueOctets(byte[] val) {
100         init(val);
101     }
102
103     void init(byte[] v) {
104     this.val = new byte[v.length];
105     this.length = v.length * 8;
106     System.arraycopy(v, 0, val, 0, val.length);
107     }
108
109     /**
110      * ValueOctets constructor from byte array and length
111      */

112     public ValueOctets(byte[] val, int length) {
113     if (length <= 0) length = val.length * 8;
114     this.length = length;
115     this.val = new byte[(length + 7)/8];
116     System.arraycopy(val, 0, this.val, 0,
117              Math.min(val.length, this.val.length));
118     }
119
120     /**
121      * String representation: returned as ASCII hex.
122      */

123     public String JavaDoc toString() {
124     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
125     int cnt = length / 8;
126         int i = 0;
127     int mod = length % 8;
128     if (mod > 0) {
129         int tmp = val[i++];
130         if (mod > 4) {
131                 sb.append(Character.forDigit((tmp >> 4) & 0xf, 16));
132         }
133             sb.append(Character.forDigit(tmp & 0xf, 16));
134     }
135     while (i < cnt) {
136         int tmp = val[i++];
137         sb.append(Character.forDigit((tmp >> 4) & 0xf, 16));
138         sb.append(Character.forDigit(tmp & 0xf, 16));
139     }
140     return sb.toString();
141     }
142
143     /**
144      * Unary op: we only implement ISNULL
145      */

146     public Value unop(int op) throws ValueException {
147     switch (op) {
148     case Op.NULL:
149         return ValueBoolean.falseBoolean;
150     default:
151         throw new ValueException("Unary op: " + Op.toString(op) +
152                      " not implemented for this type");
153     }
154     }
155
156     /**
157      * First level binary op dispatch
158      */

159     public Value binop(int op, Value l) throws ValueException {
160     return l.binop(op, this);
161     }
162
163     /**
164      * Binary op: OCTETS vs NULL
165      */

166     public Value binop(int op, ValueNull r) throws ValueException {
167     switch (op) {
168     case Op.EQ:
169     case Op.NE:
170     case Op.LT:
171     case Op.LE:
172     case Op.GT:
173     case Op.GE:
174         return ValueUnknown.valueUnknown;
175     case Op.COMPARE:
176         return r.valCmpNull();
177     default:
178         throw badBinop(op, r);
179     }
180     }
181
182     /**
183      * Binary op: OCTETS vs OCTETS
184      */

185     public Value binop(int op, ValueOctets r) throws ValueException {
186     switch (op) {
187     case Op.EQ:
188         return new ValueBoolean(0 == Util.compareBytes(val, r.val));
189     case Op.NE:
190         return new ValueBoolean(0 != Util.compareBytes(val, r.val));
191     case Op.LT:
192         return new ValueBoolean(0 > Util.compareBytes(val, r.val));
193     case Op.LE:
194         return new ValueBoolean(0 >= Util.compareBytes(val, r.val));
195     case Op.GT:
196         return new ValueBoolean(0 < Util.compareBytes(val, r.val));
197     case Op.GE:
198         return new ValueBoolean(0 <= Util.compareBytes(val, r.val));
199     case Op.COMPARE:
200         return new ValueInteger(Util.compareBytes(val, r.val));
201     default:
202         throw badBinop(op, this, r);
203     }
204     }
205
206     /**
207      * Read myself from a stream
208      */

209     public void readExternal(ObjectInput JavaDoc in)
210     throws IOException JavaDoc, ClassNotFoundException JavaDoc
211     {
212     length = in.readInt();
213     int cnt = in.readInt();
214     val = new byte[cnt];
215     int rcnt = in.read(val);
216     }
217
218     /**
219      * Write myself to a stream
220      */

221     public void writeExternal(ObjectOutput JavaDoc out)
222     throws IOException JavaDoc
223     {
224     out.writeInt(length);
225     out.writeInt(val.length);
226     out.write(val);
227     }
228
229     /**
230      * Externable: read object
231      */

232     public Object JavaDoc readObject(ObjectInput JavaDoc in)
233     throws IOException JavaDoc, ClassNotFoundException JavaDoc
234     {
235     ValueOctets v = new ValueOctets();
236     v.readExternal(in);
237     return v;
238     }
239
240     /**
241      * Externable: write object
242      */

243     public void writeObject(ObjectOutput JavaDoc out, Object JavaDoc object)
244     throws IOException JavaDoc
245     {
246     ((ValueOctets)object).writeExternal(out);
247     }
248
249     /**
250      * JDBC Java mapping to: 'byte[]'
251      */

252     public Object JavaDoc asJavaObject() {
253     return val;
254     }
255
256     /**
257      * JDBC Java mapping from 'byte[]'
258      */

259     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
260     if (obj instanceof byte[]) {
261         init((byte[])obj);
262     } else {
263         throw new ValueException("bad type: " + obj);
264     }
265     }
266
267     /**
268      * Return the underlying byte array
269      */

270     public final byte[] getBytes() {
271     return val;
272     }
273
274     /**
275      * Return the length of the OCTETS value
276      */

277     public final int getLength() { return length; }
278
279     /**
280      * Return a binary input stream to read the OCTETS value
281      */

282     public final InputStream JavaDoc getBinaryStream() {
283     return new ByteArrayInputStream JavaDoc(val);
284     }
285
286     /**
287      * Convert to BINARY type
288      */

289     public Value convert(TypeBinary type) throws ValueException {
290     if (length != type.getMax()) {
291         return new ValueOctets(val, type.getMax());
292     }
293     return this;
294     }
295
296     /**
297      * Convert to VAR BINARY type
298      */

299     public Value convert(TypeVarBinary type) throws ValueException {
300     if (length > type.getMax()) {
301         return new ValueOctets(val, type.getMax());
302     }
303     return this;
304     }
305
306     /**
307      * Convert to BLOB
308      */

309     public Value convert(TypeBlob type) throws ValueException {
310         int blobLen = (length + 7) / 8;
311         byte[] blobVal = val;
312         if (blobLen != blobVal.length) {
313             blobVal = new byte[blobLen];
314             System.arraycopy(val, 0, blobVal, 0, blobLen);
315         }
316         return new ValueBlob(blobVal);
317     }
318
319     /**
320      * Convert to BOOLEAN
321      */

322     public Value convert(TypeBoolean type) throws ValueException {
323     for (int i = 0; i < val.length; i++) {
324         if (val[i] != 0) return ValueBoolean.trueBoolean;
325     }
326     return ValueBoolean.falseBoolean;
327     }
328
329     /**
330      * Convert to VARCHAR
331      */

332     public Value convert(TypeVarChar type) throws ValueException {
333         String JavaDoc s = new String JavaDoc(val, 0, (length+7)/8);
334         if (type.getMax() > 0 && s.length() > type.getMax()) {
335             // XXXX Should issue warning here
336
s = s.substring(0, type.getMax());
337         }
338         return new ValueString(s);
339     }
340
341     /**
342      * Convert to CHAR
343      */

344     public Value convert(TypeChar type) throws ValueException {
345         String JavaDoc s = new String JavaDoc(val, 0, (length+7)/8);
346         if (type.getMax() > 0 && s.length() > type.getMax()) {
347             // XXXX Should issue warning here
348
s = s.substring(0, type.getMax());
349         }
350         while (s.length() < type.getMax()) {
351             s = s + " ";
352         }
353         return new ValueString(s);
354     }
355
356     /**
357      * Return my type (BINARY(len))
358      */

359     public Type getType() {
360     return new TypeBinary(length);
361     }
362
363     /**
364      * Key serialization for this type
365      */

366     public void serializeKey(KeyStream out) throws IOException JavaDoc {
367     out.write(val);
368     }
369 }
370
Popular Tags