Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/nio/file/spi/TestProvider.java
66645 views
1
/*
2
* Copyright (c) 2008, 2011, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.File;
25
import java.nio.file.*;
26
import java.nio.file.attribute.BasicFileAttributes;
27
import java.nio.file.attribute.FileAttribute;
28
import java.nio.file.attribute.FileAttributeView;
29
import java.nio.file.attribute.UserPrincipalLookupService;
30
import java.nio.file.spi.FileSystemProvider;
31
import java.nio.channels.FileChannel;
32
import java.nio.channels.SeekableByteChannel;
33
import java.net.URI;
34
import java.io.IOException;
35
import java.util.Collections;
36
import java.util.Iterator;
37
import java.util.Map;
38
import java.util.Set;
39
40
public class TestProvider extends FileSystemProvider {
41
42
private final FileSystemProvider defaultProvider;
43
private final TestFileSystem theFileSystem;
44
45
public TestProvider(FileSystemProvider defaultProvider) {
46
this.defaultProvider = defaultProvider;
47
FileSystem fs = defaultProvider.getFileSystem(URI.create("file:/"));
48
this.theFileSystem = new TestFileSystem(fs, this);
49
}
50
51
FileSystemProvider defaultProvider() {
52
return defaultProvider;
53
}
54
55
@Override
56
public String getScheme() {
57
return "file";
58
}
59
60
@Override
61
public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException {
62
return defaultProvider.newFileSystem(uri, env);
63
}
64
65
@Override
66
public FileSystem getFileSystem(URI uri) {
67
return theFileSystem;
68
}
69
70
@Override
71
public Path getPath(URI uri) {
72
Path path = defaultProvider.getPath(uri);
73
return theFileSystem.wrap(path);
74
}
75
76
@Override
77
public void setAttribute(Path file, String attribute, Object value,
78
LinkOption... options)
79
throws IOException
80
{
81
throw new RuntimeException("not implemented");
82
}
83
84
@Override
85
public Map<String,Object> readAttributes(Path file, String attributes,
86
LinkOption... options)
87
throws IOException
88
{
89
Path delegate = theFileSystem.unwrap(file);
90
return defaultProvider.readAttributes(delegate, attributes, options);
91
}
92
93
@Override
94
public <A extends BasicFileAttributes> A readAttributes(Path file,
95
Class<A> type,
96
LinkOption... options)
97
throws IOException
98
{
99
Path delegate = theFileSystem.unwrap(file);
100
return defaultProvider.readAttributes(delegate, type, options);
101
}
102
103
@Override
104
public <V extends FileAttributeView> V getFileAttributeView(Path file,
105
Class<V> type,
106
LinkOption... options)
107
{
108
Path delegate = theFileSystem.unwrap(file);
109
return defaultProvider.getFileAttributeView(delegate, type, options);
110
}
111
112
@Override
113
public void delete(Path file) throws IOException {
114
Path delegate = theFileSystem.unwrap(file);
115
defaultProvider.delete(delegate);
116
}
117
118
@Override
119
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
120
throws IOException
121
{
122
throw new RuntimeException("not implemented");
123
}
124
125
@Override
126
public void createLink(Path link, Path existing) throws IOException {
127
throw new RuntimeException("not implemented");
128
}
129
130
@Override
131
public Path readSymbolicLink(Path link) throws IOException {
132
Path delegate = theFileSystem.unwrap(link);
133
Path target = defaultProvider.readSymbolicLink(delegate);
134
return theFileSystem.wrap(target);
135
}
136
137
@Override
138
public void copy(Path source, Path target, CopyOption... options)
139
throws IOException
140
{
141
throw new RuntimeException("not implemented");
142
}
143
144
@Override
145
public void move(Path source, Path target, CopyOption... options)
146
throws IOException
147
{
148
throw new RuntimeException("not implemented");
149
}
150
151
@Override
152
public DirectoryStream<Path> newDirectoryStream(Path dir,
153
DirectoryStream.Filter<? super Path> filter)
154
throws IOException
155
{
156
throw new RuntimeException("not implemented");
157
}
158
159
@Override
160
public void createDirectory(Path dir, FileAttribute<?>... attrs)
161
throws IOException
162
{
163
Path delegate = theFileSystem.unwrap(dir);
164
defaultProvider.createDirectory(delegate, attrs);
165
}
166
167
@Override
168
public SeekableByteChannel newByteChannel(Path file,
169
Set<? extends OpenOption> options,
170
FileAttribute<?>... attrs)
171
throws IOException
172
{
173
Path delegate = theFileSystem.unwrap(file);
174
return defaultProvider.newByteChannel(delegate, options, attrs);
175
}
176
177
@Override
178
public FileChannel newFileChannel(Path file,
179
Set<? extends OpenOption> options,
180
FileAttribute<?>... attrs)
181
throws IOException
182
{
183
Path delegate = theFileSystem.unwrap(file);
184
return defaultProvider.newFileChannel(delegate, options, attrs);
185
}
186
187
@Override
188
public boolean isHidden(Path file) throws IOException {
189
throw new ReadOnlyFileSystemException();
190
}
191
192
@Override
193
public FileStore getFileStore(Path file) throws IOException {
194
throw new RuntimeException("not implemented");
195
}
196
197
@Override
198
public boolean isSameFile(Path file, Path other) throws IOException {
199
throw new RuntimeException("not implemented");
200
}
201
202
@Override
203
public void checkAccess(Path file, AccessMode... modes)
204
throws IOException
205
{
206
throw new RuntimeException("not implemented");
207
}
208
209
static class TestFileSystem extends FileSystem {
210
private final FileSystem delegate;
211
private final TestProvider provider;
212
213
TestFileSystem(FileSystem delegate, TestProvider provider) {
214
this.delegate = delegate;
215
this.provider = provider;
216
}
217
218
Path wrap(Path path) {
219
return (path != null) ? new TestPath(this, path) : null;
220
}
221
222
Path unwrap(Path wrapper) {
223
if (wrapper == null)
224
throw new NullPointerException();
225
if (!(wrapper instanceof TestPath))
226
throw new ProviderMismatchException();
227
return ((TestPath)wrapper).unwrap();
228
}
229
230
@Override
231
public FileSystemProvider provider() {
232
return provider;
233
}
234
235
@Override
236
public void close() throws IOException {
237
throw new RuntimeException("not implemented");
238
}
239
240
@Override
241
public boolean isOpen() {
242
return true;
243
}
244
245
@Override
246
public boolean isReadOnly() {
247
return false;
248
}
249
250
@Override
251
public String getSeparator() {
252
return delegate.getSeparator();
253
}
254
255
@Override
256
public Iterable<Path> getRootDirectories() {
257
throw new RuntimeException("not implemented");
258
}
259
260
@Override
261
public Iterable<FileStore> getFileStores() {
262
throw new RuntimeException("not implemented");
263
}
264
265
@Override
266
public Set<String> supportedFileAttributeViews() {
267
return delegate.supportedFileAttributeViews();
268
}
269
270
@Override
271
public Path getPath(String first, String... more) {
272
Path path = delegate.getPath(first, more);
273
return wrap(path);
274
}
275
276
@Override
277
public PathMatcher getPathMatcher(String syntaxAndPattern) {
278
return delegate.getPathMatcher(syntaxAndPattern);
279
}
280
281
@Override
282
public UserPrincipalLookupService getUserPrincipalLookupService() {
283
return delegate.getUserPrincipalLookupService();
284
}
285
286
@Override
287
public WatchService newWatchService() throws IOException {
288
throw new UnsupportedOperationException();
289
}
290
}
291
292
static class TestPath implements Path {
293
private final TestFileSystem fs;
294
private final Path delegate;
295
296
TestPath(TestFileSystem fs, Path delegate) {
297
this.fs = fs;
298
this.delegate = delegate;
299
}
300
301
Path unwrap() {
302
return delegate;
303
}
304
305
@Override
306
public FileSystem getFileSystem() {
307
return fs;
308
}
309
310
@Override
311
public boolean isAbsolute() {
312
return delegate.isAbsolute();
313
}
314
315
@Override
316
public Path getRoot() {
317
return fs.wrap(delegate.getRoot());
318
}
319
320
@Override
321
public Path getParent() {
322
return fs.wrap(delegate.getParent());
323
}
324
325
@Override
326
public int getNameCount() {
327
return delegate.getNameCount();
328
}
329
330
@Override
331
public Path getFileName() {
332
return fs.wrap(delegate.getFileName());
333
}
334
335
@Override
336
public Path getName(int index) {
337
return fs.wrap(delegate.getName(index));
338
}
339
340
@Override
341
public Path subpath(int beginIndex, int endIndex) {
342
return fs.wrap(delegate.subpath(beginIndex, endIndex));
343
}
344
345
@Override
346
public boolean startsWith(Path other) {
347
return delegate.startsWith(fs.unwrap(other));
348
}
349
350
@Override
351
public boolean startsWith(String other) {
352
return delegate.startsWith(other);
353
}
354
355
@Override
356
public boolean endsWith(Path other) {
357
return delegate.endsWith(fs.unwrap(other));
358
}
359
360
@Override
361
public boolean endsWith(String other) {
362
return delegate.endsWith(other);
363
}
364
365
@Override
366
public Path normalize() {
367
return fs.wrap(delegate.normalize());
368
}
369
370
@Override
371
public Path resolve(Path other) {
372
return fs.wrap(delegate.resolve(fs.unwrap(other)));
373
}
374
375
@Override
376
public Path resolve(String other) {
377
return fs.wrap(delegate.resolve(other));
378
}
379
380
@Override
381
public Path resolveSibling(Path other) {
382
return fs.wrap(delegate.resolveSibling(fs.unwrap(other)));
383
}
384
385
@Override
386
public Path resolveSibling(String other) {
387
return fs.wrap(delegate.resolveSibling(other));
388
}
389
390
@Override
391
public Path relativize(Path other) {
392
return fs.wrap(delegate.relativize(fs.unwrap(other)));
393
}
394
395
@Override
396
public boolean equals(Object other) {
397
if (!(other instanceof TestPath))
398
return false;
399
return delegate.equals(fs.unwrap((TestPath) other));
400
}
401
402
@Override
403
public int hashCode() {
404
return delegate.hashCode();
405
}
406
407
@Override
408
public String toString() {
409
return delegate.toString();
410
}
411
412
@Override
413
public URI toUri() {
414
String ssp = delegate.toUri().getSchemeSpecificPart();
415
return URI.create(fs.provider().getScheme() + ":" + ssp);
416
}
417
418
@Override
419
public Path toAbsolutePath() {
420
return fs.wrap(delegate.toAbsolutePath());
421
}
422
423
@Override
424
public Path toRealPath(LinkOption... options) throws IOException {
425
return fs.wrap(delegate.toRealPath(options));
426
}
427
428
@Override
429
public File toFile() {
430
return new File(toString());
431
}
432
433
@Override
434
public Iterator<Path> iterator() {
435
final Iterator<Path> itr = delegate.iterator();
436
return new Iterator<Path>() {
437
@Override
438
public boolean hasNext() {
439
return itr.hasNext();
440
}
441
@Override
442
public Path next() {
443
return fs.wrap(itr.next());
444
}
445
@Override
446
public void remove() {
447
itr.remove();
448
}
449
};
450
}
451
452
@Override
453
public int compareTo(Path other) {
454
return delegate.compareTo(fs.unwrap(other));
455
}
456
457
@Override
458
public WatchKey register(WatchService watcher,
459
WatchEvent.Kind<?>[] events,
460
WatchEvent.Modifier... modifiers)
461
{
462
throw new UnsupportedOperationException();
463
}
464
465
@Override
466
public WatchKey register(WatchService watcher,
467
WatchEvent.Kind<?>... events)
468
{
469
throw new UnsupportedOperationException();
470
}
471
}
472
}
473
474