1 24 25 package com.mckoi.database.global; 26 27 import java.io.*; 28 import java.util.Date ; 29 import com.mckoi.util.BigNumber; 30 import java.math.BigInteger ; 31 32 38 39 public class ObjectTransfer { 40 41 45 public static int size(Object ob) throws IOException { 46 if (ob == null) { 47 return 9; 48 } 49 else if (ob instanceof StringObject) { 50 return (ob.toString().length() * 2) + 9; 51 } 52 else if (ob instanceof BigNumber) { 53 return 15 + 9; 54 } 55 else if (ob instanceof Date ) { 56 return 8 + 9; 57 } 58 else if (ob instanceof Boolean ) { 59 return 2 + 9; 60 } 61 else if (ob instanceof ByteLongObject) { 62 return ((ByteLongObject) ob).length() + 9; 63 } 64 else if (ob instanceof StreamableObject) { 65 return 5 + 9; 66 } 67 else { 68 throw new IOException("Unrecognised type: " + ob.getClass()); 69 } 70 } 71 72 75 public static int exactSize(Object ob) throws IOException { 76 if (ob == null) { 77 return 1; 78 } 79 else if (ob instanceof StringObject) { 80 return (ob.toString().length() * 2) + 1 + 4; 81 } 82 else if (ob instanceof BigNumber) { 83 BigNumber n = (BigNumber) ob; 84 if (n.canBeRepresentedAsInt()) { 85 return 4 + 1; 86 } 87 else if (n.canBeRepresentedAsLong()) { 88 return 8 + 1; 89 } 90 byte[] buf = n.toByteArray(); 91 return buf.length + 1 + 1 + 4 + 4; 92 } 93 else if (ob instanceof Date ) { 94 return 8 + 1; 95 } 96 else if (ob instanceof Boolean ) { 97 return 1 + 1; 98 } 99 else if (ob instanceof ByteLongObject) { 100 return ((ByteLongObject) ob).length() + 1 + 8; 101 } 102 else if (ob instanceof StreamableObject) { 103 return 1 + 1 + 4; 104 } 105 else { 106 throw new IOException("Unrecognised type: " + ob.getClass()); 107 } 108 } 109 110 113 public static void writeTo(DataOutput out, Object ob) throws IOException { 114 if (ob == null) { 115 out.writeByte(1); 116 } 117 else if (ob instanceof StringObject) { 118 String str = ob.toString(); 119 120 out.writeByte(18); 122 out.writeInt(str.length()); 123 out.writeChars(str); 124 125 } 126 else if (ob instanceof BigNumber) { 127 BigNumber n = (BigNumber) ob; 128 if (n.canBeRepresentedAsInt()) { 129 out.writeByte(24); 130 out.writeInt(n.intValue()); 131 } 132 else if (n.canBeRepresentedAsLong()) { 133 out.writeByte(8); 134 out.writeLong(n.longValue()); 135 } 136 else { 137 out.writeByte(7); 138 out.writeByte(n.getState()); 139 out.writeInt(n.getScale()); 140 byte[] buf = n.toByteArray(); 141 out.writeInt(buf.length); 142 out.write(buf); 143 } 144 145 } 157 else if (ob instanceof Date ) { 158 Date d = (Date ) ob; 159 out.writeByte(9); 160 out.writeLong(d.getTime()); 161 } 162 else if (ob instanceof Boolean ) { 163 Boolean b = (Boolean ) ob; 164 out.writeByte(12); 165 out.writeBoolean(b.booleanValue()); 166 } 167 else if (ob instanceof ByteLongObject) { 168 ByteLongObject barr = (ByteLongObject) ob; 169 out.writeByte(15); 170 byte[] arr = barr.getByteArray(); 171 out.writeLong(arr.length); 172 out.write(arr); 173 } 174 else if (ob instanceof StreamableObject) { 175 StreamableObject ob_head = (StreamableObject) ob; 176 out.writeByte(16); 177 out.writeByte(ob_head.getType()); 178 out.writeLong(ob_head.getSize()); 179 out.writeLong(ob_head.getIdentifier()); 180 } 181 else { 182 throw new IOException("Unrecognised type: " + ob.getClass()); 183 } 184 } 185 186 189 public static Object readFrom(DataInputStream in) throws IOException { 190 byte type = in.readByte(); 191 192 switch (type) { 193 case(1): 194 return null; 195 196 case(3): 197 String str = in.readUTF(); 198 return StringObject.fromString(str); 199 200 case(6): { 201 int scale = in.readInt(); 202 int blen = in.readInt(); 203 byte[] buf = new byte[blen]; 204 in.readFully(buf); 205 return BigNumber.fromData(buf, scale, (byte) 0); 206 } 207 208 case(7): { 209 byte state = in.readByte(); 210 int scale = in.readInt(); 211 int blen = in.readInt(); 212 byte[] buf = new byte[blen]; 213 in.readFully(buf); 214 return BigNumber.fromData(buf, scale, state); 215 } 216 217 case(8): { 218 long val = in.readLong(); 220 return BigNumber.fromLong(val); 221 } 222 223 case(9): 224 long time = in.readLong(); 225 return new Date (time); 226 227 case(12): 228 return new Boolean (in.readBoolean()); 229 230 case(15): { 231 long size = in.readLong(); 232 byte[] arr = new byte[(int) size]; 233 in.readFully(arr, 0, (int) size); 234 return new ByteLongObject(arr); 235 } 236 237 case(16): { 238 final byte h_type = in.readByte(); 239 final long h_size = in.readLong(); 240 final long h_id = in.readLong(); 241 return new StreamableObject(h_type, h_size, h_id); 242 } 243 244 case(18): { 245 int len = in.readInt(); 247 StringBuffer buf = new StringBuffer (len); 248 while (len > 0) { 249 buf.append(in.readChar()); 250 --len; 251 } 252 return StringObject.fromString(new String (buf)); 253 } 254 255 case(24): { 256 long val = (long) in.readInt(); 258 return BigNumber.fromLong(val); 259 } 260 261 default: 262 throw new IOException("Unrecognised type: " + type); 263 264 } 265 } 266 267 } 268 | Popular Tags |