1 30 31 32 package org.hsqldb.rowio; 33 34 import java.math.BigDecimal ; 35 import java.math.BigInteger ; 36 import java.sql.Date ; 37 import java.sql.Time ; 38 import java.sql.Timestamp ; 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 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 76 public RowOutputBinary(byte[] buffer) { 77 super(buffer); 78 } 79 80 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 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 s) { 129 130 int temp = count; 131 132 writeInt(0); 133 StringConverter.writeUTF(s, this); 134 writeIntData(count - temp - 4, temp); 135 } 136 137 144 public int getSize(CachedRow row) { 145 146 Object [] 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 [] 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 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 s, int t) { 172 writeString(s); 173 } 174 175 protected void writeSmallint(Number o) { 176 writeShort(o.intValue()); 177 } 178 179 protected void writeInteger(Number o) { 180 writeInt(o.intValue()); 181 } 182 183 protected void writeBigint(Number o) { 184 writeLong(o.longValue()); 185 } 186 187 protected void writeReal(Double o, int type) { 188 writeLong(Double.doubleToLongBits((o.doubleValue()))); 189 } 190 191 protected void writeDecimal(BigDecimal o) { 192 193 int scale = o.scale(); 194 BigInteger bigint = JavaSystem.getUnscaledValue(o); 195 byte[] bytearr = bigint.toByteArray(); 196 197 writeByteArray(bytearr); 198 writeInt(scale); 199 } 200 201 protected void writeBit(Boolean o) { 202 write(o.booleanValue() ? 1 203 : 0); 204 } 205 206 protected void writeDate(Date o) { 207 writeLong(o.getTime()); 208 } 209 210 protected void writeTime(Time o) { 211 writeLong(o.getTime()); 212 } 213 214 protected void writeTimestamp(Timestamp 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 protected void writeByteArray(byte[] b) { 229 writeInt(b.length); 230 write(b, 0, b.length); 231 } 232 233 242 private static int getSize(Object [] data, int l, int[] type) { 243 244 int s = 0; 245 246 for (int i = 0; i < l; i++) { 247 Object o = data[i]; 248 249 s += 1; 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 ) 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 bigdecimal = (BigDecimal ) o; 284 BigInteger 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 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 |