Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.prefs/share/classes/java/util/prefs/Preferences.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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
26
package java.util.prefs;
27
28
import java.io.InputStream;
29
import java.io.IOException;
30
import java.io.OutputStream;
31
import java.security.AccessController;
32
import java.security.Permission;
33
import java.security.PrivilegedAction;
34
import java.util.Iterator;
35
import java.util.ServiceLoader;
36
import java.util.ServiceConfigurationError;
37
38
// These imports needed only as a workaround for a JavaDoc bug
39
import java.lang.RuntimePermission;
40
import java.lang.Integer;
41
import java.lang.Long;
42
import java.lang.Float;
43
import 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 {@code "/"}. Children of
76
* the root node have absolute path names of {@code "/" + }<i>&lt;node
77
* name&gt;</i>. All other nodes have absolute path names of <i>&lt;parent's
78
* absolute path name&gt;</i>{@code + "/" + }<i>&lt;node name&gt;</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 {@code flush} 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
* {@code flush} 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 {@code Preferences}
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 {@code flush}, 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>{@code
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>{@code
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 {@code Preferences} implementation must have an associated {@link
185
* PreferencesFactory} implementation. Every Java(TM) SE implementation must provide
186
* some means of specifying which {@code PreferencesFactory} 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
* @implNote
192
* The {@code PreferencesFactory} implementation is located as follows:
193
*
194
* <ol>
195
*
196
* <li><p>If the system property
197
* {@systemProperty java.util.prefs.PreferencesFactory} is defined, then it is
198
* taken to be the fully-qualified name of a class implementing the
199
* {@code PreferencesFactory} 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 {@code PreferencesFactory} 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
* {@code java.util.prefs.PreferencesFactory} in the resource
208
* directory {@code META-INF/services}, 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
* {@code PreferencesFactory} 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
*/
224
public abstract class Preferences {
225
226
private static final PreferencesFactory factory = factory();
227
228
@SuppressWarnings("removal")
229
private static PreferencesFactory factory() {
230
// 1. Try user-specified system property
231
String factoryName = AccessController.doPrivileged(
232
new PrivilegedAction<String>() {
233
public String run() {
234
return System.getProperty(
235
"java.util.prefs.PreferencesFactory");}});
236
if (factoryName != null) {
237
// FIXME: This code should be run in a doPrivileged and
238
// not use the context classloader, to avoid being
239
// dependent on the invoking thread.
240
// Checking AllPermission also seems wrong.
241
try {
242
@SuppressWarnings("deprecation")
243
Object result =Class.forName(factoryName, false,
244
ClassLoader.getSystemClassLoader())
245
.newInstance();
246
return (PreferencesFactory)result;
247
} catch (Exception ex) {
248
try {
249
// workaround for javaws, plugin,
250
// load factory class using non-system classloader
251
SecurityManager sm = System.getSecurityManager();
252
if (sm != null) {
253
sm.checkPermission(new java.security.AllPermission());
254
}
255
@SuppressWarnings("deprecation")
256
Object result = Class.forName(factoryName, false,
257
Thread.currentThread()
258
.getContextClassLoader())
259
.newInstance();
260
return (PreferencesFactory) result;
261
} catch (Exception e) {
262
throw new InternalError(
263
"Can't instantiate Preferences factory "
264
+ factoryName, e);
265
}
266
}
267
}
268
269
return AccessController.doPrivileged(
270
new PrivilegedAction<PreferencesFactory>() {
271
public PreferencesFactory run() {
272
return factory1();}});
273
}
274
275
private static PreferencesFactory factory1() {
276
// 2. Try service provider interface
277
Iterator<PreferencesFactory> itr = ServiceLoader
278
.load(PreferencesFactory.class, ClassLoader.getSystemClassLoader())
279
.iterator();
280
281
// choose first provider instance
282
while (itr.hasNext()) {
283
try {
284
return itr.next();
285
} catch (ServiceConfigurationError sce) {
286
if (sce.getCause() instanceof SecurityException) {
287
// Ignore the security exception, try the next provider
288
continue;
289
}
290
throw sce;
291
}
292
}
293
294
// 3. Use platform-specific system-wide default
295
String osName = System.getProperty("os.name");
296
String platformFactory;
297
if (osName.startsWith("Windows")) {
298
platformFactory = "java.util.prefs.WindowsPreferencesFactory";
299
} else if (osName.contains("OS X")) {
300
platformFactory = "java.util.prefs.MacOSXPreferencesFactory";
301
} else {
302
platformFactory = "java.util.prefs.FileSystemPreferencesFactory";
303
}
304
try {
305
@SuppressWarnings("deprecation")
306
Object result = Class.forName(platformFactory, false,
307
Preferences.class.getClassLoader()).newInstance();
308
return (PreferencesFactory) result;
309
} catch (Exception e) {
310
throw new InternalError(
311
"Can't instantiate platform default Preferences factory "
312
+ platformFactory, e);
313
}
314
}
315
316
/**
317
* Maximum length of string allowed as a key (80 characters).
318
*/
319
public static final int MAX_KEY_LENGTH = 80;
320
321
/**
322
* Maximum length of string allowed as a value (8192 characters).
323
*/
324
public static final int MAX_VALUE_LENGTH = 8*1024;
325
326
/**
327
* Maximum length of a node name (80 characters).
328
*/
329
public static final int MAX_NAME_LENGTH = 80;
330
331
/**
332
* Returns the preference node from the calling user's preference tree
333
* that is associated (by convention) with the specified class's package.
334
* The convention is as follows: the absolute path name of the node is the
335
* fully qualified package name, preceded by a slash ({@code '/'}), and
336
* with each period ({@code '.'}) replaced by a slash. For example the
337
* absolute path name of the node associated with the class
338
* {@code com.acme.widget.Foo} is {@code /com/acme/widget}.
339
*
340
* <p>This convention does not apply to the unnamed package, whose
341
* associated preference node is {@code <unnamed>}. This node
342
* is not intended for long term use, but for convenience in the early
343
* development of programs that do not yet belong to a package, and
344
* for "throwaway" programs. <i>Valuable data should not be stored
345
* at this node as it is shared by all programs that use it.</i>
346
*
347
* <p>A class {@code Foo} wishing to access preferences pertaining to its
348
* package can obtain a preference node as follows: <pre>
349
* static Preferences prefs = Preferences.userNodeForPackage(Foo.class);
350
* </pre>
351
* This idiom obviates the need for using a string to describe the
352
* preferences node and decreases the likelihood of a run-time failure.
353
* (If the class name is misspelled, it will typically result in a
354
* compile-time error.)
355
*
356
* <p>Invoking this method will result in the creation of the returned
357
* node and its ancestors if they do not already exist. If the returned
358
* node did not exist prior to this call, this node and any ancestors that
359
* were created by this call are not guaranteed to become permanent until
360
* the {@code flush} method is called on the returned node (or one of its
361
* ancestors or descendants).
362
*
363
* @param c the class for whose package a user preference node is desired.
364
* @return the user preference node associated with the package of which
365
* {@code c} is a member.
366
* @throws NullPointerException if {@code c} is {@code null}.
367
* @throws SecurityException if a security manager is present and
368
* it denies {@code RuntimePermission("preferences")}.
369
* @see RuntimePermission
370
*/
371
public static Preferences userNodeForPackage(Class<?> c) {
372
return userRoot().node(nodeName(c));
373
}
374
375
/**
376
* Returns the preference node from the system preference tree that is
377
* associated (by convention) with the specified class's package. The
378
* convention is as follows: the absolute path name of the node is the
379
* fully qualified package name, preceded by a slash ({@code '/'}), and
380
* with each period ({@code '.'}) replaced by a slash. For example the
381
* absolute path name of the node associated with the class
382
* {@code com.acme.widget.Foo} is {@code /com/acme/widget}.
383
*
384
* <p>This convention does not apply to the unnamed package, whose
385
* associated preference node is {@code <unnamed>}. This node
386
* is not intended for long term use, but for convenience in the early
387
* development of programs that do not yet belong to a package, and
388
* for "throwaway" programs. <i>Valuable data should not be stored
389
* at this node as it is shared by all programs that use it.</i>
390
*
391
* <p>A class {@code Foo} wishing to access preferences pertaining to its
392
* package can obtain a preference node as follows: <pre>
393
* static Preferences prefs = Preferences.systemNodeForPackage(Foo.class);
394
* </pre>
395
* This idiom obviates the need for using a string to describe the
396
* preferences node and decreases the likelihood of a run-time failure.
397
* (If the class name is misspelled, it will typically result in a
398
* compile-time error.)
399
*
400
* <p>Invoking this method will result in the creation of the returned
401
* node and its ancestors if they do not already exist. If the returned
402
* node did not exist prior to this call, this node and any ancestors that
403
* were created by this call are not guaranteed to become permanent until
404
* the {@code flush} method is called on the returned node (or one of its
405
* ancestors or descendants).
406
*
407
* @param c the class for whose package a system preference node is desired.
408
* @return the system preference node associated with the package of which
409
* {@code c} is a member.
410
* @throws NullPointerException if {@code c} is {@code null}.
411
* @throws SecurityException if a security manager is present and
412
* it denies {@code RuntimePermission("preferences")}.
413
* @see RuntimePermission
414
*/
415
public static Preferences systemNodeForPackage(Class<?> c) {
416
return systemRoot().node(nodeName(c));
417
}
418
419
/**
420
* Returns the absolute path name of the node corresponding to the package
421
* of the specified object.
422
*
423
* @throws IllegalArgumentException if the package has node preferences
424
* node associated with it.
425
*/
426
private static String nodeName(Class<?> c) {
427
if (c.isArray())
428
throw new IllegalArgumentException(
429
"Arrays have no associated preferences node.");
430
String className = c.getName();
431
int pkgEndIndex = className.lastIndexOf('.');
432
if (pkgEndIndex < 0)
433
return "/<unnamed>";
434
String packageName = className.substring(0, pkgEndIndex);
435
return "/" + packageName.replace('.', '/');
436
}
437
438
/**
439
* This permission object represents the permission required to get
440
* access to the user or system root (which in turn allows for all
441
* other operations).
442
*/
443
private static Permission prefsPerm = new RuntimePermission("preferences");
444
445
/**
446
* Returns the root preference node for the calling user.
447
*
448
* @return the root preference node for the calling user.
449
* @throws SecurityException If a security manager is present and
450
* it denies {@code RuntimePermission("preferences")}.
451
* @see RuntimePermission
452
*/
453
public static Preferences userRoot() {
454
@SuppressWarnings("removal")
455
SecurityManager security = System.getSecurityManager();
456
if (security != null)
457
security.checkPermission(prefsPerm);
458
459
return factory.userRoot();
460
}
461
462
/**
463
* Returns the root preference node for the system.
464
*
465
* @return the root preference node for the system.
466
* @throws SecurityException If a security manager is present and
467
* it denies {@code RuntimePermission("preferences")}.
468
* @see RuntimePermission
469
*/
470
public static Preferences systemRoot() {
471
@SuppressWarnings("removal")
472
SecurityManager security = System.getSecurityManager();
473
if (security != null)
474
security.checkPermission(prefsPerm);
475
476
return factory.systemRoot();
477
}
478
479
/**
480
* Sole constructor. (For invocation by subclass constructors, typically
481
* implicit.)
482
*/
483
protected Preferences() {
484
}
485
486
/**
487
* Associates the specified value with the specified key in this
488
* preference node.
489
*
490
* @param key key with which the specified value is to be associated.
491
* @param value value to be associated with the specified key.
492
* @throws NullPointerException if key or value is {@code null}.
493
* @throws IllegalArgumentException if {@code key.length()} exceeds
494
* {@code MAX_KEY_LENGTH} or if {@code value.length} exceeds
495
* {@code MAX_VALUE_LENGTH}.
496
* @throws IllegalStateException if this node (or an ancestor) has been
497
* removed with the {@link #removeNode()} method.
498
* @throws IllegalArgumentException if either key or value contain
499
* the null control character, code point U+0000.
500
*/
501
public abstract void put(String key, String value);
502
503
/**
504
* Returns the value associated with the specified key in this preference
505
* node. Returns the specified default if there is no value associated
506
* with the key, or the backing store is inaccessible.
507
*
508
* <p>Some implementations may store default values in their backing
509
* stores. If there is no value associated with the specified key
510
* but there is such a <i>stored default</i>, it is returned in
511
* preference to the specified default.
512
*
513
* @param key key whose associated value is to be returned.
514
* @param def the value to be returned in the event that this
515
* preference node has no value associated with {@code key}.
516
* @return the value associated with {@code key}, or {@code def}
517
* if no value is associated with {@code key}, or the backing
518
* store is inaccessible.
519
* @throws IllegalStateException if this node (or an ancestor) has been
520
* removed with the {@link #removeNode()} method.
521
* @throws NullPointerException if {@code key} is {@code null}. (A
522
* {@code null} value for {@code def} <i>is</i> permitted.)
523
* @throws IllegalArgumentException if key contains the null control
524
* character, code point U+0000.
525
*/
526
public abstract String get(String key, String def);
527
528
/**
529
* Removes the value associated with the specified key in this preference
530
* node, if any.
531
*
532
* <p>If this implementation supports <i>stored defaults</i>, and there is
533
* such a default for the specified preference, the stored default will be
534
* "exposed" by this call, in the sense that it will be returned
535
* by a succeeding call to {@code get}.
536
*
537
* @param key key whose mapping is to be removed from the preference node.
538
* @throws NullPointerException if {@code key} is {@code null}.
539
* @throws IllegalStateException if this node (or an ancestor) has been
540
* removed with the {@link #removeNode()} method.
541
* @throws IllegalArgumentException if key contains the null control
542
* character, code point U+0000.
543
*/
544
public abstract void remove(String key);
545
546
/**
547
* Removes all of the preferences (key-value associations) in this
548
* preference node. This call has no effect on any descendants
549
* of this node.
550
*
551
* <p>If this implementation supports <i>stored defaults</i>, and this
552
* node in the preferences hierarchy contains any such defaults,
553
* the stored defaults will be "exposed" by this call, in the sense that
554
* they will be returned by succeeding calls to {@code get}.
555
*
556
* @throws BackingStoreException if this operation cannot be completed
557
* due to a failure in the backing store, or inability to
558
* communicate with it.
559
* @throws IllegalStateException if this node (or an ancestor) has been
560
* removed with the {@link #removeNode()} method.
561
* @see #removeNode()
562
*/
563
public abstract void clear() throws BackingStoreException;
564
565
/**
566
* Associates a string representing the specified int value with the
567
* specified key in this preference node. The associated string is the
568
* one that would be returned if the int value were passed to
569
* {@link Integer#toString(int)}. This method is intended for use in
570
* conjunction with {@link #getInt}.
571
*
572
* @param key key with which the string form of value is to be associated.
573
* @param value value whose string form is to be associated with key.
574
* @throws NullPointerException if {@code key} is {@code null}.
575
* @throws IllegalArgumentException if {@code key.length()} exceeds
576
* {@code MAX_KEY_LENGTH}.
577
* @throws IllegalStateException if this node (or an ancestor) has been
578
* removed with the {@link #removeNode()} method.
579
* @throws IllegalArgumentException if key contains
580
* the null control character, code point U+0000.
581
* @see #getInt(String,int)
582
*/
583
public abstract void putInt(String key, int value);
584
585
/**
586
* Returns the int value represented by the string associated with the
587
* specified key in this preference node. The string is converted to
588
* an integer as by {@link Integer#parseInt(String)}. Returns the
589
* specified default if there is no value associated with the key,
590
* the backing store is inaccessible, or if
591
* {@code Integer.parseInt(String)} would throw a {@link
592
* NumberFormatException} if the associated value were passed. This
593
* method is intended for use in conjunction with {@link #putInt}.
594
*
595
* <p>If the implementation supports <i>stored defaults</i> and such a
596
* default exists, is accessible, and could be converted to an int
597
* with {@code Integer.parseInt}, this int is returned in preference to
598
* the specified default.
599
*
600
* @param key key whose associated value is to be returned as an int.
601
* @param def the value to be returned in the event that this
602
* preference node has no value associated with {@code key}
603
* or the associated value cannot be interpreted as an int,
604
* or the backing store is inaccessible.
605
* @return the int value represented by the string associated with
606
* {@code key} in this preference node, or {@code def} if the
607
* associated value does not exist or cannot be interpreted as
608
* an int.
609
* @throws IllegalStateException if this node (or an ancestor) has been
610
* removed with the {@link #removeNode()} method.
611
* @throws NullPointerException if {@code key} is {@code null}.
612
* @throws IllegalArgumentException if key contains the null control
613
* character, code point U+0000.
614
* @see #putInt(String,int)
615
* @see #get(String,String)
616
*/
617
public abstract int getInt(String key, int def);
618
619
/**
620
* Associates a string representing the specified long value with the
621
* specified key in this preference node. The associated string is the
622
* one that would be returned if the long value were passed to
623
* {@link Long#toString(long)}. This method is intended for use in
624
* conjunction with {@link #getLong}.
625
*
626
* @param key key with which the string form of value is to be associated.
627
* @param value value whose string form is to be associated with key.
628
* @throws NullPointerException if {@code key} is {@code null}.
629
* @throws IllegalArgumentException if {@code key.length()} exceeds
630
* {@code MAX_KEY_LENGTH}.
631
* @throws IllegalStateException if this node (or an ancestor) has been
632
* removed with the {@link #removeNode()} method.
633
* @throws IllegalArgumentException if key contains
634
* the null control character, code point U+0000.
635
* @see #getLong(String,long)
636
*/
637
public abstract void putLong(String key, long value);
638
639
/**
640
* Returns the long value represented by the string associated with the
641
* specified key in this preference node. The string is converted to
642
* a long as by {@link Long#parseLong(String)}. Returns the
643
* specified default if there is no value associated with the key,
644
* the backing store is inaccessible, or if
645
* {@code Long.parseLong(String)} would throw a {@link
646
* NumberFormatException} if the associated value were passed. This
647
* method is intended for use in conjunction with {@link #putLong}.
648
*
649
* <p>If the implementation supports <i>stored defaults</i> and such a
650
* default exists, is accessible, and could be converted to a long
651
* with {@code Long.parseLong}, this long is returned in preference to
652
* the specified default.
653
*
654
* @param key key whose associated value is to be returned as a long.
655
* @param def the value to be returned in the event that this
656
* preference node has no value associated with {@code key}
657
* or the associated value cannot be interpreted as a long,
658
* or the backing store is inaccessible.
659
* @return the long value represented by the string associated with
660
* {@code key} in this preference node, or {@code def} if the
661
* associated value does not exist or cannot be interpreted as
662
* a long.
663
* @throws IllegalStateException if this node (or an ancestor) has been
664
* removed with the {@link #removeNode()} method.
665
* @throws NullPointerException if {@code key} is {@code null}.
666
* @throws IllegalArgumentException if key contains the null control
667
* character, code point U+0000.
668
* @see #putLong(String,long)
669
* @see #get(String,String)
670
*/
671
public abstract long getLong(String key, long def);
672
673
/**
674
* Associates a string representing the specified boolean value with the
675
* specified key in this preference node. The associated string is
676
* {@code "true"} if the value is true, and {@code "false"} if it is
677
* false. This method is intended for use in conjunction with
678
* {@link #getBoolean}.
679
*
680
* @param key key with which the string form of value is to be associated.
681
* @param value value whose string form is to be associated with key.
682
* @throws NullPointerException if {@code key} is {@code null}.
683
* @throws IllegalArgumentException if {@code key.length()} exceeds
684
* {@code MAX_KEY_LENGTH}.
685
* @throws IllegalStateException if this node (or an ancestor) has been
686
* removed with the {@link #removeNode()} method.
687
* @throws IllegalArgumentException if key contains
688
* the null control character, code point U+0000.
689
* @see #getBoolean(String,boolean)
690
* @see #get(String,String)
691
*/
692
public abstract void putBoolean(String key, boolean value);
693
694
/**
695
* Returns the boolean value represented by the string associated with the
696
* specified key in this preference node. Valid strings
697
* are {@code "true"}, which represents true, and {@code "false"}, which
698
* represents false. Case is ignored, so, for example, {@code "TRUE"}
699
* and {@code "False"} are also valid. This method is intended for use in
700
* conjunction with {@link #putBoolean}.
701
*
702
* <p>Returns the specified default if there is no value
703
* associated with the key, the backing store is inaccessible, or if the
704
* associated value is something other than {@code "true"} or
705
* {@code "false"}, ignoring case.
706
*
707
* <p>If the implementation supports <i>stored defaults</i> and such a
708
* default exists and is accessible, it is used in preference to the
709
* specified default, unless the stored default is something other than
710
* {@code "true"} or {@code "false"}, ignoring case, in which case the
711
* specified default is used.
712
*
713
* @param key key whose associated value is to be returned as a boolean.
714
* @param def the value to be returned in the event that this
715
* preference node has no value associated with {@code key}
716
* or the associated value cannot be interpreted as a boolean,
717
* or the backing store is inaccessible.
718
* @return the boolean value represented by the string associated with
719
* {@code key} in this preference node, or {@code def} if the
720
* associated value does not exist or cannot be interpreted as
721
* a boolean.
722
* @throws IllegalStateException if this node (or an ancestor) has been
723
* removed with the {@link #removeNode()} method.
724
* @throws NullPointerException if {@code key} is {@code null}.
725
* @throws IllegalArgumentException if key contains the null control
726
* character, code point U+0000.
727
* @see #get(String,String)
728
* @see #putBoolean(String,boolean)
729
*/
730
public abstract boolean getBoolean(String key, boolean def);
731
732
/**
733
* Associates a string representing the specified float value with the
734
* specified key in this preference node. The associated string is the
735
* one that would be returned if the float value were passed to
736
* {@link Float#toString(float)}. This method is intended for use in
737
* conjunction with {@link #getFloat}.
738
*
739
* @param key key with which the string form of value is to be associated.
740
* @param value value whose string form is to be associated with key.
741
* @throws NullPointerException if {@code key} is {@code null}.
742
* @throws IllegalArgumentException if {@code key.length()} exceeds
743
* {@code MAX_KEY_LENGTH}.
744
* @throws IllegalStateException if this node (or an ancestor) has been
745
* removed with the {@link #removeNode()} method.
746
* @throws IllegalArgumentException if key contains
747
* the null control character, code point U+0000.
748
* @see #getFloat(String,float)
749
*/
750
public abstract void putFloat(String key, float value);
751
752
/**
753
* Returns the float value represented by the string associated with the
754
* specified key in this preference node. The string is converted to an
755
* integer as by {@link Float#parseFloat(String)}. Returns the specified
756
* default if there is no value associated with the key, the backing store
757
* is inaccessible, or if {@code Float.parseFloat(String)} would throw a
758
* {@link NumberFormatException} if the associated value were passed.
759
* This method is intended for use in conjunction with {@link #putFloat}.
760
*
761
* <p>If the implementation supports <i>stored defaults</i> and such a
762
* default exists, is accessible, and could be converted to a float
763
* with {@code Float.parseFloat}, this float is returned in preference to
764
* the specified default.
765
*
766
* @param key key whose associated value is to be returned as a float.
767
* @param def the value to be returned in the event that this
768
* preference node has no value associated with {@code key}
769
* or the associated value cannot be interpreted as a float,
770
* or the backing store is inaccessible.
771
* @return the float value represented by the string associated with
772
* {@code key} in this preference node, or {@code def} if the
773
* associated value does not exist or cannot be interpreted as
774
* a float.
775
* @throws IllegalStateException if this node (or an ancestor) has been
776
* removed with the {@link #removeNode()} method.
777
* @throws NullPointerException if {@code key} is {@code null}.
778
* @throws IllegalArgumentException if key contains the null control
779
* character, code point U+0000.
780
* @see #putFloat(String,float)
781
* @see #get(String,String)
782
*/
783
public abstract float getFloat(String key, float def);
784
785
/**
786
* Associates a string representing the specified double value with the
787
* specified key in this preference node. The associated string is the
788
* one that would be returned if the double value were passed to
789
* {@link Double#toString(double)}. This method is intended for use in
790
* conjunction with {@link #getDouble}.
791
*
792
* @param key key with which the string form of value is to be associated.
793
* @param value value whose string form is to be associated with key.
794
* @throws NullPointerException if {@code key} is {@code null}.
795
* @throws IllegalArgumentException if {@code key.length()} exceeds
796
* {@code MAX_KEY_LENGTH}.
797
* @throws IllegalStateException if this node (or an ancestor) has been
798
* removed with the {@link #removeNode()} method.
799
* @throws IllegalArgumentException if key contains
800
* the null control character, code point U+0000.
801
* @see #getDouble(String,double)
802
*/
803
public abstract void putDouble(String key, double value);
804
805
/**
806
* Returns the double value represented by the string associated with the
807
* specified key in this preference node. The string is converted to an
808
* integer as by {@link Double#parseDouble(String)}. Returns the specified
809
* default if there is no value associated with the key, the backing store
810
* is inaccessible, or if {@code Double.parseDouble(String)} would throw a
811
* {@link NumberFormatException} if the associated value were passed.
812
* This method is intended for use in conjunction with {@link #putDouble}.
813
*
814
* <p>If the implementation supports <i>stored defaults</i> and such a
815
* default exists, is accessible, and could be converted to a double
816
* with {@code Double.parseDouble}, this double is returned in preference
817
* to the specified default.
818
*
819
* @param key key whose associated value is to be returned as a double.
820
* @param def the value to be returned in the event that this
821
* preference node has no value associated with {@code key}
822
* or the associated value cannot be interpreted as a double,
823
* or the backing store is inaccessible.
824
* @return the double value represented by the string associated with
825
* {@code key} in this preference node, or {@code def} if the
826
* associated value does not exist or cannot be interpreted as
827
* a double.
828
* @throws IllegalStateException if this node (or an ancestor) has been
829
* removed with the {@link #removeNode()} method.
830
* @throws NullPointerException if {@code key} is {@code null}.
831
* @throws IllegalArgumentException if key contains the null control
832
* character, code point U+0000.
833
* @see #putDouble(String,double)
834
* @see #get(String,String)
835
*/
836
public abstract double getDouble(String key, double def);
837
838
/**
839
* Associates a string representing the specified byte array with the
840
* specified key in this preference node. The associated string is
841
* the <i>Base64</i> encoding of the byte array, as defined in <a
842
* href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8,
843
* with one minor change: the string will consist solely of characters
844
* from the <i>Base64 Alphabet</i>; it will not contain any newline
845
* characters. Note that the maximum length of the byte array is limited
846
* to three quarters of {@code MAX_VALUE_LENGTH} so that the length
847
* of the Base64 encoded String does not exceed {@code MAX_VALUE_LENGTH}.
848
* This method is intended for use in conjunction with
849
* {@link #getByteArray}.
850
*
851
* @param key key with which the string form of value is to be associated.
852
* @param value value whose string form is to be associated with key.
853
* @throws NullPointerException if key or value is {@code null}.
854
* @throws IllegalArgumentException if key.length() exceeds MAX_KEY_LENGTH
855
* or if value.length exceeds MAX_VALUE_LENGTH*3/4.
856
* @throws IllegalStateException if this node (or an ancestor) has been
857
* removed with the {@link #removeNode()} method.
858
* @throws IllegalArgumentException if key contains
859
* the null control character, code point U+0000.
860
* @see #getByteArray(String,byte[])
861
* @see #get(String,String)
862
*/
863
public abstract void putByteArray(String key, byte[] value);
864
865
/**
866
* Returns the byte array value represented by the string associated with
867
* the specified key in this preference node. Valid strings are
868
* <i>Base64</i> encoded binary data, as defined in <a
869
* href=http://www.ietf.org/rfc/rfc2045.txt>RFC 2045</a>, Section 6.8,
870
* with one minor change: the string must consist solely of characters
871
* from the <i>Base64 Alphabet</i>; no newline characters or
872
* extraneous characters are permitted. This method is intended for use
873
* in conjunction with {@link #putByteArray}.
874
*
875
* <p>Returns the specified default if there is no value
876
* associated with the key, the backing store is inaccessible, or if the
877
* associated value is not a valid Base64 encoded byte array
878
* (as defined above).
879
*
880
* <p>If the implementation supports <i>stored defaults</i> and such a
881
* default exists and is accessible, it is used in preference to the
882
* specified default, unless the stored default is not a valid Base64
883
* encoded byte array (as defined above), in which case the
884
* specified default is used.
885
*
886
* @param key key whose associated value is to be returned as a byte array.
887
* @param def the value to be returned in the event that this
888
* preference node has no value associated with {@code key}
889
* or the associated value cannot be interpreted as a byte array,
890
* or the backing store is inaccessible.
891
* @return the byte array value represented by the string associated with
892
* {@code key} in this preference node, or {@code def} if the
893
* associated value does not exist or cannot be interpreted as
894
* a byte array.
895
* @throws IllegalStateException if this node (or an ancestor) has been
896
* removed with the {@link #removeNode()} method.
897
* @throws NullPointerException if {@code key} is {@code null}. (A
898
* {@code null} value for {@code def} <i>is</i> permitted.)
899
* @throws IllegalArgumentException if key contains the null control
900
* character, code point U+0000.
901
* @see #get(String,String)
902
* @see #putByteArray(String,byte[])
903
*/
904
public abstract byte[] getByteArray(String key, byte[] def);
905
906
/**
907
* Returns all of the keys that have an associated value in this
908
* preference node. (The returned array will be of size zero if
909
* this node has no preferences.)
910
*
911
* <p>If the implementation supports <i>stored defaults</i> and there
912
* are any such defaults at this node that have not been overridden,
913
* by explicit preferences, the defaults are returned in the array in
914
* addition to any explicit preferences.
915
*
916
* @return an array of the keys that have an associated value in this
917
* preference node.
918
* @throws BackingStoreException if this operation cannot be completed
919
* due to a failure in the backing store, or inability to
920
* communicate with it.
921
* @throws IllegalStateException if this node (or an ancestor) has been
922
* removed with the {@link #removeNode()} method.
923
*/
924
public abstract String[] keys() throws BackingStoreException;
925
926
/**
927
* Returns the names of the children of this preference node, relative to
928
* this node. (The returned array will be of size zero if this node has
929
* no children.)
930
*
931
* @return the names of the children of this preference node.
932
* @throws BackingStoreException if this operation cannot be completed
933
* due to a failure in the backing store, or inability to
934
* communicate with it.
935
* @throws IllegalStateException if this node (or an ancestor) has been
936
* removed with the {@link #removeNode()} method.
937
*/
938
public abstract String[] childrenNames() throws BackingStoreException;
939
940
/**
941
* Returns the parent of this preference node, or {@code null} if this is
942
* the root.
943
*
944
* @return the parent of this preference node.
945
* @throws IllegalStateException if this node (or an ancestor) has been
946
* removed with the {@link #removeNode()} method.
947
*/
948
public abstract Preferences parent();
949
950
/**
951
* Returns the named preference node in the same tree as this node,
952
* creating it and any of its ancestors if they do not already exist.
953
* Accepts a relative or absolute path name. Relative path names
954
* (which do not begin with the slash character {@code ('/')}) are
955
* interpreted relative to this preference node.
956
*
957
* <p>If the returned node did not exist prior to this call, this node and
958
* any ancestors that were created by this call are not guaranteed
959
* to become permanent until the {@code flush} method is called on
960
* the returned node (or one of its ancestors or descendants).
961
*
962
* @param pathName the path name of the preference node to return.
963
* @return the specified preference node.
964
* @throws IllegalArgumentException if the path name is invalid (i.e.,
965
* it contains multiple consecutive slash characters, or ends
966
* with a slash character and is more than one character long).
967
* @throws NullPointerException if path name is {@code null}.
968
* @throws IllegalStateException if this node (or an ancestor) has been
969
* removed with the {@link #removeNode()} method.
970
* @see #flush()
971
*/
972
public abstract Preferences node(String pathName);
973
974
/**
975
* Returns true if the named preference node exists in the same tree
976
* as this node. Relative path names (which do not begin with the slash
977
* character {@code ('/')}) are interpreted relative to this preference
978
* node.
979
*
980
* <p>If this node (or an ancestor) has already been removed with the
981
* {@link #removeNode()} method, it <i>is</i> legal to invoke this method,
982
* but only with the path name {@code ""}; the invocation will return
983
* {@code false}. Thus, the idiom {@code p.nodeExists("")} may be
984
* used to test whether {@code p} has been removed.
985
*
986
* @param pathName the path name of the node whose existence
987
* is to be checked.
988
* @return true if the specified node exists.
989
* @throws BackingStoreException if this operation cannot be completed
990
* due to a failure in the backing store, or inability to
991
* communicate with it.
992
* @throws IllegalArgumentException if the path name is invalid (i.e.,
993
* it contains multiple consecutive slash characters, or ends
994
* with a slash character and is more than one character long).
995
* @throws NullPointerException if path name is {@code null}.
996
* @throws IllegalStateException if this node (or an ancestor) has been
997
* removed with the {@link #removeNode()} method and
998
* {@code pathName} is not the empty string ({@code ""}).
999
*/
1000
public abstract boolean nodeExists(String pathName)
1001
throws BackingStoreException;
1002
1003
/**
1004
* Removes this preference node and all of its descendants, invalidating
1005
* any preferences contained in the removed nodes. Once a node has been
1006
* removed, attempting any method other than {@link #name()},
1007
* {@link #absolutePath()}, {@link #isUserNode()}, {@link #flush()} or
1008
* {@link #node(String) nodeExists("")} on the corresponding
1009
* {@code Preferences} instance will fail with an
1010
* {@code IllegalStateException}. (The methods defined on {@link Object}
1011
* can still be invoked on a node after it has been removed; they will not
1012
* throw {@code IllegalStateException}.)
1013
*
1014
* <p>The removal is not guaranteed to be persistent until the
1015
* {@code flush} method is called on this node (or an ancestor).
1016
*
1017
* <p>If this implementation supports <i>stored defaults</i>, removing a
1018
* node exposes any stored defaults at or below this node. Thus, a
1019
* subsequent call to {@code nodeExists} on this node's path name may
1020
* return {@code true}, and a subsequent call to {@code node} on this
1021
* path name may return a (different) {@code Preferences} instance
1022
* representing a non-empty collection of preferences and/or children.
1023
*
1024
* @throws BackingStoreException if this operation cannot be completed
1025
* due to a failure in the backing store, or inability to
1026
* communicate with it.
1027
* @throws IllegalStateException if this node (or an ancestor) has already
1028
* been removed with the {@link #removeNode()} method.
1029
* @throws UnsupportedOperationException if this method is invoked on
1030
* the root node.
1031
* @see #flush()
1032
*/
1033
public abstract void removeNode() throws BackingStoreException;
1034
1035
/**
1036
* Returns this preference node's name, relative to its parent.
1037
*
1038
* @return this preference node's name, relative to its parent.
1039
*/
1040
public abstract String name();
1041
1042
/**
1043
* Returns this preference node's absolute path name.
1044
*
1045
* @return this preference node's absolute path name.
1046
*/
1047
public abstract String absolutePath();
1048
1049
/**
1050
* Returns {@code true} if this preference node is in the user
1051
* preference tree, {@code false} if it's in the system preference tree.
1052
*
1053
* @return {@code true} if this preference node is in the user
1054
* preference tree, {@code false} if it's in the system
1055
* preference tree.
1056
*/
1057
public abstract boolean isUserNode();
1058
1059
/**
1060
* Returns a string representation of this preferences node,
1061
* as if computed by the expression:{@code (this.isUserNode() ? "User" :
1062
* "System") + " Preference Node: " + this.absolutePath()}.
1063
*/
1064
public abstract String toString();
1065
1066
/**
1067
* Forces any changes in the contents of this preference node and its
1068
* descendants to the persistent store. Once this method returns
1069
* successfully, it is safe to assume that all changes made in the
1070
* subtree rooted at this node prior to the method invocation have become
1071
* permanent.
1072
*
1073
* <p>Implementations are free to flush changes into the persistent store
1074
* at any time. They do not need to wait for this method to be called.
1075
*
1076
* <p>When a flush occurs on a newly created node, it is made persistent,
1077
* as are any ancestors (and descendants) that have yet to be made
1078
* persistent. Note however that any preference value changes in
1079
* ancestors are <i>not</i> guaranteed to be made persistent.
1080
*
1081
* <p> If this method is invoked on a node that has been removed with
1082
* the {@link #removeNode()} method, flushSpi() is invoked on this node,
1083
* but not on others.
1084
*
1085
* @throws BackingStoreException if this operation cannot be completed
1086
* due to a failure in the backing store, or inability to
1087
* communicate with it.
1088
* @see #sync()
1089
*/
1090
public abstract void flush() throws BackingStoreException;
1091
1092
/**
1093
* Ensures that future reads from this preference node and its
1094
* descendants reflect any changes that were committed to the persistent
1095
* store (from any VM) prior to the {@code sync} invocation. As a
1096
* side-effect, forces any changes in the contents of this preference node
1097
* and its descendants to the persistent store, as if the {@code flush}
1098
* method had been invoked on this node.
1099
*
1100
* @throws BackingStoreException if this operation cannot be completed
1101
* due to a failure in the backing store, or inability to
1102
* communicate with it.
1103
* @throws IllegalStateException if this node (or an ancestor) has been
1104
* removed with the {@link #removeNode()} method.
1105
* @see #flush()
1106
*/
1107
public abstract void sync() throws BackingStoreException;
1108
1109
/**
1110
* Registers the specified listener to receive <i>preference change
1111
* events</i> for this preference node. A preference change event is
1112
* generated when a preference is added to this node, removed from this
1113
* node, or when the value associated with a preference is changed.
1114
* (Preference change events are <i>not</i> generated by the {@link
1115
* #removeNode()} method, which generates a <i>node change event</i>.
1116
* Preference change events <i>are</i> generated by the {@code clear}
1117
* method.)
1118
*
1119
* <p>Events are only guaranteed for changes made within the same JVM
1120
* as the registered listener, though some implementations may generate
1121
* events for changes made outside this JVM. Events may be generated
1122
* before the changes have been made persistent. Events are not generated
1123
* when preferences are modified in descendants of this node; a caller
1124
* desiring such events must register with each descendant.
1125
*
1126
* @param pcl The preference change listener to add.
1127
* @throws NullPointerException if {@code pcl} is null.
1128
* @throws IllegalStateException if this node (or an ancestor) has been
1129
* removed with the {@link #removeNode()} method.
1130
* @see #removePreferenceChangeListener(PreferenceChangeListener)
1131
* @see #addNodeChangeListener(NodeChangeListener)
1132
*/
1133
public abstract void addPreferenceChangeListener(
1134
PreferenceChangeListener pcl);
1135
1136
/**
1137
* Removes the specified preference change listener, so it no longer
1138
* receives preference change events.
1139
*
1140
* @param pcl The preference change listener to remove.
1141
* @throws IllegalArgumentException if {@code pcl} was not a registered
1142
* preference change listener on this node.
1143
* @throws IllegalStateException if this node (or an ancestor) has been
1144
* removed with the {@link #removeNode()} method.
1145
* @see #addPreferenceChangeListener(PreferenceChangeListener)
1146
*/
1147
public abstract void removePreferenceChangeListener(
1148
PreferenceChangeListener pcl);
1149
1150
/**
1151
* Registers the specified listener to receive <i>node change events</i>
1152
* for this node. A node change event is generated when a child node is
1153
* added to or removed from this node. (A single {@link #removeNode()}
1154
* invocation results in multiple <i>node change events</i>, one for every
1155
* node in the subtree rooted at the removed node.)
1156
*
1157
* <p>Events are only guaranteed for changes made within the same JVM
1158
* as the registered listener, though some implementations may generate
1159
* events for changes made outside this JVM. Events may be generated
1160
* before the changes have become permanent. Events are not generated
1161
* when indirect descendants of this node are added or removed; a
1162
* caller desiring such events must register with each descendant.
1163
*
1164
* <p>Few guarantees can be made regarding node creation. Because nodes
1165
* are created implicitly upon access, it may not be feasible for an
1166
* implementation to determine whether a child node existed in the backing
1167
* store prior to access (for example, because the backing store is
1168
* unreachable or cached information is out of date). Under these
1169
* circumstances, implementations are neither required to generate node
1170
* change events nor prohibited from doing so.
1171
*
1172
* @param ncl The {@code NodeChangeListener} to add.
1173
* @throws NullPointerException if {@code ncl} is null.
1174
* @throws IllegalStateException if this node (or an ancestor) has been
1175
* removed with the {@link #removeNode()} method.
1176
* @see #removeNodeChangeListener(NodeChangeListener)
1177
* @see #addPreferenceChangeListener(PreferenceChangeListener)
1178
*/
1179
public abstract void addNodeChangeListener(NodeChangeListener ncl);
1180
1181
/**
1182
* Removes the specified {@code NodeChangeListener}, so it no longer
1183
* receives change events.
1184
*
1185
* @param ncl The {@code NodeChangeListener} to remove.
1186
* @throws IllegalArgumentException if {@code ncl} was not a registered
1187
* {@code NodeChangeListener} on this node.
1188
* @throws IllegalStateException if this node (or an ancestor) has been
1189
* removed with the {@link #removeNode()} method.
1190
* @see #addNodeChangeListener(NodeChangeListener)
1191
*/
1192
public abstract void removeNodeChangeListener(NodeChangeListener ncl);
1193
1194
/**
1195
* Emits on the specified output stream an XML document representing all
1196
* of the preferences contained in this node (but not its descendants).
1197
* This XML document is, in effect, an offline backup of the node.
1198
*
1199
* <p>The XML document will have the following DOCTYPE declaration:
1200
* <pre>{@code
1201
* <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
1202
* }</pre>
1203
* The UTF-8 character encoding will be used.
1204
*
1205
* <p>This method is an exception to the general rule that the results of
1206
* concurrently executing multiple methods in this class yields
1207
* results equivalent to some serial execution. If the preferences
1208
* at this node are modified concurrently with an invocation of this
1209
* method, the exported preferences comprise a "fuzzy snapshot" of the
1210
* preferences contained in the node; some of the concurrent modifications
1211
* may be reflected in the exported data while others may not.
1212
*
1213
* @param os the output stream on which to emit the XML document.
1214
* @throws IOException if writing to the specified output stream
1215
* results in an {@code IOException}.
1216
* @throws BackingStoreException if preference data cannot be read from
1217
* backing store.
1218
* @see #importPreferences(InputStream)
1219
* @throws IllegalStateException if this node (or an ancestor) has been
1220
* removed with the {@link #removeNode()} method.
1221
*/
1222
public abstract void exportNode(OutputStream os)
1223
throws IOException, BackingStoreException;
1224
1225
/**
1226
* Emits an XML document representing all of the preferences contained
1227
* in this node and all of its descendants. This XML document is, in
1228
* effect, an offline backup of the subtree rooted at the node.
1229
*
1230
* <p>The XML document will have the following DOCTYPE declaration:
1231
* <pre>{@code
1232
* <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
1233
* }</pre>
1234
* The UTF-8 character encoding will be used.
1235
*
1236
* <p>This method is an exception to the general rule that the results of
1237
* concurrently executing multiple methods in this class yields
1238
* results equivalent to some serial execution. If the preferences
1239
* or nodes in the subtree rooted at this node are modified concurrently
1240
* with an invocation of this method, the exported preferences comprise a
1241
* "fuzzy snapshot" of the subtree; some of the concurrent modifications
1242
* may be reflected in the exported data while others may not.
1243
*
1244
* @param os the output stream on which to emit the XML document.
1245
* @throws IOException if writing to the specified output stream
1246
* results in an {@code IOException}.
1247
* @throws BackingStoreException if preference data cannot be read from
1248
* backing store.
1249
* @throws IllegalStateException if this node (or an ancestor) has been
1250
* removed with the {@link #removeNode()} method.
1251
* @see #importPreferences(InputStream)
1252
* @see #exportNode(OutputStream)
1253
*/
1254
public abstract void exportSubtree(OutputStream os)
1255
throws IOException, BackingStoreException;
1256
1257
/**
1258
* Imports all of the preferences represented by the XML document on the
1259
* specified input stream. The document may represent user preferences or
1260
* system preferences. If it represents user preferences, the preferences
1261
* will be imported into the calling user's preference tree (even if they
1262
* originally came from a different user's preference tree). If any of
1263
* the preferences described by the document inhabit preference nodes that
1264
* do not exist, the nodes will be created.
1265
*
1266
* <p>The XML document must have the following DOCTYPE declaration:
1267
* <pre>{@code
1268
* <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
1269
* }</pre>
1270
* (This method is designed for use in conjunction with
1271
* {@link #exportNode(OutputStream)} and
1272
* {@link #exportSubtree(OutputStream)}.
1273
*
1274
* <p>This method is an exception to the general rule that the results of
1275
* concurrently executing multiple methods in this class yields
1276
* results equivalent to some serial execution. The method behaves
1277
* as if implemented on top of the other public methods in this class,
1278
* notably {@link #node(String)} and {@link #put(String, String)}.
1279
*
1280
* @param is the input stream from which to read the XML document.
1281
* @throws IOException if reading from the specified input stream
1282
* results in an {@code IOException}.
1283
* @throws InvalidPreferencesFormatException Data on input stream does not
1284
* constitute a valid XML document with the mandated document type.
1285
* @throws SecurityException If a security manager is present and
1286
* it denies {@code RuntimePermission("preferences")}.
1287
* @see RuntimePermission
1288
*/
1289
public static void importPreferences(InputStream is)
1290
throws IOException, InvalidPreferencesFormatException
1291
{
1292
XmlSupport.importPreferences(is);
1293
}
1294
}
1295
1296