1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.FormatableHashtable |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import org.apache.derby.iapi.services.io.ArrayInputStream; |
24 | |
25 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
26 | import org.apache.derby.iapi.services.io.FormatIdUtil; |
27 | import org.apache.derby.iapi.services.io.Formatable; |
28 | import org.apache.derby.iapi.services.sanity.SanityManager; |
29 | |
30 | import java.util.Hashtable; |
31 | import java.util.Enumeration; |
32 | import java.io.ObjectOutput; |
33 | import java.io.ObjectInput; |
34 | import java.io.IOException; |
35 | |
36 | |
37 | /** |
38 | * A formatable holder for a java.util.Hashtable. |
39 | * Used to avoid serializing Properties. |
40 | */ |
41 | public class FormatableHashtable extends Hashtable implements Formatable |
42 | { |
43 | /******************************************************** |
44 | ** |
45 | ** This class implements Formatable. That means that it |
46 | ** can write itself to and from a formatted stream. If |
47 | ** you add more fields to this class, make sure that you |
48 | ** also write/read them with the writeExternal()/readExternal() |
49 | ** methods. |
50 | ** |
51 | ** If, inbetween releases, you add more fields to this class, |
52 | ** then you should bump the version number emitted by the getTypeFormatId() |
53 | ** method. |
54 | ** |
55 | ********************************************************/ |
56 | |
57 | /** |
58 | * Niladic constructor for formatable |
59 | */ |
60 | public FormatableHashtable() |
61 | { |
62 | } |
63 | |
64 | |
65 | /** |
66 | * Our special put method that wont barf |
67 | * on a null value. |
68 | * @see java.util.Hashtable |
69 | */ |
70 | public Object put(Object key, Object value) |
71 | { |
72 | if (value == null) |
73 | { |
74 | return remove(key); |
75 | } |
76 | |
77 | if (SanityManager.DEBUG) { |
78 | |
79 | if ((value instanceof FormatableIntHolder) || |
80 | (value instanceof FormatableLongHolder) || |
81 | ((value instanceof java.io.Serializable) && (!(value instanceof Formatable)) && (!(value instanceof String))) |
82 | ) { |
83 | |
84 | if (!value.getClass().isArray()) { |
85 | |
86 | // System.out.println("key " + key + " class " + value.getClass()); |
87 | //new Throwable().printStackTrace(System.out); |
88 | //System.exit(1); |
89 | } |
90 | } |
91 | } |
92 | return super.put(key, value); |
93 | } |
94 | |
95 | public void putInt(Object key, int value) { |
96 | |
97 | super.put(key, new FormatableIntHolder(value)); |
98 | } |
99 | |
100 | public int getInt(Object key) { |
101 | |
102 | return ((FormatableIntHolder) get(key)).getInt(); |
103 | } |
104 | public void putLong(Object key, long value) { |
105 | |
106 | super.put(key, new FormatableLongHolder(value)); |
107 | } |
108 | |
109 | public long getLong(Object key) { |
110 | |
111 | return ((FormatableLongHolder) get(key)).getLong(); |
112 | } |
113 | public void putBoolean(Object key, boolean value) { |
114 | |
115 | putInt(key,value ? 1 : 0); |
116 | } |
117 | |
118 | public boolean getBoolean(Object key) { |
119 | |
120 | return getInt(key) == 0 ? false : true; |
121 | |
122 | } |
123 | |
124 | ////////////////////////////////////////////// |
125 | // |
126 | // FORMATABLE |
127 | // |
128 | ////////////////////////////////////////////// |
129 | /** |
130 | * Write the hash table out. Step through |
131 | * the enumeration and write the strings out |
132 | * in UTF. |
133 | * |
134 | * @param out write bytes here |
135 | * |
136 | * @exception IOException thrown on error |
137 | */ |
138 | public void writeExternal(ObjectOutput out) throws IOException |
139 | { |
140 | out.writeInt(size()); |
141 | for (Enumeration e = keys(); e.hasMoreElements(); ) |
142 | { |
143 | Object key = e.nextElement(); |
144 | out.writeObject(key); |
145 | out.writeObject(get(key)); |
146 | |
147 | if (SanityManager.DEBUG) |
148 | { |
149 | Object value = get(key); |
150 | if (value instanceof Formatable[]) |
151 | { |
152 | SanityManager.THROWASSERT("you should be using FormatableArrayHolder rather than writing out an array of Formatables, otherwise you will get bad behavior for null Storables. Your class is a "+value.getClass().getName()); |
153 | } |
154 | } |
155 | } |
156 | } |
157 | |
158 | /** |
159 | * Read the hash table from a stream of stored objects. |
160 | * |
161 | * @param in read this. |
162 | * |
163 | * @exception IOException thrown on error |
164 | * @exception ClassNotFoundException thrown on error |
165 | */ |
166 | public void readExternal(ObjectInput in) |
167 | throws IOException, ClassNotFoundException |
168 | { |
169 | int size = in.readInt(); |
170 | for (; size > 0; size--) |
171 | { |
172 | super.put(in.readObject(), in.readObject()); |
173 | } |
174 | } |
175 | public void readExternal(ArrayInputStream in) |
176 | throws IOException, ClassNotFoundException |
177 | { |
178 | int size = in.readInt(); |
179 | for (; size > 0; size--) |
180 | { |
181 | super.put(in.readObject(), in.readObject()); |
182 | } |
183 | } |
184 | |
185 | /** |
186 | * Get the formatID which corresponds to this class. |
187 | * |
188 | * @return the formatID of this class |
189 | */ |
190 | public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_HASHTABLE_V01_ID; } |
191 | } |