Preferences.java revision 96b40e87c32023084d7153360b1fa0c44c598095
1/* 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 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 26package java.util.prefs; 27 28import java.io.InputStream; 29import java.io.IOException; 30import java.io.OutputStream; 31import java.security.AccessController; 32import java.security.Permission; 33import java.security.PrivilegedAction; 34import java.util.Iterator; 35import java.util.ServiceLoader; 36import java.util.ServiceConfigurationError; 37 38// These imports needed only as a workaround for a JavaDoc bug 39import java.lang.RuntimePermission; 40import java.lang.Integer; 41import java.lang.Long; 42import java.lang.Float; 43import java.lang.Double; 44 45/** 46 * A node in a hierarchical collection of preference data. This class 47 * allows applications to store and retrieve user and system 48 * preference and configuration data. This data is stored 49 * persistently in an implementation-dependent backing store. Typical 50 * implementations include flat files, OS-specific registries, 51 * directory servers and SQL databases. The user of this class needn't 52 * be concerned with details of the backing store. 53 * 54 * <p>There are two separate trees of preference nodes, one for user 55 * preferences and one for system preferences. Each user has a separate user 56 * preference tree, and all users in a given system share the same system 57 * preference tree. The precise description of "user" and "system" will vary 58 * from implementation to implementation. Typical information stored in the 59 * user preference tree might include font choice, color choice, or preferred 60 * window location and size for a particular application. Typical information 61 * stored in the system preference tree might include installation 62 * configuration data for an application. 63 * 64 * <p>Nodes in a preference tree are named in a similar fashion to 65 * directories in a hierarchical file system. Every node in a preference 66 * tree has a <i>node name</i> (which is not necessarily unique), 67 * a unique <i>absolute path name</i>, and a path name <i>relative</i> to each 68 * ancestor including itself. 69 * 70 * <p>The root node has a node name of the empty string (""). Every other 71 * node has an arbitrary node name, specified at the time it is created. The 72 * only restrictions on this name are that it cannot be the empty string, and 73 * it cannot contain the slash character ('/'). 74 * 75 * <p>The root node has an absolute path name of <tt>"/"</tt>. Children of 76 * the root node have absolute path names of <tt>"/" + </tt><i><node 77 * name></i>. All other nodes have absolute path names of <i><parent's 78 * absolute path name></i><tt> + "/" + </tt><i><node name></i>. 79 * Note that all absolute path names begin with the slash character. 80 * 81 * <p>A node <i>n</i>'s path name relative to its ancestor <i>a</i> 82 * is simply the string that must be appended to <i>a</i>'s absolute path name 83 * in order to form <i>n</i>'s absolute path name, with the initial slash 84 * character (if present) removed. Note that: 85 * <ul> 86 * <li>No relative path names begin with the slash character. 87 * <li>Every node's path name relative to itself is the empty string. 88 * <li>Every node's path name relative to its parent is its node name (except 89 * for the root node, which does not have a parent). 90 * <li>Every node's path name relative to the root is its absolute path name 91 * with the initial slash character removed. 92 * </ul> 93 * 94 * <p>Note finally that: 95 * <ul> 96 * <li>No path name contains multiple consecutive slash characters. 97 * <li>No path name with the exception of the root's absolute path name 98 * ends in the slash character. 99 * <li>Any string that conforms to these two rules is a valid path name. 100 * </ul> 101 * 102 * <p>All of the methods that modify preferences data are permitted to operate 103 * asynchronously; they may return immediately, and changes will eventually 104 * propagate to the persistent backing store with an implementation-dependent 105 * delay. The <tt>flush</tt> method may be used to synchronously force 106 * updates to the backing store. Normal termination of the Java Virtual 107 * Machine will <i>not</i> result in the loss of pending updates -- an explicit 108 * <tt>flush</tt> invocation is <i>not</i> required upon termination to ensure 109 * that pending updates are made persistent. 110 * 111 * <p>All of the methods that read preferences from a <tt>Preferences</tt> 112 * object require the invoker to provide a default value. The default value is 113 * returned if no value has been previously set <i>or if the backing store is 114 * unavailable</i>. The intent is to allow applications to operate, albeit 115 * with slightly degraded functionality, even if the backing store becomes 116 * unavailable. Several methods, like <tt>flush</tt>, have semantics that 117 * prevent them from operating if the backing store is unavailable. Ordinary 118 * applications should have no need to invoke any of these methods, which can 119 * be identified by the fact that they are declared to throw {@link 120 * BackingStoreException}. 121 * 122 * <p>The methods in this class may be invoked concurrently by multiple threads 123 * in a single JVM without the need for external synchronization, and the 124 * results will be equivalent to some serial execution. If this class is used 125 * concurrently <i>by multiple JVMs</i> that store their preference data in 126 * the same backing store, the data store will not be corrupted, but no 127 * other guarantees are made concerning the consistency of the preference 128 * data. 129 * 130 * <p>This class contains an export/import facility, allowing preferences 131 * to be "exported" to an XML document, and XML documents representing 132 * preferences to be "imported" back into the system. This facility 133 * may be used to back up all or part of a preference tree, and 134 * subsequently restore from the backup. 135 * 136 * <p>The XML document has the following DOCTYPE declaration: 137 * <pre> 138 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 139 * </pre> 140 * Note that the system URI (http://java.sun.com/dtd/preferences.dtd) is 141 * <i>not</i> accessed when exporting or importing preferences; it merely 142 * serves as a string to uniquely identify the DTD, which is: 143 * <pre> 144 * <?xml version="1.0" encoding="UTF-8"?> 145 * 146 * <!-- DTD for a Preferences tree. --> 147 * 148 * <!-- The preferences element is at the root of an XML document 149 * representing a Preferences tree. --> 150 * <!ELEMENT preferences (root)> 151 * 152 * <!-- The preferences element contains an optional version attribute, 153 * which specifies version of DTD. --> 154 * <!ATTLIST preferences EXTERNAL_XML_VERSION CDATA "0.0" > 155 * 156 * <!-- The root element has a map representing the root's preferences 157 * (if any), and one node for each child of the root (if any). --> 158 * <!ELEMENT root (map, node*) > 159 * 160 * <!-- Additionally, the root contains a type attribute, which 161 * specifies whether it's the system or user root. --> 162 * <!ATTLIST root 163 * type (system|user) #REQUIRED > 164 * 165 * <!-- Each node has a map representing its preferences (if any), 166 * and one node for each child (if any). --> 167 * <!ELEMENT node (map, node*) > 168 * 169 * <!-- Additionally, each node has a name attribute --> 170 * <!ATTLIST node 171 * name CDATA #REQUIRED > 172 * 173 * <!-- A map represents the preferences stored at a node (if any). --> 174 * <!ELEMENT map (entry*) > 175 * 176 * <!-- An entry represents a single preference, which is simply 177 * a key-value pair. --> 178 * <!ELEMENT entry EMPTY > 179 * <!ATTLIST entry 180 * key CDATA #REQUIRED 181 * value CDATA #REQUIRED > 182 * </pre> 183 * 184 * Every <tt>Preferences</tt> implementation must have an associated {@link 185 * PreferencesFactory} implementation. Every Java(TM) SE implementation must provide 186 * some means of specifying which <tt>PreferencesFactory</tt> implementation 187 * is used to generate the root preferences nodes. This allows the 188 * administrator to replace the default preferences implementation with an 189 * alternative implementation. 190 * 191 * <p>Implementation note: In Sun's JRE, the <tt>PreferencesFactory</tt> 192 * implementation is located as follows: 193 * 194 * <ol> 195 * 196 * <li><p>If the system property 197 * <tt>java.util.prefs.PreferencesFactory</tt> is defined, then it is 198 * taken to be the fully-qualified name of a class implementing the 199 * <tt>PreferencesFactory</tt> interface. The class is loaded and 200 * instantiated; if this process fails then an unspecified error is 201 * thrown.</p></li> 202 * 203 * <li><p> If a <tt>PreferencesFactory</tt> implementation class file 204 * has been installed in a jar file that is visible to the 205 * {@link java.lang.ClassLoader#getSystemClassLoader system class loader}, 206 * and that jar file contains a provider-configuration file named 207 * <tt>java.util.prefs.PreferencesFactory</tt> in the resource 208 * directory <tt>META-INF/services</tt>, then the first class name 209 * specified in that file is taken. If more than one such jar file is 210 * provided, the first one found will be used. The class is loaded 211 * and instantiated; if this process fails then an unspecified error 212 * is thrown. </p></li> 213 * 214 * <li><p>Finally, if neither the above-mentioned system property nor 215 * an extension jar file is provided, then the system-wide default 216 * <tt>PreferencesFactory</tt> implementation for the underlying 217 * platform is loaded and instantiated.</p></li> 218 * 219 * </ol> 220 * 221 * @author Josh Bloch 222 * @since 1.4 223 */ 224public abstract class Preferences { 225 226 // Android-changed: Not final for testing. 227 private static PreferencesFactory factory = findPreferencesFactory(); 228 229 // Android-changed: Custom implementation of findPreferencesFactory. 230 private static PreferencesFactory findPreferencesFactory() { 231 // Try the system property first... 232 PreferencesFactory result = ServiceLoader.loadFromSystemProperty(PreferencesFactory.class); 233 if (result != null) { 234 return result; 235 } 236 // Then use ServiceLoader for META-INF/services/... 237 for (PreferencesFactory impl : ServiceLoader.load(PreferencesFactory.class)) { 238 return impl; 239 } 240 // Finally return a default... 241 return new FileSystemPreferencesFactory(); 242 } 243 244 /** 245 * @hide for testing only. 246 */ 247 // Android-changed: Allow this to be set for testing. 248 public static PreferencesFactory setPreferencesFactory(PreferencesFactory pf) { 249 PreferencesFactory previous = factory; 250 factory = pf; 251 return previous; 252 } 253 254 /** 255 * Maximum length of string allowed as a key (80 characters). 256 */ 257 public static final int MAX_KEY_LENGTH = 80; 258 259 /** 260 * Maximum length of string allowed as a value (8192 characters). 261 */ 262 public static final int MAX_VALUE_LENGTH = 8*1024; 263 264 /** 265 * Maximum length of a node name (80 characters). 266 */ 267 public static final int MAX_NAME_LENGTH = 80; 268 269 /** 270 * Returns the preference node from the calling user's preference tree 271 * that is associated (by convention) with the specified class's package. 272 * The convention is as follows: the absolute path name of the node is the 273 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 274 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 275 * absolute path name of the node associated with the class 276 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 277 * 278 * <p>This convention does not apply to the unnamed package, whose 279 * associated preference node is <tt><unnamed></tt>. This node 280 * is not intended for long term use, but for convenience in the early 281 * development of programs that do not yet belong to a package, and 282 * for "throwaway" programs. <i>Valuable data should not be stored 283 * at this node as it is shared by all programs that use it.</i> 284 * 285 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 286 * package can obtain a preference node as follows: <pre> 287 * static Preferences prefs = Preferences.userNodeForPackage(Foo.class); 288 * </pre> 289 * This idiom obviates the need for using a string to describe the 290 * preferences node and decreases the likelihood of a run-time failure. 291 * (If the class name is misspelled, it will typically result in a 292 * compile-time error.) 293 * 294 * <p>Invoking this method will result in the creation of the returned 295 * node and its ancestors if they do not already exist. If the returned 296 * node did not exist prior to this call, this node and any ancestors that 297 * were created by this call are not guaranteed to become permanent until 298 * the <tt>flush</tt> method is called on the returned node (or one of its 299 * ancestors or descendants). 300 * 301 * @param c the class for whose package a user preference node is desired. 302 * @return the user preference node associated with the package of which 303 * <tt>c</tt> is a member. 304 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 305 * @throws SecurityException if a security manager is present and 306 * it denies <tt>RuntimePermission("preferences")</tt>. 307 * @see RuntimePermission 308 */ 309 public static Preferences userNodeForPackage(Class<?> c) { 310 return userRoot().node(nodeName(c)); 311 } 312 313 /** 314 * Returns the preference node from the system preference tree that is 315 * associated (by convention) with the specified class's package. The 316 * convention is as follows: the absolute path name of the node is the 317 * fully qualified package name, preceded by a slash (<tt>'/'</tt>), and 318 * with each period (<tt>'.'</tt>) replaced by a slash. For example the 319 * absolute path name of the node associated with the class 320 * <tt>com.acme.widget.Foo</tt> is <tt>/com/acme/widget</tt>. 321 * 322 * <p>This convention does not apply to the unnamed package, whose 323 * associated preference node is <tt><unnamed></tt>. This node 324 * is not intended for long term use, but for convenience in the early 325 * development of programs that do not yet belong to a package, and 326 * for "throwaway" programs. <i>Valuable data should not be stored 327 * at this node as it is shared by all programs that use it.</i> 328 * 329 * <p>A class <tt>Foo</tt> wishing to access preferences pertaining to its 330 * package can obtain a preference node as follows: <pre> 331 * static Preferences prefs = Preferences.systemNodeForPackage(Foo.class); 332 * </pre> 333 * This idiom obviates the need for using a string to describe the 334 * preferences node and decreases the likelihood of a run-time failure. 335 * (If the class name is misspelled, it will typically result in a 336 * compile-time error.) 337 * 338 * <p>Invoking this method will result in the creation of the returned 339 * node and its ancestors if they do not already exist. If the returned 340 * node did not exist prior to this call, this node and any ancestors that 341 * were created by this call are not guaranteed to become permanent until 342 * the <tt>flush</tt> method is called on the returned node (or one of its 343 * ancestors or descendants). 344 * 345 * @param c the class for whose package a system preference node is desired. 346 * @return the system preference node associated with the package of which 347 * <tt>c</tt> is a member. 348 * @throws NullPointerException if <tt>c</tt> is <tt>null</tt>. 349 * @throws SecurityException if a security manager is present and 350 * it denies <tt>RuntimePermission("preferences")</tt>. 351 * @see RuntimePermission 352 */ 353 public static Preferences systemNodeForPackage(Class<?> c) { 354 return systemRoot().node(nodeName(c)); 355 } 356 357 /** 358 * Returns the absolute path name of the node corresponding to the package 359 * of the specified object. 360 * 361 * @throws IllegalArgumentException if the package has node preferences 362 * node associated with it. 363 */ 364 private static String nodeName(Class c) { 365 if (c.isArray()) 366 throw new IllegalArgumentException( 367 "Arrays have no associated preferences node."); 368 String className = c.getName(); 369 int pkgEndIndex = className.lastIndexOf('.'); 370 if (pkgEndIndex < 0) 371 return "/<unnamed>"; 372 String packageName = className.substring(0, pkgEndIndex); 373 return "/" + packageName.replace('.', '/'); 374 } 375 376 /** 377 * This permission object represents the permission required to get 378 * access to the user or system root (which in turn allows for all 379 * other operations). 380 */ 381 private static Permission prefsPerm = new RuntimePermission("preferences"); 382 383 /** 384 * Returns the root preference node for the calling user. 385 * 386 * @return the root preference node for the calling user. 387 * @throws SecurityException If a security manager is present and 388 * it denies <tt>RuntimePermission("preferences")</tt>. 389 * @see RuntimePermission 390 */ 391 public static Preferences userRoot() { 392 SecurityManager security = System.getSecurityManager(); 393 if (security != null) 394 security.checkPermission(prefsPerm); 395 396 return factory.userRoot(); 397 } 398 399 /** 400 * Returns the root preference node for the system. 401 * 402 * @return the root preference node for the system. 403 * @throws SecurityException If a security manager is present and 404 * it denies <tt>RuntimePermission("preferences")</tt>. 405 * @see RuntimePermission 406 */ 407 public static Preferences systemRoot() { 408 SecurityManager security = System.getSecurityManager(); 409 if (security != null) 410 security.checkPermission(prefsPerm); 411 412 return factory.systemRoot(); 413 } 414 415 /** 416 * Sole constructor. (For invocation by subclass constructors, typically 417 * implicit.) 418 */ 419 protected Preferences() { 420 } 421 422 /** 423 * Associates the specified value with the specified key in this 424 * preference node. 425 * 426 * @param key key with which the specified value is to be associated. 427 * @param value value to be associated with the specified key. 428 * @throws NullPointerException if key or value is <tt>null</tt>. 429 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 430 * <tt>MAX_KEY_LENGTH</tt> or if <tt>value.length</tt> exceeds 431 * <tt>MAX_VALUE_LENGTH</tt>. 432 * @throws IllegalStateException if this node (or an ancestor) has been 433 * removed with the {@link #removeNode()} method. 434 */ 435 public abstract void put(String key, String value); 436 437 /** 438 * Returns the value associated with the specified key in this preference 439 * node. Returns the specified default if there is no value associated 440 * with the key, or the backing store is inaccessible. 441 * 442 * <p>Some implementations may store default values in their backing 443 * stores. If there is no value associated with the specified key 444 * but there is such a <i>stored default</i>, it is returned in 445 * preference to the specified default. 446 * 447 * @param key key whose associated value is to be returned. 448 * @param def the value to be returned in the event that this 449 * preference node has no value associated with <tt>key</tt>. 450 * @return the value associated with <tt>key</tt>, or <tt>def</tt> 451 * if no value is associated with <tt>key</tt>, or the backing 452 * store is inaccessible. 453 * @throws IllegalStateException if this node (or an ancestor) has been 454 * removed with the {@link #removeNode()} method. 455 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 456 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 457 */ 458 public abstract String get(String key, String def); 459 460 /** 461 * Removes the value associated with the specified key in this preference 462 * node, if any. 463 * 464 * <p>If this implementation supports <i>stored defaults</i>, and there is 465 * such a default for the specified preference, the stored default will be 466 * "exposed" by this call, in the sense that it will be returned 467 * by a succeeding call to <tt>get</tt>. 468 * 469 * @param key key whose mapping is to be removed from the preference node. 470 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 471 * @throws IllegalStateException if this node (or an ancestor) has been 472 * removed with the {@link #removeNode()} method. 473 */ 474 public abstract void remove(String key); 475 476 /** 477 * Removes all of the preferences (key-value associations) in this 478 * preference node. This call has no effect on any descendants 479 * of this node. 480 * 481 * <p>If this implementation supports <i>stored defaults</i>, and this 482 * node in the preferences hierarchy contains any such defaults, 483 * the stored defaults will be "exposed" by this call, in the sense that 484 * they will be returned by succeeding calls to <tt>get</tt>. 485 * 486 * @throws BackingStoreException if this operation cannot be completed 487 * due to a failure in the backing store, or inability to 488 * communicate with it. 489 * @throws IllegalStateException if this node (or an ancestor) has been 490 * removed with the {@link #removeNode()} method. 491 * @see #removeNode() 492 */ 493 public abstract void clear() throws BackingStoreException; 494 495 /** 496 * Associates a string representing the specified int value with the 497 * specified key in this preference node. The associated string is the 498 * one that would be returned if the int value were passed to 499 * {@link Integer#toString(int)}. This method is intended for use in 500 * conjunction with {@link #getInt}. 501 * 502 * @param key key with which the string form of value is to be associated. 503 * @param value value whose string form is to be associated with key. 504 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 505 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 506 * <tt>MAX_KEY_LENGTH</tt>. 507 * @throws IllegalStateException if this node (or an ancestor) has been 508 * removed with the {@link #removeNode()} method. 509 * @see #getInt(String,int) 510 */ 511 public abstract void putInt(String key, int value); 512 513 /** 514 * Returns the int value represented by the string associated with the 515 * specified key in this preference node. The string is converted to 516 * an integer as by {@link Integer#parseInt(String)}. Returns the 517 * specified default if there is no value associated with the key, 518 * the backing store is inaccessible, or if 519 * <tt>Integer.parseInt(String)</tt> would throw a {@link 520 * NumberFormatException} if the associated value were passed. This 521 * method is intended for use in conjunction with {@link #putInt}. 522 * 523 * <p>If the implementation supports <i>stored defaults</i> and such a 524 * default exists, is accessible, and could be converted to an int 525 * with <tt>Integer.parseInt</tt>, this int is returned in preference to 526 * the specified default. 527 * 528 * @param key key whose associated value is to be returned as an int. 529 * @param def the value to be returned in the event that this 530 * preference node has no value associated with <tt>key</tt> 531 * or the associated value cannot be interpreted as an int, 532 * or the backing store is inaccessible. 533 * @return the int value represented by the string associated with 534 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 535 * associated value does not exist or cannot be interpreted as 536 * an int. 537 * @throws IllegalStateException if this node (or an ancestor) has been 538 * removed with the {@link #removeNode()} method. 539 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 540 * @see #putInt(String,int) 541 * @see #get(String,String) 542 */ 543 public abstract int getInt(String key, int def); 544 545 /** 546 * Associates a string representing the specified long value with the 547 * specified key in this preference node. The associated string is the 548 * one that would be returned if the long value were passed to 549 * {@link Long#toString(long)}. This method is intended for use in 550 * conjunction with {@link #getLong}. 551 * 552 * @param key key with which the string form of value is to be associated. 553 * @param value value whose string form is to be associated with key. 554 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 555 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 556 * <tt>MAX_KEY_LENGTH</tt>. 557 * @throws IllegalStateException if this node (or an ancestor) has been 558 * removed with the {@link #removeNode()} method. 559 * @see #getLong(String,long) 560 */ 561 public abstract void putLong(String key, long value); 562 563 /** 564 * Returns the long value represented by the string associated with the 565 * specified key in this preference node. The string is converted to 566 * a long as by {@link Long#parseLong(String)}. Returns the 567 * specified default if there is no value associated with the key, 568 * the backing store is inaccessible, or if 569 * <tt>Long.parseLong(String)</tt> would throw a {@link 570 * NumberFormatException} if the associated value were passed. This 571 * method is intended for use in conjunction with {@link #putLong}. 572 * 573 * <p>If the implementation supports <i>stored defaults</i> and such a 574 * default exists, is accessible, and could be converted to a long 575 * with <tt>Long.parseLong</tt>, this long is returned in preference to 576 * the specified default. 577 * 578 * @param key key whose associated value is to be returned as a long. 579 * @param def the value to be returned in the event that this 580 * preference node has no value associated with <tt>key</tt> 581 * or the associated value cannot be interpreted as a long, 582 * or the backing store is inaccessible. 583 * @return the long value represented by the string associated with 584 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 585 * associated value does not exist or cannot be interpreted as 586 * a long. 587 * @throws IllegalStateException if this node (or an ancestor) has been 588 * removed with the {@link #removeNode()} method. 589 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 590 * @see #putLong(String,long) 591 * @see #get(String,String) 592 */ 593 public abstract long getLong(String key, long def); 594 595 /** 596 * Associates a string representing the specified boolean value with the 597 * specified key in this preference node. The associated string is 598 * <tt>"true"</tt> if the value is true, and <tt>"false"</tt> if it is 599 * false. This method is intended for use in conjunction with 600 * {@link #getBoolean}. 601 * 602 * @param key key with which the string form of value is to be associated. 603 * @param value value whose string form is to be associated with key. 604 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 605 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 606 * <tt>MAX_KEY_LENGTH</tt>. 607 * @throws IllegalStateException if this node (or an ancestor) has been 608 * removed with the {@link #removeNode()} method. 609 * @see #getBoolean(String,boolean) 610 * @see #get(String,String) 611 */ 612 public abstract void putBoolean(String key, boolean value); 613 614 /** 615 * Returns the boolean value represented by the string associated with the 616 * specified key in this preference node. Valid strings 617 * are <tt>"true"</tt>, which represents true, and <tt>"false"</tt>, which 618 * represents false. Case is ignored, so, for example, <tt>"TRUE"</tt> 619 * and <tt>"False"</tt> are also valid. This method is intended for use in 620 * conjunction with {@link #putBoolean}. 621 * 622 * <p>Returns the specified default if there is no value 623 * associated with the key, the backing store is inaccessible, or if the 624 * associated value is something other than <tt>"true"</tt> or 625 * <tt>"false"</tt>, ignoring case. 626 * 627 * <p>If the implementation supports <i>stored defaults</i> and such a 628 * default exists and is accessible, it is used in preference to the 629 * specified default, unless the stored default is something other than 630 * <tt>"true"</tt> or <tt>"false"</tt>, ignoring case, in which case the 631 * specified default is used. 632 * 633 * @param key key whose associated value is to be returned as a boolean. 634 * @param def the value to be returned in the event that this 635 * preference node has no value associated with <tt>key</tt> 636 * or the associated value cannot be interpreted as a boolean, 637 * or the backing store is inaccessible. 638 * @return the boolean value represented by the string associated with 639 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 640 * associated value does not exist or cannot be interpreted as 641 * a boolean. 642 * @throws IllegalStateException if this node (or an ancestor) has been 643 * removed with the {@link #removeNode()} method. 644 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 645 * @see #get(String,String) 646 * @see #putBoolean(String,boolean) 647 */ 648 public abstract boolean getBoolean(String key, boolean def); 649 650 /** 651 * Associates a string representing the specified float value with the 652 * specified key in this preference node. The associated string is the 653 * one that would be returned if the float value were passed to 654 * {@link Float#toString(float)}. This method is intended for use in 655 * conjunction with {@link #getFloat}. 656 * 657 * @param key key with which the string form of value is to be associated. 658 * @param value value whose string form is to be associated with key. 659 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 660 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 661 * <tt>MAX_KEY_LENGTH</tt>. 662 * @throws IllegalStateException if this node (or an ancestor) has been 663 * removed with the {@link #removeNode()} method. 664 * @see #getFloat(String,float) 665 */ 666 public abstract void putFloat(String key, float value); 667 668 /** 669 * Returns the float value represented by the string associated with the 670 * specified key in this preference node. The string is converted to an 671 * integer as by {@link Float#parseFloat(String)}. Returns the specified 672 * default if there is no value associated with the key, the backing store 673 * is inaccessible, or if <tt>Float.parseFloat(String)</tt> would throw a 674 * {@link NumberFormatException} if the associated value were passed. 675 * This method is intended for use in conjunction with {@link #putFloat}. 676 * 677 * <p>If the implementation supports <i>stored defaults</i> and such a 678 * default exists, is accessible, and could be converted to a float 679 * with <tt>Float.parseFloat</tt>, this float is returned in preference to 680 * the specified default. 681 * 682 * @param key key whose associated value is to be returned as a float. 683 * @param def the value to be returned in the event that this 684 * preference node has no value associated with <tt>key</tt> 685 * or the associated value cannot be interpreted as a float, 686 * or the backing store is inaccessible. 687 * @return the float value represented by the string associated with 688 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 689 * associated value does not exist or cannot be interpreted as 690 * a float. 691 * @throws IllegalStateException if this node (or an ancestor) has been 692 * removed with the {@link #removeNode()} method. 693 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 694 * @see #putFloat(String,float) 695 * @see #get(String,String) 696 */ 697 public abstract float getFloat(String key, float def); 698 699 /** 700 * Associates a string representing the specified double value with the 701 * specified key in this preference node. The associated string is the 702 * one that would be returned if the double value were passed to 703 * {@link Double#toString(double)}. This method is intended for use in 704 * conjunction with {@link #getDouble}. 705 * 706 * @param key key with which the string form of value is to be associated. 707 * @param value value whose string form is to be associated with key. 708 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 709 * @throws IllegalArgumentException if <tt>key.length()</tt> exceeds 710 * <tt>MAX_KEY_LENGTH</tt>. 711 * @throws IllegalStateException if this node (or an ancestor) has been 712 * removed with the {@link #removeNode()} method. 713 * @see #getDouble(String,double) 714 */ 715 public abstract void putDouble(String key, double value); 716 717 /** 718 * Returns the double value represented by the string associated with the 719 * specified key in this preference node. The string is converted to an 720 * integer as by {@link Double#parseDouble(String)}. Returns the specified 721 * default if there is no value associated with the key, the backing store 722 * is inaccessible, or if <tt>Double.parseDouble(String)</tt> would throw a 723 * {@link NumberFormatException} if the associated value were passed. 724 * This method is intended for use in conjunction with {@link #putDouble}. 725 * 726 * <p>If the implementation supports <i>stored defaults</i> and such a 727 * default exists, is accessible, and could be converted to a double 728 * with <tt>Double.parseDouble</tt>, this double is returned in preference 729 * to the specified default. 730 * 731 * @param key key whose associated value is to be returned as a double. 732 * @param def the value to be returned in the event that this 733 * preference node has no value associated with <tt>key</tt> 734 * or the associated value cannot be interpreted as a double, 735 * or the backing store is inaccessible. 736 * @return the double value represented by the string associated with 737 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 738 * associated value does not exist or cannot be interpreted as 739 * a double. 740 * @throws IllegalStateException if this node (or an ancestor) has been 741 * removed with the {@link #removeNode()} method. 742 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. 743 * @see #putDouble(String,double) 744 * @see #get(String,String) 745 */ 746 public abstract double getDouble(String key, double def); 747 748 /** 749 * Associates a string representing the specified byte array with the 750 * specified key in this preference node. The associated string is 751 * the <i>Base64</i> encoding of the byte array, as defined in <a 752 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 753 * with one minor change: the string will consist solely of characters 754 * from the <i>Base64 Alphabet</i>; it will not contain any newline 755 * characters. Note that the maximum length of the byte array is limited 756 * to three quarters of <tt>MAX_VALUE_LENGTH</tt> so that the length 757 * of the Base64 encoded String does not exceed <tt>MAX_VALUE_LENGTH</tt>. 758 * This method is intended for use in conjunction with 759 * {@link #getByteArray}. 760 * 761 * @param key key with which the string form of value is to be associated. 762 * @param value value whose string form is to be associated with key. 763 * @throws NullPointerException if key or value is <tt>null</tt>. 764 * @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH 765 * or if value.length exceeds MAX_VALUE_LENGTH*3/4. 766 * @throws IllegalStateException if this node (or an ancestor) has been 767 * removed with the {@link #removeNode()} method. 768 * @see #getByteArray(String,byte[]) 769 * @see #get(String,String) 770 */ 771 public abstract void putByteArray(String key, byte[] value); 772 773 /** 774 * Returns the byte array value represented by the string associated with 775 * the specified key in this preference node. Valid strings are 776 * <i>Base64</i> encoded binary data, as defined in <a 777 * href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8, 778 * with one minor change: the string must consist solely of characters 779 * from the <i>Base64 Alphabet</i>; no newline characters or 780 * extraneous characters are permitted. This method is intended for use 781 * in conjunction with {@link #putByteArray}. 782 * 783 * <p>Returns the specified default if there is no value 784 * associated with the key, the backing store is inaccessible, or if the 785 * associated value is not a valid Base64 encoded byte array 786 * (as defined above). 787 * 788 * <p>If the implementation supports <i>stored defaults</i> and such a 789 * default exists and is accessible, it is used in preference to the 790 * specified default, unless the stored default is not a valid Base64 791 * encoded byte array (as defined above), in which case the 792 * specified default is used. 793 * 794 * @param key key whose associated value is to be returned as a byte array. 795 * @param def the value to be returned in the event that this 796 * preference node has no value associated with <tt>key</tt> 797 * or the associated value cannot be interpreted as a byte array, 798 * or the backing store is inaccessible. 799 * @return the byte array value represented by the string associated with 800 * <tt>key</tt> in this preference node, or <tt>def</tt> if the 801 * associated value does not exist or cannot be interpreted as 802 * a byte array. 803 * @throws IllegalStateException if this node (or an ancestor) has been 804 * removed with the {@link #removeNode()} method. 805 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>. (A 806 * <tt>null</tt> value for <tt>def</tt> <i>is</i> permitted.) 807 * @see #get(String,String) 808 * @see #putByteArray(String,byte[]) 809 */ 810 public abstract byte[] getByteArray(String key, byte[] def); 811 812 /** 813 * Returns all of the keys that have an associated value in this 814 * preference node. (The returned array will be of size zero if 815 * this node has no preferences.) 816 * 817 * <p>If the implementation supports <i>stored defaults</i> and there 818 * are any such defaults at this node that have not been overridden, 819 * by explicit preferences, the defaults are returned in the array in 820 * addition to any explicit preferences. 821 * 822 * @return an array of the keys that have an associated value in this 823 * preference node. 824 * @throws BackingStoreException if this operation cannot be completed 825 * due to a failure in the backing store, or inability to 826 * communicate with it. 827 * @throws IllegalStateException if this node (or an ancestor) has been 828 * removed with the {@link #removeNode()} method. 829 */ 830 public abstract String[] keys() throws BackingStoreException; 831 832 /** 833 * Returns the names of the children of this preference node, relative to 834 * this node. (The returned array will be of size zero if this node has 835 * no children.) 836 * 837 * @return the names of the children of this preference node. 838 * @throws BackingStoreException if this operation cannot be completed 839 * due to a failure in the backing store, or inability to 840 * communicate with it. 841 * @throws IllegalStateException if this node (or an ancestor) has been 842 * removed with the {@link #removeNode()} method. 843 */ 844 public abstract String[] childrenNames() throws BackingStoreException; 845 846 /** 847 * Returns the parent of this preference node, or <tt>null</tt> if this is 848 * the root. 849 * 850 * @return the parent of this preference node. 851 * @throws IllegalStateException if this node (or an ancestor) has been 852 * removed with the {@link #removeNode()} method. 853 */ 854 public abstract Preferences parent(); 855 856 /** 857 * Returns the named preference node in the same tree as this node, 858 * creating it and any of its ancestors if they do not already exist. 859 * Accepts a relative or absolute path name. Relative path names 860 * (which do not begin with the slash character <tt>('/')</tt>) are 861 * interpreted relative to this preference node. 862 * 863 * <p>If the returned node did not exist prior to this call, this node and 864 * any ancestors that were created by this call are not guaranteed 865 * to become permanent until the <tt>flush</tt> method is called on 866 * the returned node (or one of its ancestors or descendants). 867 * 868 * @param pathName the path name of the preference node to return. 869 * @return the specified preference node. 870 * @throws IllegalArgumentException if the path name is invalid (i.e., 871 * it contains multiple consecutive slash characters, or ends 872 * with a slash character and is more than one character long). 873 * @throws NullPointerException if path name is <tt>null</tt>. 874 * @throws IllegalStateException if this node (or an ancestor) has been 875 * removed with the {@link #removeNode()} method. 876 * @see #flush() 877 */ 878 public abstract Preferences node(String pathName); 879 880 /** 881 * Returns true if the named preference node exists in the same tree 882 * as this node. Relative path names (which do not begin with the slash 883 * character <tt>('/')</tt>) are interpreted relative to this preference 884 * node. 885 * 886 * <p>If this node (or an ancestor) has already been removed with the 887 * {@link #removeNode()} method, it <i>is</i> legal to invoke this method, 888 * but only with the path name <tt>""</tt>; the invocation will return 889 * <tt>false</tt>. Thus, the idiom <tt>p.nodeExists("")</tt> may be 890 * used to test whether <tt>p</tt> has been removed. 891 * 892 * @param pathName the path name of the node whose existence 893 * is to be checked. 894 * @return true if the specified node exists. 895 * @throws BackingStoreException if this operation cannot be completed 896 * due to a failure in the backing store, or inability to 897 * communicate with it. 898 * @throws IllegalArgumentException if the path name is invalid (i.e., 899 * it contains multiple consecutive slash characters, or ends 900 * with a slash character and is more than one character long). 901 * @throws NullPointerException if path name is <tt>null</tt>. 902 * @throws IllegalStateException if this node (or an ancestor) has been 903 * removed with the {@link #removeNode()} method and 904 * <tt>pathName</tt> is not the empty string (<tt>""</tt>). 905 */ 906 public abstract boolean nodeExists(String pathName) 907 throws BackingStoreException; 908 909 /** 910 * Removes this preference node and all of its descendants, invalidating 911 * any preferences contained in the removed nodes. Once a node has been 912 * removed, attempting any method other than {@link #name()}, 913 * {@link #absolutePath()}, {@link #isUserNode()}, {@link #flush()} or 914 * {@link #node(String) nodeExists("")} on the corresponding 915 * <tt>Preferences</tt> instance will fail with an 916 * <tt>IllegalStateException</tt>. (The methods defined on {@link Object} 917 * can still be invoked on a node after it has been removed; they will not 918 * throw <tt>IllegalStateException</tt>.) 919 * 920 * <p>The removal is not guaranteed to be persistent until the 921 * <tt>flush</tt> method is called on this node (or an ancestor). 922 * 923 * <p>If this implementation supports <i>stored defaults</i>, removing a 924 * node exposes any stored defaults at or below this node. Thus, a 925 * subsequent call to <tt>nodeExists</tt> on this node's path name may 926 * return <tt>true</tt>, and a subsequent call to <tt>node</tt> on this 927 * path name may return a (different) <tt>Preferences</tt> instance 928 * representing a non-empty collection of preferences and/or children. 929 * 930 * @throws BackingStoreException if this operation cannot be completed 931 * due to a failure in the backing store, or inability to 932 * communicate with it. 933 * @throws IllegalStateException if this node (or an ancestor) has already 934 * been removed with the {@link #removeNode()} method. 935 * @throws UnsupportedOperationException if this method is invoked on 936 * the root node. 937 * @see #flush() 938 */ 939 public abstract void removeNode() throws BackingStoreException; 940 941 /** 942 * Returns this preference node's name, relative to its parent. 943 * 944 * @return this preference node's name, relative to its parent. 945 */ 946 public abstract String name(); 947 948 /** 949 * Returns this preference node's absolute path name. 950 * 951 * @return this preference node's absolute path name. 952 */ 953 public abstract String absolutePath(); 954 955 /** 956 * Returns <tt>true</tt> if this preference node is in the user 957 * preference tree, <tt>false</tt> if it's in the system preference tree. 958 * 959 * @return <tt>true</tt> if this preference node is in the user 960 * preference tree, <tt>false</tt> if it's in the system 961 * preference tree. 962 */ 963 public abstract boolean isUserNode(); 964 965 /** 966 * Returns a string representation of this preferences node, 967 * as if computed by the expression:<tt>(this.isUserNode() ? "User" : 968 * "System") + " Preference Node: " + this.absolutePath()</tt>. 969 */ 970 public abstract String toString(); 971 972 /** 973 * Forces any changes in the contents of this preference node and its 974 * descendants to the persistent store. Once this method returns 975 * successfully, it is safe to assume that all changes made in the 976 * subtree rooted at this node prior to the method invocation have become 977 * permanent. 978 * 979 * <p>Implementations are free to flush changes into the persistent store 980 * at any time. They do not need to wait for this method to be called. 981 * 982 * <p>When a flush occurs on a newly created node, it is made persistent, 983 * as are any ancestors (and descendants) that have yet to be made 984 * persistent. Note however that any preference value changes in 985 * ancestors are <i>not</i> guaranteed to be made persistent. 986 * 987 * <p> If this method is invoked on a node that has been removed with 988 * the {@link #removeNode()} method, flushSpi() is invoked on this node, 989 * but not on others. 990 * 991 * @throws BackingStoreException if this operation cannot be completed 992 * due to a failure in the backing store, or inability to 993 * communicate with it. 994 * @see #sync() 995 */ 996 public abstract void flush() throws BackingStoreException; 997 998 /** 999 * Ensures that future reads from this preference node and its 1000 * descendants reflect any changes that were committed to the persistent 1001 * store (from any VM) prior to the <tt>sync</tt> invocation. As a 1002 * side-effect, forces any changes in the contents of this preference node 1003 * and its descendants to the persistent store, as if the <tt>flush</tt> 1004 * method had been invoked on this node. 1005 * 1006 * @throws BackingStoreException if this operation cannot be completed 1007 * due to a failure in the backing store, or inability to 1008 * communicate with it. 1009 * @throws IllegalStateException if this node (or an ancestor) has been 1010 * removed with the {@link #removeNode()} method. 1011 * @see #flush() 1012 */ 1013 public abstract void sync() throws BackingStoreException; 1014 1015 /** 1016 * Registers the specified listener to receive <i>preference change 1017 * events</i> for this preference node. A preference change event is 1018 * generated when a preference is added to this node, removed from this 1019 * node, or when the value associated with a preference is changed. 1020 * (Preference change events are <i>not</i> generated by the {@link 1021 * #removeNode()} method, which generates a <i>node change event</i>. 1022 * Preference change events <i>are</i> generated by the <tt>clear</tt> 1023 * method.) 1024 * 1025 * <p>Events are only guaranteed for changes made within the same JVM 1026 * as the registered listener, though some implementations may generate 1027 * events for changes made outside this JVM. Events may be generated 1028 * before the changes have been made persistent. Events are not generated 1029 * when preferences are modified in descendants of this node; a caller 1030 * desiring such events must register with each descendant. 1031 * 1032 * @param pcl The preference change listener to add. 1033 * @throws NullPointerException if <tt>pcl</tt> is null. 1034 * @throws IllegalStateException if this node (or an ancestor) has been 1035 * removed with the {@link #removeNode()} method. 1036 * @see #removePreferenceChangeListener(PreferenceChangeListener) 1037 * @see #addNodeChangeListener(NodeChangeListener) 1038 */ 1039 public abstract void addPreferenceChangeListener( 1040 PreferenceChangeListener pcl); 1041 1042 /** 1043 * Removes the specified preference change listener, so it no longer 1044 * receives preference change events. 1045 * 1046 * @param pcl The preference change listener to remove. 1047 * @throws IllegalArgumentException if <tt>pcl</tt> was not a registered 1048 * preference change listener on this node. 1049 * @throws IllegalStateException if this node (or an ancestor) has been 1050 * removed with the {@link #removeNode()} method. 1051 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1052 */ 1053 public abstract void removePreferenceChangeListener( 1054 PreferenceChangeListener pcl); 1055 1056 /** 1057 * Registers the specified listener to receive <i>node change events</i> 1058 * for this node. A node change event is generated when a child node is 1059 * added to or removed from this node. (A single {@link #removeNode()} 1060 * invocation results in multiple <i>node change events</i>, one for every 1061 * node in the subtree rooted at the removed node.) 1062 * 1063 * <p>Events are only guaranteed for changes made within the same JVM 1064 * as the registered listener, though some implementations may generate 1065 * events for changes made outside this JVM. Events may be generated 1066 * before the changes have become permanent. Events are not generated 1067 * when indirect descendants of this node are added or removed; a 1068 * caller desiring such events must register with each descendant. 1069 * 1070 * <p>Few guarantees can be made regarding node creation. Because nodes 1071 * are created implicitly upon access, it may not be feasible for an 1072 * implementation to determine whether a child node existed in the backing 1073 * store prior to access (for example, because the backing store is 1074 * unreachable or cached information is out of date). Under these 1075 * circumstances, implementations are neither required to generate node 1076 * change events nor prohibited from doing so. 1077 * 1078 * @param ncl The <tt>NodeChangeListener</tt> to add. 1079 * @throws NullPointerException if <tt>ncl</tt> is null. 1080 * @throws IllegalStateException if this node (or an ancestor) has been 1081 * removed with the {@link #removeNode()} method. 1082 * @see #removeNodeChangeListener(NodeChangeListener) 1083 * @see #addPreferenceChangeListener(PreferenceChangeListener) 1084 */ 1085 public abstract void addNodeChangeListener(NodeChangeListener ncl); 1086 1087 /** 1088 * Removes the specified <tt>NodeChangeListener</tt>, so it no longer 1089 * receives change events. 1090 * 1091 * @param ncl The <tt>NodeChangeListener</tt> to remove. 1092 * @throws IllegalArgumentException if <tt>ncl</tt> was not a registered 1093 * <tt>NodeChangeListener</tt> on this node. 1094 * @throws IllegalStateException if this node (or an ancestor) has been 1095 * removed with the {@link #removeNode()} method. 1096 * @see #addNodeChangeListener(NodeChangeListener) 1097 */ 1098 public abstract void removeNodeChangeListener(NodeChangeListener ncl); 1099 1100 /** 1101 * Emits on the specified output stream an XML document representing all 1102 * of the preferences contained in this node (but not its descendants). 1103 * This XML document is, in effect, an offline backup of the node. 1104 * 1105 * <p>The XML document will have the following DOCTYPE declaration: 1106 * <pre> 1107 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1108 * </pre> 1109 * The UTF-8 character encoding will be used. 1110 * 1111 * <p>This method is an exception to the general rule that the results of 1112 * concurrently executing multiple methods in this class yields 1113 * results equivalent to some serial execution. If the preferences 1114 * at this node are modified concurrently with an invocation of this 1115 * method, the exported preferences comprise a "fuzzy snapshot" of the 1116 * preferences contained in the node; some of the concurrent modifications 1117 * may be reflected in the exported data while others may not. 1118 * 1119 * @param os the output stream on which to emit the XML document. 1120 * @throws IOException if writing to the specified output stream 1121 * results in an <tt>IOException</tt>. 1122 * @throws BackingStoreException if preference data cannot be read from 1123 * backing store. 1124 * @see #importPreferences(InputStream) 1125 * @throws IllegalStateException if this node (or an ancestor) has been 1126 * removed with the {@link #removeNode()} method. 1127 */ 1128 public abstract void exportNode(OutputStream os) 1129 throws IOException, BackingStoreException; 1130 1131 /** 1132 * Emits an XML document representing all of the preferences contained 1133 * in this node and all of its descendants. This XML document is, in 1134 * effect, an offline backup of the subtree rooted at the node. 1135 * 1136 * <p>The XML document will have the following DOCTYPE declaration: 1137 * <pre> 1138 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1139 * </pre> 1140 * The UTF-8 character encoding will be used. 1141 * 1142 * <p>This method is an exception to the general rule that the results of 1143 * concurrently executing multiple methods in this class yields 1144 * results equivalent to some serial execution. If the preferences 1145 * or nodes in the subtree rooted at this node are modified concurrently 1146 * with an invocation of this method, the exported preferences comprise a 1147 * "fuzzy snapshot" of the subtree; some of the concurrent modifications 1148 * may be reflected in the exported data while others may not. 1149 * 1150 * @param os the output stream on which to emit the XML document. 1151 * @throws IOException if writing to the specified output stream 1152 * results in an <tt>IOException</tt>. 1153 * @throws BackingStoreException if preference data cannot be read from 1154 * backing store. 1155 * @throws IllegalStateException if this node (or an ancestor) has been 1156 * removed with the {@link #removeNode()} method. 1157 * @see #importPreferences(InputStream) 1158 * @see #exportNode(OutputStream) 1159 */ 1160 public abstract void exportSubtree(OutputStream os) 1161 throws IOException, BackingStoreException; 1162 1163 /** 1164 * Imports all of the preferences represented by the XML document on the 1165 * specified input stream. The document may represent user preferences or 1166 * system preferences. If it represents user preferences, the preferences 1167 * will be imported into the calling user's preference tree (even if they 1168 * originally came from a different user's preference tree). If any of 1169 * the preferences described by the document inhabit preference nodes that 1170 * do not exist, the nodes will be created. 1171 * 1172 * <p>The XML document must have the following DOCTYPE declaration: 1173 * <pre> 1174 * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 1175 * </pre> 1176 * (This method is designed for use in conjunction with 1177 * {@link #exportNode(OutputStream)} and 1178 * {@link #exportSubtree(OutputStream)}. 1179 * 1180 * <p>This method is an exception to the general rule that the results of 1181 * concurrently executing multiple methods in this class yields 1182 * results equivalent to some serial execution. The method behaves 1183 * as if implemented on top of the other public methods in this class, 1184 * notably {@link #node(String)} and {@link #put(String, String)}. 1185 * 1186 * @param is the input stream from which to read the XML document. 1187 * @throws IOException if reading from the specified input stream 1188 * results in an <tt>IOException</tt>. 1189 * @throws InvalidPreferencesFormatException Data on input stream does not 1190 * constitute a valid XML document with the mandated document type. 1191 * @throws SecurityException If a security manager is present and 1192 * it denies <tt>RuntimePermission("preferences")</tt>. 1193 * @see RuntimePermission 1194 */ 1195 public static void importPreferences(InputStream is) 1196 throws IOException, InvalidPreferencesFormatException 1197 { 1198 XmlSupport.importPreferences(is); 1199 } 1200} 1201