Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/file/FileSystems.java
41159 views
1
/*
2
* Copyright (c) 2007, 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.nio.file;
27
28
import java.nio.file.spi.FileSystemProvider;
29
import java.net.URI;
30
import java.io.IOException;
31
import java.security.AccessController;
32
import java.security.PrivilegedAction;
33
import java.lang.reflect.Constructor;
34
import java.util.Collections;
35
import java.util.Map;
36
import java.util.ServiceConfigurationError;
37
import java.util.ServiceLoader;
38
39
import jdk.internal.misc.VM;
40
import sun.nio.fs.DefaultFileSystemProvider;
41
42
/**
43
* Factory methods for file systems. This class defines the {@link #getDefault
44
* getDefault} method to get the default file system and factory methods to
45
* construct other types of file systems.
46
*
47
* <p> The first invocation of any of the methods defined by this class causes
48
* the default {@link FileSystemProvider provider} to be loaded. The default
49
* provider, identified by the URI scheme "file", creates the {@link FileSystem}
50
* that provides access to the file systems accessible to the Java virtual
51
* machine. If the process of loading or initializing the default provider fails
52
* then an unspecified error is thrown.
53
*
54
* <p> The first invocation of the {@link FileSystemProvider#installedProviders()
55
* installedProviders} method, by way of invoking any of the {@code
56
* newFileSystem} methods defined by this class, locates and loads all
57
* installed file system providers. Installed providers are loaded using the
58
* service-provider loading facility defined by the {@link ServiceLoader} class.
59
* Installed providers are loaded using the system class loader. If the
60
* system class loader cannot be found then the platform class loader is used.
61
* Providers are typically installed by placing them in a JAR file on the
62
* application class path, the JAR file contains a
63
* provider-configuration file named {@code java.nio.file.spi.FileSystemProvider}
64
* in the resource directory {@code META-INF/services}, and the file lists one or
65
* more fully-qualified names of concrete subclass of {@link FileSystemProvider}
66
* that have a zero argument constructor.
67
* The ordering that installed providers are located is implementation specific.
68
* If a provider is instantiated and its {@link FileSystemProvider#getScheme()
69
* getScheme} returns the same URI scheme of a provider that was previously
70
* instantiated then the most recently instantiated duplicate is discarded. URI
71
* schemes are compared without regard to case. During construction a provider
72
* may safely access files associated with the default provider but care needs
73
* to be taken to avoid circular loading of other installed providers. If
74
* circular loading of installed providers is detected then an unspecified error
75
* is thrown.
76
*
77
* <p> This class also defines factory methods that allow a {@link ClassLoader}
78
* to be specified when locating a provider. As with installed providers, the
79
* provider classes are identified by placing the provider configuration file
80
* in the resource directory {@code META-INF/services}.
81
*
82
* <p> If a thread initiates the loading of the installed file system providers
83
* and another thread invokes a method that also attempts to load the providers
84
* then the method will block until the loading completes.
85
*
86
* @since 1.7
87
*/
88
89
public final class FileSystems {
90
private FileSystems() { }
91
92
// lazy initialization of default file system
93
private static class DefaultFileSystemHolder {
94
static final FileSystem defaultFileSystem = defaultFileSystem();
95
96
// returns default file system
97
private static FileSystem defaultFileSystem() {
98
// load default provider
99
@SuppressWarnings("removal")
100
FileSystemProvider provider = AccessController
101
.doPrivileged(new PrivilegedAction<>() {
102
public FileSystemProvider run() {
103
return getDefaultProvider();
104
}
105
});
106
107
// return file system
108
return provider.getFileSystem(URI.create("file:///"));
109
}
110
111
// returns default provider
112
private static FileSystemProvider getDefaultProvider() {
113
// start with the platform's default file system provider
114
FileSystemProvider provider = DefaultFileSystemProvider.instance();
115
116
// if the property java.nio.file.spi.DefaultFileSystemProvider is
117
// set then its value is the name of the default provider (or a list)
118
String prop = "java.nio.file.spi.DefaultFileSystemProvider";
119
String propValue = System.getProperty(prop);
120
if (propValue != null) {
121
for (String cn: propValue.split(",")) {
122
try {
123
Class<?> c = Class
124
.forName(cn, true, ClassLoader.getSystemClassLoader());
125
Constructor<?> ctor = c
126
.getDeclaredConstructor(FileSystemProvider.class);
127
provider = (FileSystemProvider)ctor.newInstance(provider);
128
129
// must be "file"
130
if (!provider.getScheme().equals("file"))
131
throw new Error("Default provider must use scheme 'file'");
132
133
} catch (Exception x) {
134
throw new Error(x);
135
}
136
}
137
}
138
return provider;
139
}
140
}
141
142
/**
143
* Returns the default {@code FileSystem}. The default file system creates
144
* objects that provide access to the file systems accessible to the Java
145
* virtual machine. The <em>working directory</em> of the file system is
146
* the current user directory, named by the system property {@code user.dir}.
147
* This allows for interoperability with the {@link java.io.File java.io.File}
148
* class.
149
*
150
* <p> The first invocation of any of the methods defined by this class
151
* locates the default {@link FileSystemProvider provider} object. Where the
152
* system property {@code java.nio.file.spi.DefaultFileSystemProvider} is
153
* not defined then the default provider is a system-default provider that
154
* is invoked to create the default file system.
155
*
156
* <p> If the system property {@code java.nio.file.spi.DefaultFileSystemProvider}
157
* is defined then it is taken to be a list of one or more fully-qualified
158
* names of concrete provider classes identified by the URI scheme
159
* {@code "file"}. Where the property is a list of more than one name then
160
* the names are separated by a comma. Each class is loaded, using the system
161
* class loader, and instantiated by invoking a one argument constructor
162
* whose formal parameter type is {@code FileSystemProvider}. The providers
163
* are loaded and instantiated in the order they are listed in the property.
164
* If this process fails or a provider's scheme is not equal to {@code "file"}
165
* then an unspecified error is thrown. URI schemes are normally compared
166
* without regard to case but for the default provider, the scheme is
167
* required to be {@code "file"}. The first provider class is instantiated
168
* by invoking it with a reference to the system-default provider.
169
* The second provider class is instantiated by invoking it with a reference
170
* to the first provider instance. The third provider class is instantiated
171
* by invoking it with a reference to the second instance, and so on. The
172
* last provider to be instantiated becomes the default provider; its {@code
173
* getFileSystem} method is invoked with the URI {@code "file:///"} to
174
* get a reference to the default file system.
175
*
176
* <p> Subsequent invocations of this method return the file system that was
177
* returned by the first invocation.
178
*
179
* @return the default file system
180
*/
181
public static FileSystem getDefault() {
182
if (VM.isModuleSystemInited()) {
183
return DefaultFileSystemHolder.defaultFileSystem;
184
} else {
185
// always use the platform's default file system during startup
186
return DefaultFileSystemProvider.theFileSystem();
187
}
188
}
189
190
/**
191
* Returns a reference to an existing {@code FileSystem}.
192
*
193
* <p> This method iterates over the {@link FileSystemProvider#installedProviders()
194
* installed} providers to locate the provider that is identified by the URI
195
* {@link URI#getScheme scheme} of the given URI. URI schemes are compared
196
* without regard to case. The exact form of the URI is highly provider
197
* dependent. If found, the provider's {@link FileSystemProvider#getFileSystem
198
* getFileSystem} method is invoked to obtain a reference to the {@code
199
* FileSystem}.
200
*
201
* <p> Once a file system created by this provider is {@link FileSystem#close
202
* closed} it is provider-dependent if this method returns a reference to
203
* the closed file system or throws {@link FileSystemNotFoundException}.
204
* If the provider allows a new file system to be created with the same URI
205
* as a file system it previously created then this method throws the
206
* exception if invoked after the file system is closed (and before a new
207
* instance is created by the {@link #newFileSystem newFileSystem} method).
208
*
209
* <p> If a security manager is installed then a provider implementation
210
* may require to check a permission before returning a reference to an
211
* existing file system. In the case of the {@link FileSystems#getDefault
212
* default} file system, no permission check is required.
213
*
214
* @param uri the URI to locate the file system
215
*
216
* @return the reference to the file system
217
*
218
* @throws IllegalArgumentException
219
* if the pre-conditions for the {@code uri} parameter are not met
220
* @throws FileSystemNotFoundException
221
* if the file system, identified by the URI, does not exist
222
* @throws ProviderNotFoundException
223
* if a provider supporting the URI scheme is not installed
224
* @throws SecurityException
225
* if a security manager is installed and it denies an unspecified
226
* permission
227
*/
228
public static FileSystem getFileSystem(URI uri) {
229
String scheme = uri.getScheme();
230
if (scheme == null) {
231
throw new IllegalArgumentException(uri.toString());
232
}
233
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
234
if (scheme.equalsIgnoreCase(provider.getScheme())) {
235
return provider.getFileSystem(uri);
236
}
237
}
238
throw new ProviderNotFoundException("Provider \"" + scheme + "\" not found");
239
}
240
241
/**
242
* Constructs a new file system that is identified by a {@link URI}
243
*
244
* <p> This method iterates over the {@link FileSystemProvider#installedProviders()
245
* installed} providers to locate the provider that is identified by the URI
246
* {@link URI#getScheme scheme} of the given URI. URI schemes are compared
247
* without regard to case. The exact form of the URI is highly provider
248
* dependent. If found, the provider's {@link FileSystemProvider#newFileSystem(URI,Map)
249
* newFileSystem(URI,Map)} method is invoked to construct the new file system.
250
*
251
* <p> Once a file system is {@link FileSystem#close closed} it is
252
* provider-dependent if the provider allows a new file system to be created
253
* with the same URI as a file system it previously created.
254
*
255
* <p> <b>Usage Example:</b>
256
* Suppose there is a provider identified by the scheme {@code "memory"}
257
* installed:
258
* <pre>
259
* FileSystem fs = FileSystems.newFileSystem(URI.create("memory:///?name=logfs"),
260
* Map.of("capacity", "16G", "blockSize", "4k"));
261
* </pre>
262
*
263
* @param uri
264
* the URI identifying the file system
265
* @param env
266
* a map of provider specific properties to configure the file system;
267
* may be empty
268
*
269
* @return a new file system
270
*
271
* @throws IllegalArgumentException
272
* if the pre-conditions for the {@code uri} parameter are not met,
273
* or the {@code env} parameter does not contain properties required
274
* by the provider, or a property value is invalid
275
* @throws FileSystemAlreadyExistsException
276
* if the file system has already been created
277
* @throws ProviderNotFoundException
278
* if a provider supporting the URI scheme is not installed
279
* @throws IOException
280
* if an I/O error occurs creating the file system
281
* @throws SecurityException
282
* if a security manager is installed and it denies an unspecified
283
* permission required by the file system provider implementation
284
*/
285
public static FileSystem newFileSystem(URI uri, Map<String,?> env)
286
throws IOException
287
{
288
return newFileSystem(uri, env, null);
289
}
290
291
/**
292
* Constructs a new file system that is identified by a {@link URI}
293
*
294
* <p> This method first attempts to locate an installed provider in exactly
295
* the same manner as the {@link #newFileSystem(URI,Map) newFileSystem(URI,Map)}
296
* method. If none of the installed providers support the URI scheme then an
297
* attempt is made to locate the provider using the given class loader. If a
298
* provider supporting the URI scheme is located then its {@link
299
* FileSystemProvider#newFileSystem(URI,Map) newFileSystem(URI,Map)} is
300
* invoked to construct the new file system.
301
*
302
* @param uri
303
* the URI identifying the file system
304
* @param env
305
* a map of provider specific properties to configure the file system;
306
* may be empty
307
* @param loader
308
* the class loader to locate the provider or {@code null} to only
309
* attempt to locate an installed provider
310
*
311
* @return a new file system
312
*
313
* @throws IllegalArgumentException
314
* if the pre-conditions for the {@code uri} parameter are not met,
315
* or the {@code env} parameter does not contain properties required
316
* by the provider, or a property value is invalid
317
* @throws FileSystemAlreadyExistsException
318
* if the URI scheme identifies an installed provider and the file
319
* system has already been created
320
* @throws ProviderNotFoundException
321
* if a provider supporting the URI scheme is not found
322
* @throws ServiceConfigurationError
323
* when an error occurs while loading a service provider
324
* @throws IOException
325
* an I/O error occurs creating the file system
326
* @throws SecurityException
327
* if a security manager is installed and it denies an unspecified
328
* permission required by the file system provider implementation
329
*/
330
public static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader)
331
throws IOException
332
{
333
String scheme = uri.getScheme();
334
335
// check installed providers
336
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
337
if (scheme.equalsIgnoreCase(provider.getScheme())) {
338
try {
339
return provider.newFileSystem(uri, env);
340
} catch (UnsupportedOperationException uoe) {
341
}
342
}
343
}
344
345
// if not found, use service-provider loading facility
346
if (loader != null) {
347
ServiceLoader<FileSystemProvider> sl = ServiceLoader
348
.load(FileSystemProvider.class, loader);
349
for (FileSystemProvider provider : sl) {
350
if (scheme.equalsIgnoreCase(provider.getScheme())) {
351
try {
352
return provider.newFileSystem(uri, env);
353
} catch (UnsupportedOperationException uoe) {
354
}
355
}
356
}
357
}
358
359
throw new ProviderNotFoundException("Provider \"" + scheme + "\" not found");
360
}
361
362
/**
363
* Constructs a new {@code FileSystem} to access the contents of a file as a
364
* file system.
365
*
366
* <p> This method makes use of specialized providers that create pseudo file
367
* systems where the contents of one or more files is treated as a file
368
* system.
369
*
370
* <p> This method first attempts to locate an installed provider in exactly
371
* the same manner as the {@link #newFileSystem(Path, Map, ClassLoader)
372
* newFileSystem(Path, Map, ClassLoader)} method with an empty map. If none
373
* of the installed providers return a {@code FileSystem} then an attempt is
374
* made to locate the provider using the given class loader. If a provider
375
* returns a file system then the lookup terminates and the file system is
376
* returned.
377
*
378
* @param path
379
* the path to the file
380
* @param loader
381
* the class loader to locate the provider or {@code null} to only
382
* attempt to locate an installed provider
383
*
384
* @return a new file system
385
*
386
* @throws ProviderNotFoundException
387
* if a provider supporting this file type cannot be located
388
* @throws ServiceConfigurationError
389
* when an error occurs while loading a service provider
390
* @throws IOException
391
* if an I/O error occurs
392
* @throws SecurityException
393
* if a security manager is installed and it denies an unspecified
394
* permission
395
*/
396
public static FileSystem newFileSystem(Path path,
397
ClassLoader loader)
398
throws IOException
399
{
400
return newFileSystem(path, Map.of(), loader);
401
}
402
403
/**
404
* Constructs a new {@code FileSystem} to access the contents of a file as a
405
* file system.
406
*
407
* <p> This method makes use of specialized providers that create pseudo file
408
* systems where the contents of one or more files is treated as a file
409
* system.
410
*
411
* <p> This method first attempts to locate an installed provider in exactly
412
* the same manner as the {@link #newFileSystem(Path,Map,ClassLoader)
413
* newFileSystem(Path, Map, ClassLoader)} method. If found, the provider's
414
* {@link FileSystemProvider#newFileSystem(Path, Map) newFileSystem(Path, Map)}
415
* method is invoked to construct the new file system.
416
*
417
* @param path
418
* the path to the file
419
* @param env
420
* a map of provider specific properties to configure the file system;
421
* may be empty
422
*
423
* @return a new file system
424
*
425
* @throws ProviderNotFoundException
426
* if a provider supporting this file type cannot be located
427
* @throws ServiceConfigurationError
428
* when an error occurs while loading a service provider
429
* @throws IOException
430
* if an I/O error occurs
431
* @throws SecurityException
432
* if a security manager is installed and it denies an unspecified
433
* permission
434
*
435
* @since 13
436
*/
437
public static FileSystem newFileSystem(Path path, Map<String,?> env)
438
throws IOException
439
{
440
return newFileSystem(path, env, null);
441
}
442
443
/**
444
* Constructs a new {@code FileSystem} to access the contents of a file as a
445
* file system.
446
*
447
* <p> This method makes use of specialized providers that create pseudo file
448
* systems where the contents of one or more files is treated as a file
449
* system.
450
*
451
* <p> This method first attempts to locate an installed provider in exactly
452
* the same manner as the {@link #newFileSystem(Path,Map,ClassLoader)
453
* newFileSystem(Path, Map, ClassLoader)} method. If found, the provider's
454
* {@link FileSystemProvider#newFileSystem(Path, Map) newFileSystem(Path, Map)}
455
* method is invoked with an empty map to construct the new file system.
456
*
457
* @param path
458
* the path to the file
459
*
460
* @return a new file system
461
*
462
* @throws ProviderNotFoundException
463
* if a provider supporting this file type cannot be located
464
* @throws ServiceConfigurationError
465
* when an error occurs while loading a service provider
466
* @throws IOException
467
* if an I/O error occurs
468
* @throws SecurityException
469
* if a security manager is installed and it denies an unspecified
470
* permission
471
*
472
* @since 13
473
*/
474
public static FileSystem newFileSystem(Path path) throws IOException {
475
return newFileSystem(path, Map.of(), null);
476
}
477
478
/**
479
* Constructs a new {@code FileSystem} to access the contents of a file as a
480
* file system.
481
*
482
* <p> This method makes use of specialized providers that create pseudo file
483
* systems where the contents of one or more files is treated as a file
484
* system.
485
*
486
* <p> This method iterates over the {@link FileSystemProvider#installedProviders()
487
* installed} providers. It invokes, in turn, each provider's {@link
488
* FileSystemProvider#newFileSystem(Path,Map) newFileSystem(Path,Map)}
489
* method. If a provider returns a file system then the iteration
490
* terminates and the file system is returned.
491
* If none of the installed providers return a {@code FileSystem} then
492
* an attempt is made to locate the provider using the given class loader.
493
* If a provider returns a file
494
* system, then the lookup terminates and the file system is returned.
495
*
496
* @param path
497
* the path to the file
498
* @param env
499
* a map of provider specific properties to configure the file system;
500
* may be empty
501
* @param loader
502
* the class loader to locate the provider or {@code null} to only
503
* attempt to locate an installed provider
504
*
505
* @return a new file system
506
*
507
* @throws ProviderNotFoundException
508
* if a provider supporting this file type cannot be located
509
* @throws ServiceConfigurationError
510
* when an error occurs while loading a service provider
511
* @throws IOException
512
* if an I/O error occurs
513
* @throws SecurityException
514
* if a security manager is installed and it denies an unspecified
515
* permission
516
*
517
* @since 13
518
*/
519
public static FileSystem newFileSystem(Path path, Map<String,?> env,
520
ClassLoader loader)
521
throws IOException
522
{
523
if (path == null)
524
throw new NullPointerException();
525
// check installed providers
526
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
527
try {
528
return provider.newFileSystem(path, env);
529
} catch (UnsupportedOperationException uoe) {
530
}
531
}
532
533
// if not found, use service-provider loading facility
534
if (loader != null) {
535
ServiceLoader<FileSystemProvider> sl = ServiceLoader
536
.load(FileSystemProvider.class, loader);
537
for (FileSystemProvider provider: sl) {
538
try {
539
return provider.newFileSystem(path, env);
540
} catch (UnsupportedOperationException uoe) {
541
}
542
}
543
}
544
545
throw new ProviderNotFoundException("Provider not found");
546
}
547
}
548
549