KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > io > ExternProxyString


1 package com.quadcap.sql.io;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42 import java.io.ObjectInput JavaDoc;
43 import java.io.ObjectOutput JavaDoc;
44
45 import com.quadcap.util.Debug;
46
47 /**
48  * ExternalizeProxy for strings.
49  *
50  * @author Stan Bailes
51  */

52 public class ExternProxyString implements ExternalizeProxy {
53
54     //#ifdef SMALLDB
55
public Object JavaDoc readObject(ObjectInput JavaDoc in) throws IOException JavaDoc {
56     int cnt = in.readInt();
57     if (cnt == -1) return null;
58     char[] buf = new char[cnt];
59         for (int i = 0; i < cnt; i++) {
60             buf[i] = in.readChar();
61         }
62     return new String JavaDoc(buf);
63     }
64
65     public void writeObject(ObjectOutput JavaDoc out, Object JavaDoc object)
66     throws IOException JavaDoc
67     {
68     if (object == null) {
69         out.writeInt(-1);
70     } else {
71         String JavaDoc s = (String JavaDoc)object;
72         int cnt = s.length();
73         out.writeInt(cnt);
74         for (int i = 0; i < cnt; i++) {
75                 out.writeChar(s.charAt(i));
76         }
77     }
78     }
79     //#else
80
//- public Object readObject(ObjectInput in) throws IOException {
81
//- int cnt = in.readInt();
82
//- if (cnt == -1) return null;
83
//- char[] buf = new char[cnt];
84
//- byte[] b = new byte[cnt << 1];
85
//- in.readFully(b);
86
//- int k = 0;
87
//- for (int i = 0; i < cnt; i++) {
88
//- int s = (b[k++] & 0xff) << 8;
89
//- s |= (b[k++] & 0xff);
90
//- buf[i] = (char)s;
91
//- }
92
//- return new String(buf);
93
//- }
94
//-
95
//- public void writeObject(ObjectOutput out, Object object)
96
//- throws IOException
97
//- {
98
//- if (object == null) {
99
//- out.writeInt(-1);
100
//- } else {
101
//- String s = (String)object;
102
//- int cnt = s.length();
103
//- out.writeInt(cnt);
104
//- byte[] buf = new byte[cnt << 1];
105
//- int k = 0;
106
//- for (int i = 0; i < cnt; i++) {
107
//- int c = s.charAt(i);
108
//- buf[k++] = (byte)((c >> 8) & 0xff);
109
//- buf[k++] = (byte)(c & 0xff);
110
//- }
111
//- out.write(buf);
112
//- }
113
//- }
114
//#endif
115
}
116
Popular Tags