KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.sql.Date JavaDoc;
37 import java.sql.Time JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39
40 import org.hsqldb.HsqlException;
41 import org.hsqldb.Trace;
42 import org.hsqldb.Types;
43 import org.hsqldb.lib.HsqlByteArrayInputStream;
44 import org.hsqldb.types.Binary;
45
46 /**
47  * Base class for reading the data for a database row in different formats.
48  * Defines the methods that are independent of storage format and declares
49  * the format-dependent methods that subclasses should define.
50  *
51  * @author sqlbob@users (RMP)
52  * @author fredt@users
53  * @version 1.7.2
54  * @since 1.7.0
55  */

56 public abstract class RowInputBase extends HsqlByteArrayInputStream {
57
58     static final int NO_POS = -1;
59
60     // fredt - initialisation may be unnecessary as it's done in resetRow()
61
protected int filePos = NO_POS;
62     protected int size;
63
64     public RowInputBase() {
65         this(new byte[4]);
66     }
67
68     /**
69      * Constructor takes a complete row
70      */

71     public RowInputBase(byte[] buf) {
72
73         super(buf);
74
75         size = buf.length;
76     }
77
78     public int getPos() {
79
80         if (filePos == NO_POS) {
81
82 // Trace.printSystemOut(Trace.DatabaseRowInput_getPos);
83
}
84
85         return (filePos);
86     }
87
88     public int getSize() {
89         return size;
90     }
91
92 // fredt@users - comment - methods used for node and type data
93
public abstract int readIntData() throws IOException JavaDoc;
94
95     public abstract long readLongData() throws IOException JavaDoc;
96
97     public abstract int readType() throws IOException JavaDoc;
98
99     public abstract String JavaDoc readString() throws IOException JavaDoc;
100
101 // fredt@users - comment - methods used for SQL types
102
protected abstract boolean checkNull() throws IOException JavaDoc;
103
104     protected abstract String JavaDoc readChar(int type)
105     throws IOException JavaDoc, HsqlException;
106
107     protected abstract Integer JavaDoc readSmallint()
108     throws IOException JavaDoc, HsqlException;
109
110     protected abstract Integer JavaDoc readInteger()
111     throws IOException JavaDoc, HsqlException;
112
113     protected abstract Long JavaDoc readBigint() throws IOException JavaDoc, HsqlException;
114
115     protected abstract Double JavaDoc readReal(int type)
116     throws IOException JavaDoc, HsqlException;
117
118     protected abstract BigDecimal JavaDoc readDecimal()
119     throws IOException JavaDoc, HsqlException;
120
121     protected abstract Boolean JavaDoc readBit() throws IOException JavaDoc, HsqlException;
122
123     protected abstract Time JavaDoc readTime() throws IOException JavaDoc, HsqlException;
124
125     protected abstract Date JavaDoc readDate() throws IOException JavaDoc, HsqlException;
126
127     protected abstract Timestamp JavaDoc readTimestamp()
128     throws IOException JavaDoc, HsqlException;
129
130     protected abstract Object JavaDoc readOther() throws IOException JavaDoc, HsqlException;
131
132     protected abstract Binary readBinary(int type)
133     throws IOException JavaDoc, HsqlException;
134
135     /**
136      * reads row data from a stream using the JDBC types in colTypes
137      *
138      * @param colTypes
139      * @return
140      * @throws IOException
141      * @throws HsqlException
142      */

143     public Object JavaDoc[] readData(int[] colTypes)
144     throws IOException JavaDoc, HsqlException {
145
146         int l = colTypes.length;
147         Object JavaDoc[] data = new Object JavaDoc[l];
148         Object JavaDoc o;
149         int type;
150
151         for (int i = 0; i < l; i++) {
152             if (checkNull()) {
153                 continue;
154             }
155
156             o = null;
157             type = colTypes[i];
158
159             switch (type) {
160
161                 case Types.NULL :
162                 case Types.CHAR :
163                 case Types.VARCHAR :
164                 case Types.VARCHAR_IGNORECASE :
165                 case Types.LONGVARCHAR :
166                     o = readChar(type);
167                     break;
168
169                 case Types.TINYINT :
170                 case Types.SMALLINT :
171                     o = readSmallint();
172                     break;
173
174                 case Types.INTEGER :
175                     o = readInteger();
176                     break;
177
178                 case Types.BIGINT :
179                     o = readBigint();
180                     break;
181
182                 //fredt although REAL is now Double, it is read / written in
183
//the old format for compatibility
184
case Types.REAL :
185                 case Types.FLOAT :
186                 case Types.DOUBLE :
187                     o = readReal(type);
188                     break;
189
190                 case Types.NUMERIC :
191                 case Types.DECIMAL :
192                     o = readDecimal();
193                     break;
194
195                 case Types.DATE :
196                     o = readDate();
197                     break;
198
199                 case Types.TIME :
200                     o = readTime();
201                     break;
202
203                 case Types.TIMESTAMP :
204                     o = readTimestamp();
205                     break;
206
207                 case Types.BOOLEAN :
208                     o = readBit();
209                     break;
210
211                 case Types.OTHER :
212                     o = readOther();
213                     break;
214
215                 case Types.BINARY :
216                 case Types.VARBINARY :
217                 case Types.LONGVARBINARY :
218                     o = readBinary(type);
219                     break;
220
221                 default :
222                     throw Trace.runtimeError(
223                         Trace.UNSUPPORTED_INTERNAL_OPERATION,
224                         "RowInputBase " + Types.getTypeString(type));
225             }
226
227             data[i] = o;
228         }
229
230         return data;
231     }
232
233     /**
234      * Used to reset the row, ready for a new row to be written into the
235      * byte[] buffer by an external routine.
236      *
237      */

238     public void resetRow(int filepos, int rowsize) throws IOException JavaDoc {
239
240         mark = 0;
241
242         reset();
243
244         if (buf.length < rowsize) {
245             buf = new byte[rowsize];
246         }
247
248         filePos = filepos;
249         size = count = rowsize;
250         pos = 4;
251         buf[0] = (byte) ((rowsize >>> 24) & 0xFF);
252         buf[1] = (byte) ((rowsize >>> 16) & 0xFF);
253         buf[2] = (byte) ((rowsize >>> 8) & 0xFF);
254         buf[3] = (byte) ((rowsize >>> 0) & 0xFF);
255     }
256
257     public byte[] getBuffer() {
258         return buf;
259     }
260
261     public int skipBytes(int n) throws IOException JavaDoc {
262         throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
263                                  "RowInputBase");
264     }
265
266     public String JavaDoc readLine() throws IOException JavaDoc {
267         throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION,
268                                  "RowInputBase");
269     }
270 }
271
Popular Tags