1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.FormatableProperties |
4 | |
5 | Copyright 1998, 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.FormatIdUtil; |
26 | import org.apache.derby.iapi.services.io.Formatable; |
27 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
28 | |
29 | import java.util.Enumeration; |
30 | import java.util.Properties; |
31 | |
32 | import java.io.IOException; |
33 | import java.io.ObjectOutput; |
34 | import java.io.ObjectInput; |
35 | |
36 | /** |
37 | * A formatable holder for a java.util.Properties. |
38 | * Used to avoid serializing Properties. |
39 | */ |
40 | public class FormatableProperties extends Properties implements Formatable |
41 | { |
42 | /******************************************************** |
43 | ** |
44 | ** This class implements Formatable. That means that it |
45 | ** can write itself to and from a formatted stream. If |
46 | ** you add more fields to this class, make sure that you |
47 | ** also write/read them with the writeExternal()/readExternal() |
48 | ** methods. |
49 | ** |
50 | ** If, inbetween releases, you add more fields to this class, |
51 | ** then you should bump the version number emitted by the getTypeFormatId() |
52 | ** method. |
53 | ** |
54 | ********************************************************/ |
55 | |
56 | /** |
57 | * Niladic constructor for formatable |
58 | */ |
59 | public FormatableProperties() |
60 | { |
61 | this(null); |
62 | } |
63 | |
64 | /** |
65 | * Creates an empty property list with the specified |
66 | * defaults. |
67 | * |
68 | * @param defaults the defaults |
69 | */ |
70 | public FormatableProperties(Properties defaults) |
71 | { |
72 | super(defaults); |
73 | } |
74 | |
75 | /** |
76 | Clear the defaults from this Properties set. |
77 | This sets the default field to null and thus |
78 | breaks any link with the Properties set that |
79 | was the default. |
80 | */ |
81 | public void clearDefaults() { |
82 | defaults = null; |
83 | } |
84 | |
85 | ////////////////////////////////////////////// |
86 | // |
87 | // FORMATABLE |
88 | // |
89 | ////////////////////////////////////////////// |
90 | /** |
91 | * Write the properties out. Step through |
92 | * the enumeration and write the strings out |
93 | * in UTF. |
94 | * |
95 | * @param out write bytes here |
96 | * |
97 | * @exception IOException thrown on error |
98 | */ |
99 | public void writeExternal(ObjectOutput out) throws IOException |
100 | { |
101 | out.writeInt(size()); |
102 | for (Enumeration e = keys(); e.hasMoreElements(); ) |
103 | { |
104 | String key = (String)e.nextElement(); |
105 | out.writeUTF(key); |
106 | out.writeUTF(getProperty(key)); |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * Read the properties from a stream of stored objects. |
112 | * |
113 | * @param in read this. |
114 | * |
115 | * @exception IOException thrown on error |
116 | */ |
117 | public void readExternal(ObjectInput in) |
118 | throws IOException |
119 | { |
120 | int size = in.readInt(); |
121 | for (; size > 0; size--) |
122 | { |
123 | put(in.readUTF(), in.readUTF()); |
124 | } |
125 | } |
126 | |
127 | public void readExternal(ArrayInputStream in) |
128 | throws IOException |
129 | { |
130 | int size = in.readInt(); |
131 | for (; size > 0; size--) |
132 | { |
133 | put(in.readUTF(), in.readUTF()); |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * Get the formatID which corresponds to this class. |
139 | * |
140 | * @return the formatID of this class |
141 | */ |
142 | public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_PROPERTIES_V01_ID; } |
143 | } |