KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43
44 import com.quadcap.util.Debug;
45 import com.quadcap.util.Util;
46
47 /**
48  * Simple key serialization primitives.
49  *
50  * @author Stan Bailes
51  */

52 public final class KeyStream {
53     public static final int COMPOUND = 0;
54     public static final int INTEGER = 1;
55     public static final int STRING = 2;
56     public static final int NULL = 3;
57
58     OutputStream JavaDoc out;
59     boolean caseSensitive = false;
60
61     public KeyStream(OutputStream JavaDoc out, boolean caseSensitive) {
62     this.out = out;
63         this.caseSensitive = caseSensitive;
64     }
65
66     final void writeLength(int len) throws IOException JavaDoc {
67     writeLength(len, STRING);
68     }
69
70     final void writeLength(int len, int type) throws IOException JavaDoc {
71     //Debug.println("writeLength(" + len + ", " + type + ")");
72
if (len <= 31) {
73         out.write((len << 2) | type);
74     } else {
75         out.write(0x80 | ((len & 0x1f00) >> 6) | type);
76         out.write(len & 0xff);
77     }
78     }
79
80     public final void writeNull(boolean last) throws IOException JavaDoc {
81     //Debug.println("writeNull(" + last + ")");
82
int len = last ? 1 : 0;
83     out.write((len << 2) | NULL);
84     }
85
86     public final void write(int b) throws IOException JavaDoc {
87     //Debug.println("write(" + b + ")");
88
writeByte((byte)b);
89     }
90
91     public final void write(byte[] b) throws IOException JavaDoc {
92     writeLength(b.length);
93     for (int i = 0; i < b.length; i++) {
94         out.write(b[i]);
95     }
96     }
97
98     public final void writeInt(byte[] b) throws IOException JavaDoc {
99     //Debug.println("writeInt(" + Util.hexBytes(b) + ")");
100
writeLength(b.length, INTEGER);
101     for (int i = 0; i < b.length; i++) {
102         out.write(b[i]);
103     }
104     }
105
106     public final void write(byte b[], int off, int len) throws IOException JavaDoc {
107     //Debug.println("write(" + Util.hexBytes(b, off, len) + ")");
108
writeLength(len);
109     for (int i = 0; i < len; i++) {
110         out.write(b[off + i]);
111     }
112     }
113
114     public final void writeBoolean(boolean val) throws IOException JavaDoc {
115     //Debug.println("writeBoolean(" + val + ")");
116
write(val ? 1 : 0);
117     }
118
119     public final void writeByte(byte val) throws IOException JavaDoc {
120     //Debug.println("writeByte(" + val + ")");
121
out.write((1 << 2) | INTEGER);
122     out.write(val);
123     }
124
125 // public final void writeChar(int val) throws IOException {
126
// //Debug.println("writeChar(" + val + ")");
127
// writeInt(val);
128
// }
129

130     
131     public final void writeShort(short val) throws IOException JavaDoc {
132     //Debug.println("writeShort(" + val + ")");
133
if (val == (byte)val) {
134         writeByte((byte)val);
135     } else {
136         out.write((2 << 2) | INTEGER);
137         out.write((val >> 8) & 0xff);
138         out.write(val & 0xff);
139     }
140     }
141
142     public final void writeInt(int val) throws IOException JavaDoc {
143     //Debug.println("writeInt(" + val + ")");
144
if (val == (short)val) {
145         writeShort((short)val);
146     } else {
147         out.write((4 << 2) | INTEGER);
148         out.write((val >> 24) & 0xff);
149         out.write((val >> 16) & 0xff);
150         out.write((val >> 8) & 0xff);
151         out.write(val & 0xff);
152     }
153     }
154
155     public final void writeLong(long val) throws IOException JavaDoc {
156     //Debug.println("writeLong(" + val + ")");
157
if (val == (int)val) {
158         writeInt((int)val);
159     } else {
160         out.write((8 << 2) | INTEGER);
161         out.write((int)((val >> 56) & 0xff));
162         out.write((int)((val >> 48) & 0xff));
163         out.write((int)((val >> 40) & 0xff));
164         out.write((int)((val >> 32) & 0xff));
165         out.write((int)((val >> 24) & 0xff));
166         out.write((int)((val >> 16) & 0xff));
167         out.write((int)((val >> 8) & 0xff));
168         out.write((int)(val & 0xff));
169     }
170     }
171
172     public final void writeFloat(float f) throws IOException JavaDoc {
173     //Debug.println("writeFloat(" + f + ")");
174
writeInt(Float.floatToIntBits(f));
175     }
176
177     public final void writeDouble(double d) throws IOException JavaDoc {
178     //Debug.println("writeDouble(" + d + ")");
179
writeLong(Double.doubleToLongBits(d));
180     }
181
182     public final void writeChars(String JavaDoc s) throws IOException JavaDoc {
183         final int len = s.length();
184     writeLength(len * 2);
185         if (caseSensitive) {
186             for (int i = 0; i < len; i++) {
187                 final char c = s.charAt(i);
188                 out.write((c >> 8) & 0xff);
189                 out.write(c & 0xff);
190             }
191         } else {
192             for (int i = 0; i < len; i++) {
193                 final char c = Character.toLowerCase(s.charAt(i));
194                 out.write((c >> 8) & 0xff);
195                 out.write(c & 0xff);
196             }
197         }
198     }
199 }
200
Popular Tags