KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FormatableProperties


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FormatableProperties
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.iapi.services.io.ArrayInputStream;
25
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.io.StoredFormatIds;
29
30 import java.util.Enumeration JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectOutput JavaDoc;
35 import java.io.ObjectInput JavaDoc;
36
37 /**
38  * A formatable holder for a java.util.Properties.
39  * Used to avoid serializing Properties.
40  */

41 public class FormatableProperties extends Properties JavaDoc 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 FormatableProperties()
61     {
62         this(null);
63     }
64
65     /**
66      * Creates an empty property list with the specified
67      * defaults.
68      *
69      * @param defaults the defaults
70      */

71     public FormatableProperties(Properties JavaDoc defaults)
72     {
73         super(defaults);
74     }
75
76     /**
77         Clear the defaults from this Properties set.
78         This sets the default field to null and thus
79         breaks any link with the Properties set that
80         was the default.
81     */

82     public void clearDefaults() {
83         defaults = null;
84     }
85     
86     //////////////////////////////////////////////
87
//
88
// FORMATABLE
89
//
90
//////////////////////////////////////////////
91
/**
92      * Write the properties out. Step through
93      * the enumeration and write the strings out
94      * in UTF.
95      *
96      * @param out write bytes here
97      *
98      * @exception IOException thrown on error
99      */

100     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
101     {
102         out.writeInt(size());
103         for (Enumeration JavaDoc e = keys(); e.hasMoreElements(); )
104         {
105             String JavaDoc key = (String JavaDoc)e.nextElement();
106             out.writeUTF(key);
107             out.writeUTF(getProperty(key));
108         }
109     }
110
111     /**
112      * Read the properties from a stream of stored objects.
113      *
114      * @param in read this.
115      *
116      * @exception IOException thrown on error
117      */

118     public void readExternal(ObjectInput JavaDoc in)
119         throws IOException JavaDoc
120     {
121         int size = in.readInt();
122         for (; size > 0; size--)
123         {
124             put(in.readUTF(), in.readUTF());
125         }
126     }
127
128     public void readExternal(ArrayInputStream in)
129         throws IOException JavaDoc
130     {
131         int size = in.readInt();
132         for (; size > 0; size--)
133         {
134             put(in.readUTF(), in.readUTF());
135         }
136     }
137     
138     /**
139      * Get the formatID which corresponds to this class.
140      *
141      * @return the formatID of this class
142      */

143     public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_PROPERTIES_V01_ID; }
144 }
145
Popular Tags