KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > rowio > RowOutputBinary


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.rowio;
33
34 import java.math.BigDecimal JavaDoc;
35 import java.math.BigInteger JavaDoc;
36 import java.sql.Date JavaDoc;
37 import java.sql.Time JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39
40 import org.hsqldb.CachedRow;
41 import org.hsqldb.Trace;
42 import org.hsqldb.Types;
43 import org.hsqldb.lib.StringConverter;
44 import org.hsqldb.lib.java.JavaSystem;
45 import org.hsqldb.types.Binary;
46 import org.hsqldb.types.JavaObject;
47
48 /**
49  * Provides methods for writing the data for a row to a
50  * byte array. The new format of data consists of mainly binary values
51  * and is not compatible with v.1.6.x databases.
52  *
53  * @author sqlbob@users (RMP)
54  * @author fredt@users
55  * @version 1.7.2
56  * @since 1.7.0
57  */

58 public class RowOutputBinary extends RowOutputBase {
59
60     private static final int INT_STORE_SIZE = 4;
61     int storageSize;
62
63     public RowOutputBinary() {
64         super();
65     }
66
67     public RowOutputBinary(int initialSize) {
68         super(initialSize);
69     }
70
71     /**
72      * Constructor used for network transmission of result sets
73      *
74      * @exception IOException when an IO error is encountered
75      */

76     public RowOutputBinary(byte[] buffer) {
77         super(buffer);
78     }
79
80 // fredt@users - comment - methods for writing column type, name and data size
81
public void writeShortData(short i) {
82         writeShort(i);
83     }
84
85     public void writeIntData(int i) {
86         writeInt(i);
87     }
88
89     public void writeIntData(int i, int position) {
90
91         int temp = count;
92
93         count = position;
94
95         writeInt(i);
96
97         if (count < temp) {
98             count = temp;
99         }
100     }
101
102     public void writeLongData(long i) {
103         this.writeLong(i);
104     }
105
106     public void writeEnd() {
107
108         // fredt - this value is used in 1.7.0 when reading back, for a
109
// 'data integrity' check
110
// has been removed in 1.7.2 as compatibility is no longer necessary
111
// writeInt(pos);
112
for (; count < storageSize; ) {
113             this.write(0);
114         }
115     }
116
117     public void writeSize(int size) {
118
119         storageSize = size;
120
121         writeInt(size);
122     }
123
124     public void writeType(int type) {
125         writeShort(type);
126     }
127
128     public void writeString(String JavaDoc s) {
129
130         int temp = count;
131
132         writeInt(0);
133         StringConverter.writeUTF(s, this);
134         writeIntData(count - temp - 4, temp);
135     }
136
137     /**
138      * Calculate the size of byte array required to store a row.
139      *
140      * @param row - a database row
141      * @return size of byte array
142      * @exception HsqlException When data is inconsistent
143      */

144     public int getSize(CachedRow row) {
145
146         Object JavaDoc[] data = row.getData();
147         int[] type = row.getTable().getColumnTypes();
148         int cols = row.getTable().getColumnCount();
149
150         return INT_STORE_SIZE + getSize(data, cols, type);
151     }
152
153     public static int getRowSize(CachedRow row) {
154
155         Object JavaDoc[] data = row.getData();
156         int[] type = row.getTable().getColumnTypes();
157         int cols = row.getTable().getColumnCount();
158
159         return getSize(data, cols, type);
160     }
161
162 // fredt@users - comment - methods used for writing each SQL type
163
protected void writeFieldType(int type) {
164         write(1);
165     }
166
167     protected void writeNull(int type) {
168         write(0);
169     }
170
171     protected void writeChar(String JavaDoc s, int t) {
172         writeString(s);
173     }
174
175     protected void writeSmallint(Number JavaDoc o) {
176         writeShort(o.intValue());
177     }
178
179     protected void writeInteger(Number JavaDoc o) {
180         writeInt(o.intValue());
181     }
182
183     protected void writeBigint(Number JavaDoc o) {
184         writeLong(o.longValue());
185     }
186
187     protected void writeReal(Double JavaDoc o, int type) {
188         writeLong(Double.doubleToLongBits((o.doubleValue())));
189     }
190
191     protected void writeDecimal(BigDecimal JavaDoc o) {
192
193         int scale = o.scale();
194         BigInteger JavaDoc bigint = JavaSystem.getUnscaledValue(o);
195         byte[] bytearr = bigint.toByteArray();
196
197         writeByteArray(bytearr);
198         writeInt(scale);
199     }
200
201     protected void writeBit(Boolean JavaDoc o) {
202         write(o.booleanValue() ? 1
203                                : 0);
204     }
205
206     protected void writeDate(Date JavaDoc o) {
207         writeLong(o.getTime());
208     }
209
210     protected void writeTime(Time JavaDoc o) {
211         writeLong(o.getTime());
212     }
213
214     protected void writeTimestamp(Timestamp JavaDoc o) {
215         writeLong(o.getTime());
216         writeInt(o.getNanos());
217     }
218
219     protected void writeOther(JavaObject o) {
220         writeByteArray(o.getBytes());
221     }
222
223     protected void writeBinary(Binary o, int t) {
224         writeByteArray(o.getBytes());
225     }
226
227 // fredt@users - comment - helper and conversion methods
228
protected void writeByteArray(byte[] b) {
229         writeInt(b.length);
230         write(b, 0, b.length);
231     }
232
233     /**
234      * Calculate the size of byte array required to store a row.
235      *
236      * @param data - the row data
237      * @param l - number of data[] elements to include in calculation
238      * @param type - array of java.sql.Types values
239      * @return size of byte array
240      * @exception HsqlException when data is inconsistent
241      */

242     private static int getSize(Object JavaDoc[] data, int l, int[] type) {
243
244         int s = 0;
245
246         for (int i = 0; i < l; i++) {
247             Object JavaDoc o = data[i];
248
249             s += 1; // type or null
250

251             if (o != null) {
252                 switch (type[i]) {
253
254                     case Types.NULL :
255                     case Types.CHAR :
256                     case Types.VARCHAR :
257                     case Types.VARCHAR_IGNORECASE :
258                     case Types.LONGVARCHAR :
259                         s += 4;
260                         s += StringConverter.getUTFSize((String JavaDoc) o);
261                         break;
262
263                     case Types.TINYINT :
264                     case Types.SMALLINT :
265                         s += 2;
266                         break;
267
268                     case Types.INTEGER :
269                         s += 4;
270                         break;
271
272                     case Types.BIGINT :
273                     case Types.REAL :
274                     case Types.FLOAT :
275                     case Types.DOUBLE :
276                         s += 8;
277                         break;
278
279                     case Types.NUMERIC :
280                     case Types.DECIMAL :
281                         s += 8;
282
283                         BigDecimal JavaDoc bigdecimal = (BigDecimal JavaDoc) o;
284                         BigInteger JavaDoc bigint =
285                             JavaSystem.getUnscaledValue(bigdecimal);
286
287                         s += bigint.toByteArray().length;
288                         break;
289
290                     case Types.BOOLEAN :
291                         s += 1;
292                         break;
293
294                     case Types.DATE :
295                     case Types.TIME :
296                         s += 8;
297                         break;
298
299                     case Types.TIMESTAMP :
300                         s += 12;
301                         break;
302
303                     case Types.BINARY :
304                     case Types.VARBINARY :
305                     case Types.LONGVARBINARY :
306                         s += 4;
307                         s += ((Binary) o).getBytesLength();
308                         break;
309
310                     case Types.OTHER :
311                         JavaObject jo = (JavaObject) o;
312
313                         s += 4;
314                         s += jo.getBytesLength();
315                         break;
316
317                     default :
318                         Trace.printSystemOut(Trace.FUNCTION_NOT_SUPPORTED
319                                              + " "
320                                              + Types.getTypeString(type[i]));
321                 }
322             }
323         }
324
325         return s;
326     }
327
328     /**
329      * @param extra amount of extra space
330      */

331     public void ensureRoom(int extra) {
332         super.ensureRoom(extra);
333     }
334
335     public void reset() {
336
337         super.reset();
338
339         storageSize = 0;
340     }
341
342     public void reset(int newSize) {
343
344         super.reset(newSize);
345
346         storageSize = 0;
347     }
348
349     public void setBuffer(byte[] buffer) {
350
351         buf = buffer;
352
353         reset();
354     }
355 }
356
Popular Tags