-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.sh
executable file
·640 lines (582 loc) · 27.4 KB
/
generate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/bin/bash
set -eu
SDK_PATH="$(sed -En 's/^\s*sdk.dir=(.*)$/\1/p' local.properties)"
SDK_VERSION="$(sed -En 's/^\s*compileSdk\s+([0-9]+)\s*$/\1/p' library/build.gradle)"
SDK_JAVA_SOURCE_ROOT="${SDK_PATH}/sources/android-${SDK_VERSION}"
LIBRARY_JAVA_SOURCE_ROOT="library/src/main/java"
mkdir -p "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file"
rm -rf "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file"
cp -r "${SDK_JAVA_SOURCE_ROOT}/java/nio/file" "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file"
find "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file" -iname '*.java' -type f -print0 | xargs -0 sed -Ei \
-e 's/\bjava\.nio\.file\b/java8.nio.file/g' \
-e '/^\s*import(\s+static)?\s+sun\..+\s*;\s*$/d' \
mkdir -p "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels"
rm -rf "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels"
mkdir "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels"
cp "${SDK_JAVA_SOURCE_ROOT}/java/nio/channels/SeekableByteChannel.java" "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels/SeekableByteChannel.java"
sed -Ei \
-e "s/^(\s*package\s+)java(\.nio\.channels\s*;\s*)$/\1java8\2/" \
-e '/^\s*import\s+java\.nio\.ByteBuffer\s*;\s*$/a\import java.nio.channels.*;' \
"${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels/SeekableByteChannel.java"
cat >"${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels/FileChannel.java" <<EOF
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java8.nio.channels;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java8.nio.file.OpenOption;
import java8.nio.file.Path;
import java8.nio.file.StandardOpenOption;
import java8.nio.file.attribute.FileAttribute;
import java8.nio.file.spi.FileSystemProvider;
public abstract class FileChannel extends java.nio.channels.FileChannel
implements SeekableByteChannel {
protected FileChannel() {}
/**
* Opens or creates a file, returning a file channel to access the file.
*
* <p> The {@code options} parameter determines how the file is opened.
* The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE
* WRITE} options determine if the file should be opened for reading and/or
* writing. If neither option (or the {@link StandardOpenOption#APPEND APPEND}
* option) is contained in the array then the file is opened for reading.
* By default reading or writing commences at the beginning of the file.
*
* <p> In the addition to {@code READ} and {@code WRITE}, the following
* options may be present:
*
* <table border=1 cellpadding=5 summary="">
* <tr> <th>Option</th> <th>Description</th> </tr>
* <tr>
* <td> {@link StandardOpenOption#APPEND APPEND} </td>
* <td> If this option is present then the file is opened for writing and
* each invocation of the channel's {@code write} method first advances
* the position to the end of the file and then writes the requested
* data. Whether the advancement of the position and the writing of the
* data are done in a single atomic operation is system-dependent and
* therefore unspecified. This option may not be used in conjunction
* with the {@code READ} or {@code TRUNCATE_EXISTING} options. </td>
* </tr>
* <tr>
* <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td>
* <td> If this option is present then the existing file is truncated to
* a size of 0 bytes. This option is ignored when the file is opened only
* for reading. </td>
* </tr>
* <tr>
* <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td>
* <td> If this option is present then a new file is created, failing if
* the file already exists. When creating a file the check for the
* existence of the file and the creation of the file if it does not exist
* is atomic with respect to other file system operations. This option is
* ignored when the file is opened only for reading. </td>
* </tr>
* <tr>
* <td > {@link StandardOpenOption#CREATE CREATE} </td>
* <td> If this option is present then an existing file is opened if it
* exists, otherwise a new file is created. When creating a file the check
* for the existence of the file and the creation of the file if it does
* not exist is atomic with respect to other file system operations. This
* option is ignored if the {@code CREATE_NEW} option is also present or
* the file is opened only for reading. </td>
* </tr>
* <tr>
* <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td>
* <td> When this option is present then the implementation makes a
* <em>best effort</em> attempt to delete the file when closed by the
* the {@link #close close} method. If the {@code close} method is not
* invoked then a <em>best effort</em> attempt is made to delete the file
* when the Java virtual machine terminates. </td>
* </tr>
* <tr>
* <td>{@link StandardOpenOption#SPARSE SPARSE} </td>
* <td> When creating a new file this option is a <em>hint</em> that the
* new file will be sparse. This option is ignored when not creating
* a new file. </td>
* </tr>
* <tr>
* <td> {@link StandardOpenOption#SYNC SYNC} </td>
* <td> Requires that every update to the file's content or metadata be
* written synchronously to the underlying storage device. (see <a
* href="../file/package-summary.html#integrity"> Synchronized I/O file
* integrity</a>). </td>
* </tr>
* <tr>
* <td> {@link StandardOpenOption#DSYNC DSYNC} </td>
* <td> Requires that every update to the file's content be written
* synchronously to the underlying storage device. (see <a
* href="../file/package-summary.html#integrity"> Synchronized I/O file
* integrity</a>). </td>
* </tr>
* </table>
*
* <p> An implementation may also support additional options.
*
* <p> The {@code attrs} parameter is an optional array of file {@link
* FileAttribute file-attributes} to set atomically when creating the file.
*
* <p> The new channel is created by invoking the {@link
* FileSystemProvider#newFileChannel newFileChannel} method on the
* provider that created the {@code Path}.
*
* @param path
* The path of the file to open or create
* @param options
* Options specifying how the file is opened
* @param attrs
* An optional list of file attributes to set atomically when
* creating the file
*
* @return A new file channel
*
* @throws IllegalArgumentException
* If the set contains an invalid combination of options
* @throws UnsupportedOperationException
* If the {@code path} is associated with a provider that does not
* support creating file channels, or an unsupported open option is
* specified, or the array contains an attribute that cannot be set
* atomically when creating the file
* @throws IOException
* If an I/O error occurs
* @throws SecurityException
* If a security manager is installed and it denies an
* unspecified permission required by the implementation.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String)} method is invoked to check
* read access if the file is opened for reading. The {@link
* SecurityManager#checkWrite(String)} method is invoked to check
* write access if the file is opened for writing
*
* @since 1.7
*/
public static FileChannel open(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
FileSystemProvider provider = path.getFileSystem().provider();
return provider.newFileChannel(path, options, attrs);
}
@SuppressWarnings({"unchecked", "rawtypes"}) // generic array construction
private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];
/**
* Opens or creates a file, returning a file channel to access the file.
*
* <p> An invocation of this method behaves in exactly the same way as the
* invocation
* <pre>
* fc.{@link #open(Path,Set,FileAttribute[]) open}(file, opts, new FileAttribute<?>[0]);
* </pre>
* where {@code opts} is a set of the options specified in the {@code
* options} array.
*
* @param path
* The path of the file to open or create
* @param options
* Options specifying how the file is opened
*
* @return A new file channel
*
* @throws IllegalArgumentException
* If the set contains an invalid combination of options
* @throws UnsupportedOperationException
* If the {@code path} is associated with a provider that does not
* support creating file channels, or an unsupported open option is
* specified
* @throws IOException
* If an I/O error occurs
* @throws SecurityException
* If a security manager is installed and it denies an
* unspecified permission required by the implementation.
* In the case of the default provider, the {@link
* SecurityManager#checkRead(String)} method is invoked to check
* read access if the file is opened for reading. The {@link
* SecurityManager#checkWrite(String)} method is invoked to check
* write access if the file is opened for writing
*
* @since 1.7
*/
public static FileChannel open(Path path, OpenOption... options)
throws IOException
{
Set<OpenOption> set = new HashSet<OpenOption>(options.length);
Collections.addAll(set, options);
return open(path, set, NO_ATTRIBUTES);
}
@Override
public abstract FileChannel position(long newPosition) throws IOException;
@Override
public abstract FileChannel truncate(long size) throws IOException;
}
EOF
cat >"${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/channels/FileChannels.java" <<EOF
/*
* Copyright (c) 2018 Hai Zhang <[email protected]>
* All Rights Reserved.
*/
package java8.nio.channels;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
public class FileChannels {
private FileChannels() {}
public static FileChannel from(java.nio.channels.FileChannel fileChannel) {
return new DelegateFileChannel(fileChannel);
}
private static class DelegateFileChannel extends FileChannel {
private final java.nio.channels.FileChannel mFileChannel;
public DelegateFileChannel(java.nio.channels.FileChannel fileChannel) {
mFileChannel = fileChannel;
}
@Override
public int read(ByteBuffer dst) throws IOException {
return mFileChannel.read(dst);
}
@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
return mFileChannel.read(dsts, offset, length);
}
@Override
public int write(ByteBuffer src) throws IOException {
return mFileChannel.write(src);
}
@Override
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
return mFileChannel.write(srcs, offset, length);
}
@Override
public long position() throws IOException {
return mFileChannel.position();
}
@Override
public DelegateFileChannel position(long newPosition) throws IOException {
mFileChannel.position(newPosition);
return this;
}
@Override
public long size() throws IOException {
return mFileChannel.size();
}
@Override
public DelegateFileChannel truncate(long size) throws IOException {
mFileChannel.truncate(size);
return this;
}
public void force(boolean metaData) throws IOException {
mFileChannel.force(metaData);
}
public long transferTo(long position, long count, WritableByteChannel target)
throws IOException {
return mFileChannel.transferTo(position, count, target);
}
public long transferFrom(ReadableByteChannel src, long position, long count)
throws IOException {
return mFileChannel.transferFrom(src, position, count);
}
public int read(ByteBuffer dst, long position) throws IOException {
return mFileChannel.read(dst, position);
}
public int write(ByteBuffer src, long position) throws IOException {
return mFileChannel.write(src, position);
}
public MappedByteBuffer map(MapMode mode, long position, long size)
throws IOException {
return mFileChannel.map(mode, position, size);
}
public FileLock lock(long position, long size, boolean shared) throws IOException {
return mFileChannel.lock(position, size, shared);
}
public FileLock tryLock(long position, long size, boolean shared) throws IOException {
return mFileChannel.tryLock(position, size, shared);
}
@Override
protected void implCloseChannel() throws IOException {
mFileChannel.close();
}
}
}
EOF
find "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file" -iname '*.java' -type f -print0 | xargs -0 sed -Ei \
-e "s/\bjava(\.nio\.channels\.(File|SeekableByte)Channel)\b/java8\1/g" \
-e "/^\s*import\s+java\.nio\.channels\.\*\s*;\s*$/a\import java8.nio.channels.FileChannel;\nimport java8.nio.channels.SeekableByteChannel;"
mkdir -p "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/charset"
rm -rf "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/charset"
mkdir "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/charset"
cp "${SDK_JAVA_SOURCE_ROOT}/java/nio/charset/StandardCharsets.java" "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/charset/StandardCharsets.java"
sed -Ei \
-e "s/^(\s*package\s+)java(\.nio\.charset\s*;\s*)$/\1java8\2\n\nimport java.nio.charset.*;/" \
"${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/charset/StandardCharsets.java"
find "${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file" -iname '*.java' -type f -print0 | xargs -0 sed -Ei \
-e 's/\bjava(\.nio\.charset\.StandardCharsets)\b/java8\1/g'
git apply <<EOF
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileSystems.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileSystems.java
index 432f013..cfdaacb 100644
--- a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileSystems.java
+++ b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileSystems.java
@@ -105,31 +105,7 @@ public final class FileSystems {
// returns default provider
private static FileSystemProvider getDefaultProvider() {
- FileSystemProvider provider = sun.nio.fs.DefaultFileSystemProvider.create();
-
- // if the property java8.nio.file.spi.DefaultFileSystemProvider is
- // set then its value is the name of the default provider (or a list)
- String propValue = System
- .getProperty("java8.nio.file.spi.DefaultFileSystemProvider");
- if (propValue != null) {
- for (String cn: propValue.split(",")) {
- try {
- Class<?> c = Class
- .forName(cn, true, ClassLoader.getSystemClassLoader());
- Constructor<?> ctor = c
- .getDeclaredConstructor(FileSystemProvider.class);
- provider = (FileSystemProvider)ctor.newInstance(provider);
-
- // must be "file"
- if (!provider.getScheme().equals("file"))
- throw new Error("Default provider must use scheme 'file'");
-
- } catch (Exception x) {
- throw new Error(x);
- }
- }
- }
- return provider;
+ return FileSystemProvider.defaultProvider();
}
}
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileTreeWalker.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileTreeWalker.java
index 1897e18..82d8a45 100644
--- a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileTreeWalker.java
+++ b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/FileTreeWalker.java
@@ -200,17 +200,6 @@ class FileTreeWalker implements Closeable {
private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
throws IOException
{
- // if attributes are cached then use them if possible
- if (canUseCached &&
- (file instanceof BasicFileAttributesHolder) &&
- (System.getSecurityManager() == null))
- {
- BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
- if (cached != null && (!followLinks || !cached.isSymbolicLink())) {
- return cached;
- }
- }
-
// attempt to get attributes of file. If fails and we are following
// links then a link target might not exist so get attributes of link
BasicFileAttributes attrs;
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/Files.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/Files.java
index cc80aa7..4695b94 100644
--- a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/Files.java
+++ b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/Files.java
@@ -1531,35 +1531,12 @@ public final class Files {
return provider(path).isHidden(path);
}
- // lazy loading of default and installed file type detectors
- private static class FileTypeDetectors{
- static final FileTypeDetector defaultFileTypeDetector =
- createDefaultFileTypeDetector();
- static final List<FileTypeDetector> installeDetectors =
- loadInstalledDetectors();
-
- // creates the default file type detector
- private static FileTypeDetector createDefaultFileTypeDetector() {
- return AccessController
- .doPrivileged(new PrivilegedAction<FileTypeDetector>() {
- @Override public FileTypeDetector run() {
- return sun.nio.fs.DefaultFileTypeDetector.create();
- }});
- }
+ private static final List<FileTypeDetector> installedDetectors = new ArrayList<>();
+ private static final Object installedDetectorsLock = new Object();
- // loads all installed file type detectors
- private static List<FileTypeDetector> loadInstalledDetectors() {
- return AccessController
- .doPrivileged(new PrivilegedAction<List<FileTypeDetector>>() {
- @Override public List<FileTypeDetector> run() {
- List<FileTypeDetector> list = new ArrayList<>();
- ServiceLoader<FileTypeDetector> loader = ServiceLoader
- .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader());
- for (FileTypeDetector detector: loader) {
- list.add(detector);
- }
- return list;
- }});
+ public static void installFileTypeDetector(FileTypeDetector detector) {
+ synchronized (installedDetectorsLock) {
+ installedDetectors.add(detector);
}
}
@@ -1614,14 +1591,14 @@ public final class Files {
throws IOException
{
// try installed file type detectors
- for (FileTypeDetector detector: FileTypeDetectors.installeDetectors) {
+ for (FileTypeDetector detector: installedDetectors) {
String result = detector.probeContentType(path);
if (result != null)
return result;
}
// fallback to default
- return FileTypeDetectors.defaultFileTypeDetector.probeContentType(path);
+ return null;
}
// -- File Attributes --
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/TempFileHelper.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/TempFileHelper.java
index 5ea48f2..cfb83a7 100644
--- a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/TempFileHelper.java
+++ b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/TempFileHelper.java
@@ -46,7 +46,7 @@ class TempFileHelper {
// temporary directory location
private static final Path tmpdir =
- Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));
+ Paths.get(System.getProperty("java.io.tmpdir", "."));
private static final boolean isPosix =
FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/attribute/PosixFilePermissions.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/attribute/PosixFilePermissions.java
index b4c946a..05be1b0 100644
--- a/library/src/main/java/java8/nio/file/attribute/PosixFilePermissions.java
+++ b/library/src/main/java/java8/nio/file/attribute/PosixFilePermissions.java
@@ -166,7 +166,7 @@ public final class PosixFilePermissions {
throw new NullPointerException();
}
final Set<PosixFilePermission> value = perms;
- return new FileAttribute<>() {
+ return new FileAttribute<Set<PosixFilePermission>>() {
@Override
public String name() {
return "posix:permissions";
diff --git a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/spi/FileSystemProvider.java b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/spi/FileSystemProvider.java
index 9fc2ff4..476aab8 100644
--- a/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/spi/FileSystemProvider.java
+++ b/${LIBRARY_JAVA_SOURCE_ROOT}/java8/nio/file/spi/FileSystemProvider.java
@@ -81,10 +81,7 @@ public abstract class FileSystemProvider {
private static final Object lock = new Object();
// installed providers
- private static volatile List<FileSystemProvider> installedProviders;
-
- // used to avoid recursive loading of instaled providers
- private static boolean loadingProviders = false;
+ private static volatile List<FileSystemProvider> installedProviders = new ArrayList<>();
private static Void checkPermission() {
SecurityManager sm = System.getSecurityManager();
@@ -110,32 +107,40 @@ public abstract class FileSystemProvider {
this(checkPermission());
}
- // loads all installed providers
- private static List<FileSystemProvider> loadInstalledProviders() {
- List<FileSystemProvider> list = new ArrayList<FileSystemProvider>();
+ public static void installDefaultProvider(FileSystemProvider provider) {
+ if (!provider.getScheme().equals("file")) {
+ throw new Error("Default provider must use scheme 'file'");
+ }
+ synchronized (lock) {
+ if (!installedProviders.isEmpty()) {
+ throw new Error("A provider has already been installed");
+ }
+ installedProviders.add(provider);
+ }
+ }
- ServiceLoader<FileSystemProvider> sl = ServiceLoader
- .load(FileSystemProvider.class, ClassLoader.getSystemClassLoader());
+ public static FileSystemProvider defaultProvider() {
+ synchronized (lock) {
+ if (installedProviders.isEmpty()) {
+ throw new Error("Must initialize with FileSystemProvider.installDefaultProvider()");
+ }
+ return installedProviders.get(0);
+ }
+ }
- // ServiceConfigurationError may be throw here
- for (FileSystemProvider provider: sl) {
+ public static void installProvider(FileSystemProvider provider) {
+ synchronized (lock) {
+ if (installedProviders.isEmpty()) {
+ throw new Error("Must initialize with FileSystemProvider.installDefaultProvider()");
+ }
String scheme = provider.getScheme();
-
- // add to list if the provider is not "file" and isn't a duplicate
- if (!scheme.equalsIgnoreCase("file")) {
- boolean found = false;
- for (FileSystemProvider p: list) {
- if (p.getScheme().equalsIgnoreCase(scheme)) {
- found = true;
- break;
- }
- }
- if (!found) {
- list.add(provider);
+ for (FileSystemProvider p : installedProviders) {
+ if (p.getScheme().equalsIgnoreCase(scheme)) {
+ return;
}
}
+ installedProviders.add(provider);
}
- return list;
}
/**
@@ -153,32 +158,9 @@ public abstract class FileSystemProvider {
* When an error occurs while loading a service provider
*/
public static List<FileSystemProvider> installedProviders() {
- if (installedProviders == null) {
- // ensure default provider is initialized
- FileSystemProvider defaultProvider = FileSystems.getDefault().provider();
-
- synchronized (lock) {
- if (installedProviders == null) {
- if (loadingProviders) {
- throw new Error("Circular loading of installed providers detected");
- }
- loadingProviders = true;
-
- List<FileSystemProvider> list = AccessController
- .doPrivileged(new PrivilegedAction<List<FileSystemProvider>>() {
- @Override
- public List<FileSystemProvider> run() {
- return loadInstalledProviders();
- }});
-
- // insert the default provider at the start of the list
- list.add(0, defaultProvider);
-
- installedProviders = Collections.unmodifiableList(list);
- }
- }
+ synchronized (lock) {
+ return new ArrayList<>(installedProviders);
}
- return installedProviders;
}
/**
EOF