src/share/classes/java/util/Properties.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.IOException;
  29 import java.io.PrintStream;
  30 import java.io.PrintWriter;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.Reader;
  34 import java.io.Writer;
  35 import java.io.OutputStreamWriter;
  36 import java.io.BufferedWriter;

  37 
  38 /**
  39  * The <code>Properties</code> class represents a persistent set of
  40  * properties. The <code>Properties</code> can be saved to a stream
  41  * or loaded from a stream. Each key and its corresponding value in
  42  * the property list is a string.
  43  * <p>
  44  * A property list can contain another property list as its
  45  * "defaults"; this second property list is searched if
  46  * the property key is not found in the original property list.
  47  * <p>
  48  * Because <code>Properties</code> inherits from <code>Hashtable</code>, the
  49  * <code>put</code> and <code>putAll</code> methods can be applied to a
  50  * <code>Properties</code> object.  Their use is strongly discouraged as they
  51  * allow the caller to insert entries whose keys or values are not
  52  * <code>Strings</code>.  The <code>setProperty</code> method should be used
  53  * instead.  If the <code>store</code> or <code>save</code> method is called
  54  * on a "compromised" <code>Properties</code> object that contains a
  55  * non-<code>String</code> key or value, the call will fail. Similarly,
  56  * the call to the <code>propertyNames</code> or <code>list</code> method


1094             Object k = e.nextElement();
1095             Object v = get(k);
1096             if (k instanceof String && v instanceof String) {
1097                 h.put((String) k, (String) v);
1098             }
1099         }
1100     }
1101 
1102     /**
1103      * Convert a nibble to a hex character
1104      * @param   nibble  the nibble to convert.
1105      */
1106     private static char toHex(int nibble) {
1107         return hexDigit[(nibble & 0xF)];
1108     }
1109 
1110     /** A table of hex digits */
1111     private static final char[] hexDigit = {
1112         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
1113     };

























































1114 }


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.IOException;
  29 import java.io.PrintStream;
  30 import java.io.PrintWriter;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.Reader;
  34 import java.io.Writer;
  35 import java.io.OutputStreamWriter;
  36 import java.io.BufferedWriter;
  37 import java.lang.reflect.*;
  38 
  39 /**
  40  * The <code>Properties</code> class represents a persistent set of
  41  * properties. The <code>Properties</code> can be saved to a stream
  42  * or loaded from a stream. Each key and its corresponding value in
  43  * the property list is a string.
  44  * <p>
  45  * A property list can contain another property list as its
  46  * "defaults"; this second property list is searched if
  47  * the property key is not found in the original property list.
  48  * <p>
  49  * Because <code>Properties</code> inherits from <code>Hashtable</code>, the
  50  * <code>put</code> and <code>putAll</code> methods can be applied to a
  51  * <code>Properties</code> object.  Their use is strongly discouraged as they
  52  * allow the caller to insert entries whose keys or values are not
  53  * <code>Strings</code>.  The <code>setProperty</code> method should be used
  54  * instead.  If the <code>store</code> or <code>save</code> method is called
  55  * on a "compromised" <code>Properties</code> object that contains a
  56  * non-<code>String</code> key or value, the call will fail. Similarly,
  57  * the call to the <code>propertyNames</code> or <code>list</code> method


1095             Object k = e.nextElement();
1096             Object v = get(k);
1097             if (k instanceof String && v instanceof String) {
1098                 h.put((String) k, (String) v);
1099             }
1100         }
1101     }
1102 
1103     /**
1104      * Convert a nibble to a hex character
1105      * @param   nibble  the nibble to convert.
1106      */
1107     private static char toHex(int nibble) {
1108         return hexDigit[(nibble & 0xF)];
1109     }
1110 
1111     /** A table of hex digits */
1112     private static final char[] hexDigit = {
1113         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
1114     };
1115 
1116 
1117     static class XMLUtils {
1118         private static Method load = null;
1119         private static Method save = null;
1120         static {
1121             try {
1122                 // reference sun.util.xml.Utils reflectively
1123                 // to allow the Properties class be compiled in
1124                 // the absence of XML
1125                 Class<?> c = Class.forName("sun.util.xml.XMLUtils", true, null);
1126                 load = c.getMethod("load", Properties.class, InputStream.class);
1127                 save = c.getMethod("save", Properties.class, OutputStream.class,
1128                                    String.class, String.class);
1129             } catch (ClassNotFoundException cnf) {
1130             } catch (NoSuchMethodException e) {
1131                 throw new AssertionError(e);
1132             }
1133         }
1134 
1135         static void invoke(Method m, Object... args) throws IOException {
1136             try {
1137                 m.invoke(null, args);
1138             } catch (IllegalAccessException e) {
1139                 throw new AssertionError(e);
1140             } catch (InvocationTargetException e) {
1141                 Throwable t = e.getCause(); 
1142                 if (t instanceof RuntimeException)
1143                     throw (RuntimeException)t;
1144 
1145                 if (t instanceof IOException) {
1146                     throw (IOException)t;
1147                 } else {
1148                     throw new AssertionError(t);
1149                 }
1150             }
1151         }
1152 
1153         static void load(Properties props, InputStream in)
1154             throws IOException, InvalidPropertiesFormatException
1155         {
1156             if (load == null)
1157                 throw new InternalError("sun.util.xml.XMLUtils not found");
1158     
1159             invoke(load, props, in);
1160         }
1161     
1162         static void save(Properties props, OutputStream os, String comment,
1163                          String encoding)
1164             throws IOException
1165         {
1166             if (save == null)
1167                 throw new InternalError("sun.util.xml.XMLUtils not found");
1168     
1169             invoke(save, props, os, comment, encoding);
1170         }
1171     }
1172 }