KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
37 import java.sql.Date JavaDoc;
38 import java.sql.Time JavaDoc;
39 import java.sql.Timestamp JavaDoc;
40
41 import org.hsqldb.HsqlDateTime;
42 import org.hsqldb.HsqlException;
43 import org.hsqldb.lib.StringConverter;
44 import org.hsqldb.store.ValuePool;
45 import org.hsqldb.types.Binary;
46 import org.hsqldb.types.JavaObject;
47
48 /**
49  * Provides methods for reading the data for a row from a
50  * byte array. The format of data is that used for storage of cached
51  * tables by v.1.6.x databases, apart from strings.
52  *
53  * @author sqlbob@users (RMP)
54  * @author fredt@users
55  * @version 1.7.2
56  * @since 1.7.0
57  */

58 public class RowInputBinary extends RowInputBase
59 implements org.hsqldb.rowio.RowInputInterface {
60
61     private RowOutputBinary out;
62
63     public RowInputBinary() {
64         super();
65     }
66
67     public RowInputBinary(byte[] buf) {
68         super(buf);
69     }
70
71     /**
72      * uses the byte[] buffer from out. At each reset, the buffer is set
73      * to the current one for out.
74      */

75     public RowInputBinary(RowOutputBinary out) {
76
77         super(out.getBuffer());
78
79         this.out = out;
80     }
81
82     protected byte[] readByteArray() throws IOException JavaDoc {
83
84         byte[] b = new byte[readInt()];
85
86         readFully(b);
87
88         return b;
89     }
90
91     public int readType() throws IOException JavaDoc {
92         return readShort();
93     }
94
95     public short readShortData() throws IOException JavaDoc {
96         return readShort();
97     }
98
99     public int readIntData() throws IOException JavaDoc {
100         return readInt();
101     }
102
103     public long readLongData() throws IOException JavaDoc {
104         return readLong();
105     }
106
107     public String JavaDoc readString() throws IOException JavaDoc {
108
109         int length = readInt();
110         String JavaDoc s = StringConverter.readUTF(buf, pos, length);
111
112         s = ValuePool.getString(s);
113         pos += length;
114
115         return s;
116     }
117
118     protected boolean checkNull() throws IOException JavaDoc {
119
120         int b = readByte();
121
122         return b == 0 ? true
123                       : false;
124     }
125
126     protected String JavaDoc readChar(int type) throws IOException JavaDoc {
127         return readString();
128     }
129
130     protected Integer JavaDoc readSmallint() throws IOException JavaDoc, HsqlException {
131         return ValuePool.getInt(readShort());
132     }
133
134     protected Integer JavaDoc readInteger() throws IOException JavaDoc, HsqlException {
135         return ValuePool.getInt(readInt());
136     }
137
138     protected Long JavaDoc readBigint() throws IOException JavaDoc, HsqlException {
139         return ValuePool.getLong(readLong());
140     }
141
142     protected Double JavaDoc readReal(int type) throws IOException JavaDoc, HsqlException {
143         return ValuePool.getDouble(readLong());
144     }
145
146     protected BigDecimal JavaDoc readDecimal() throws IOException JavaDoc, HsqlException {
147
148         byte[] bytes = readByteArray();
149         int scale = readInt();
150         BigInteger JavaDoc bigint = new BigInteger JavaDoc(bytes);
151
152         return ValuePool.getBigDecimal(new BigDecimal JavaDoc(bigint, scale));
153     }
154
155     protected Boolean JavaDoc readBit() throws IOException JavaDoc, HsqlException {
156         return readBoolean() ? Boolean.TRUE
157                              : Boolean.FALSE;
158     }
159
160     protected Time JavaDoc readTime() throws IOException JavaDoc, HsqlException {
161         return new Time JavaDoc(HsqlDateTime.getNormalisedTime(readLong()));
162     }
163
164     protected Date JavaDoc readDate() throws IOException JavaDoc, HsqlException {
165
166         long date = HsqlDateTime.getNormalisedDate(readLong());
167
168         return ValuePool.getDate(date);
169     }
170
171     protected Timestamp JavaDoc readTimestamp() throws IOException JavaDoc, HsqlException {
172         return HsqlDateTime.timestampValue(readLong(), readInt());
173     }
174
175     protected Object JavaDoc readOther() throws IOException JavaDoc, HsqlException {
176         return new JavaObject(readByteArray());
177     }
178
179     protected Binary readBinary(int type) throws IOException JavaDoc, HsqlException {
180         return new Binary(readByteArray(), false);
181     }
182
183     /**
184      * Used to reset the row, ready for Result data to be written into the
185      * byte[] buffer by an external routine.
186      *
187      */

188     public void resetRow(int rowsize) {
189
190         if (out != null) {
191             out.reset(rowsize);
192
193             buf = out.getBuffer();
194         }
195
196         super.reset();
197     }
198
199     /**
200      * Used to reset the row, ready for a new db row to be written into the
201      * byte[] buffer by an external routine.
202      *
203      */

204     public void resetRow(int filepos, int rowsize) throws IOException JavaDoc {
205
206         if (out != null) {
207             out.reset(rowsize);
208
209             buf = out.getBuffer();
210         }
211
212         super.resetRow(filepos, rowsize);
213     }
214 }
215
Popular Tags