KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > global > ObjectTransfer


1 /**
2  * com.mckoi.database.global.ObjectTransfer 20 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.global;
26
27 import java.io.*;
28 import java.util.Date JavaDoc;
29 import com.mckoi.util.BigNumber;
30 import java.math.BigInteger JavaDoc;
31
32 /**
33  * Provides static methods for transfering different types of objects over
34  * a Data input/output stream.
35  *
36  * @author Tobias Downer
37  */

38
39 public class ObjectTransfer {
40
41   /**
42    * Makes an estimate of the size of the object. This is useful for making
43    * a guess for how much this will take up.
44    */

45   public static int size(Object JavaDoc 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 JavaDoc) {
56       return 8 + 9;
57     }
58     else if (ob instanceof Boolean JavaDoc) {
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   /**
73    * Returns the exact size an object will take up when serialized.
74    */

75   public static int exactSize(Object JavaDoc 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 JavaDoc) {
94       return 8 + 1;
95     }
96     else if (ob instanceof Boolean JavaDoc) {
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   /**
111    * Writes an object to the data output stream.
112    */

113   public static void writeTo(DataOutput out, Object JavaDoc ob) throws IOException {
114     if (ob == null) {
115       out.writeByte(1);
116     }
117     else if (ob instanceof StringObject) {
118       String JavaDoc str = ob.toString();
119
120       // All strings send as char array,
121
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 // out.writeByte(6);
146
// // NOTE: This method is only available in 1.2. This needs to be
147
// // compatible with 1.1 so we use a slower method,
148
//// BigInteger unscaled_val = n.unscaledValue();
149
// // NOTE: This can be swapped out eventually when we can guarentee
150
// // everything is 1.2 minimum.
151
// BigInteger unscaled_val = n.movePointRight(n.scale()).toBigInteger();
152
//
153
// byte[] buf = unscaled_val.toByteArray();
154
// out.writeInt(buf.length);
155
// out.write(buf);
156
}
157     else if (ob instanceof Date JavaDoc) {
158       Date JavaDoc d = (Date JavaDoc) ob;
159       out.writeByte(9);
160       out.writeLong(d.getTime());
161     }
162     else if (ob instanceof Boolean JavaDoc) {
163       Boolean JavaDoc b = (Boolean JavaDoc) 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   /**
187    * Writes an object from the data input stream.
188    */

189   public static Object JavaDoc 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 JavaDoc 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         // 64-bit long numeric value
219
long val = in.readLong();
220         return BigNumber.fromLong(val);
221       }
222
223       case(9):
224         long time = in.readLong();
225         return new Date JavaDoc(time);
226
227       case(12):
228         return new Boolean JavaDoc(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         // Handles strings > 64k
246
int len = in.readInt();
247         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(len);
248         while (len > 0) {
249           buf.append(in.readChar());
250           --len;
251         }
252         return StringObject.fromString(new String JavaDoc(buf));
253       }
254
255       case(24): {
256         // 32-bit int numeric value
257
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