001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.rowio;
032:
033: import java.io.IOException;
034: import java.math.BigDecimal;
035: import java.math.BigInteger;
036: import java.sql.Date;
037: import java.sql.Time;
038: import java.sql.Timestamp;
039:
040: import org.hsqldb.HsqlDateTime;
041: import org.hsqldb.HsqlException;
042: import org.hsqldb.lib.StringConverter;
043: import org.hsqldb.store.ValuePool;
044: import org.hsqldb.types.Binary;
045: import org.hsqldb.types.JavaObject;
046:
047: /**
048: * Provides methods for reading the data for a row from a
049: * byte array. The format of data is that used for storage of cached
050: * tables by v.1.6.x databases, apart from strings.
051: *
052: * @author sqlbob@users (RMP)
053: * @author fredt@users
054: * @version 1.7.2
055: * @since 1.7.0
056: */
057: public class RowInputBinary extends RowInputBase implements
058: org.hsqldb.rowio.RowInputInterface {
059:
060: private RowOutputBinary out;
061:
062: public RowInputBinary() {
063: super ();
064: }
065:
066: public RowInputBinary(byte[] buf) {
067: super (buf);
068: }
069:
070: /**
071: * uses the byte[] buffer from out. At each reset, the buffer is set
072: * to the current one for out.
073: */
074: public RowInputBinary(RowOutputBinary out) {
075:
076: super (out.getBuffer());
077:
078: this .out = out;
079: }
080:
081: protected byte[] readByteArray() throws IOException {
082:
083: byte[] b = new byte[readInt()];
084:
085: readFully(b);
086:
087: return b;
088: }
089:
090: public int readType() throws IOException {
091: return readShort();
092: }
093:
094: public short readShortData() throws IOException {
095: return readShort();
096: }
097:
098: public int readIntData() throws IOException {
099: return readInt();
100: }
101:
102: public long readLongData() throws IOException {
103: return readLong();
104: }
105:
106: public String readString() throws IOException {
107:
108: int length = readInt();
109: String s = StringConverter.readUTF(buf, pos, length);
110:
111: s = ValuePool.getString(s);
112: pos += length;
113:
114: return s;
115: }
116:
117: protected boolean checkNull() throws IOException {
118:
119: int b = readByte();
120:
121: return b == 0 ? true : false;
122: }
123:
124: protected String readChar(int type) throws IOException {
125: return readString();
126: }
127:
128: protected Integer readSmallint() throws IOException, HsqlException {
129: return ValuePool.getInt(readShort());
130: }
131:
132: protected Integer readInteger() throws IOException, HsqlException {
133: return ValuePool.getInt(readInt());
134: }
135:
136: protected Long readBigint() throws IOException, HsqlException {
137: return ValuePool.getLong(readLong());
138: }
139:
140: protected Double readReal(int type) throws IOException,
141: HsqlException {
142: return ValuePool.getDouble(readLong());
143: }
144:
145: protected BigDecimal readDecimal() throws IOException,
146: HsqlException {
147:
148: byte[] bytes = readByteArray();
149: int scale = readInt();
150: BigInteger bigint = new BigInteger(bytes);
151:
152: return ValuePool.getBigDecimal(new BigDecimal(bigint, scale));
153: }
154:
155: protected Boolean readBit() throws IOException, HsqlException {
156: return readBoolean() ? Boolean.TRUE : Boolean.FALSE;
157: }
158:
159: protected Time readTime() throws IOException, HsqlException {
160: return new Time(HsqlDateTime.getNormalisedTime(readLong()));
161: }
162:
163: protected Date readDate() throws IOException, HsqlException {
164:
165: long date = HsqlDateTime.getNormalisedDate(readLong());
166:
167: return ValuePool.getDate(date);
168: }
169:
170: protected Timestamp readTimestamp() throws IOException,
171: HsqlException {
172: return HsqlDateTime.timestampValue(readLong(), readInt());
173: }
174:
175: protected Object readOther() throws IOException, HsqlException {
176: return new JavaObject(readByteArray());
177: }
178:
179: protected Binary readBinary(int type) throws IOException,
180: HsqlException {
181: return new Binary(readByteArray(), false);
182: }
183:
184: /**
185: * Used to reset the row, ready for Result data to be written into the
186: * byte[] buffer by an external routine.
187: *
188: */
189: public void resetRow(int rowsize) {
190:
191: if (out != null) {
192: out.reset(rowsize);
193:
194: buf = out.getBuffer();
195: }
196:
197: super .reset();
198: }
199:
200: /**
201: * Used to reset the row, ready for a new db row to be written into the
202: * byte[] buffer by an external routine.
203: *
204: */
205: public void resetRow(int filepos, int rowsize) throws IOException {
206:
207: if (out != null) {
208: out.reset(rowsize);
209:
210: buf = out.getBuffer();
211: }
212:
213: super.resetRow(filepos, rowsize);
214: }
215: }
|