Skip to content

Commit 364c89e

Browse files
author
Gary Gregory
committed
Add FileSystemProviders.
1 parent 55d213e commit 364c89e

4 files changed

Lines changed: 205 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.9.0" date="2020-MM-DD" description="Java 8 required.">
50-
<action issue="686" dev="ggregory" type="add" due-to="Alan Moffat, Gary Gregory">
50+
<action issue="IO-686" dev="ggregory" type="add" due-to="Alan Moffat, Gary Gregory">
5151
IOUtils.toByteArray(InputStream) Javadoc does not match code
5252
</action>
5353
<action issue="IO-689" dev="aherbert" type="fix" due-to="Uwe Schindler">
5454
FileUtils: Remove Instant->ZonedDateTime->Instant round-trip.
5555
</action>
56+
<action dev="ggregory" type="add" due-to="Gary Gregory">
57+
Add FileSystemProviders.
58+
</action>
5659
<!-- UPDATES -->
5760
<action dev="ggregory" type="update" due-to="Dependabot">
5861
Update junit-jupiter from 5.6.2 to 5.7.0 #153.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.file.spi;
19+
20+
import java.net.URI;
21+
import java.net.URL;
22+
import java.nio.file.FileSystems;
23+
import java.nio.file.Path;
24+
import java.nio.file.spi.FileSystemProvider;
25+
import java.util.List;
26+
import java.util.Objects;
27+
28+
/**
29+
* Helps working with {@link FileSystemProvider}.
30+
*
31+
* @since 2.9.0
32+
*/
33+
public class FileSystemProviders {
34+
35+
private static FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders());
36+
37+
/**
38+
* Gets the {@link FileSystemProvider} for the given Path.
39+
*
40+
* @param path The Path to query
41+
* @return the {@link FileSystemProvider} for the given Path.
42+
*/
43+
@SuppressWarnings("resource") // FileSystem is not allocated here.
44+
public static FileSystemProvider getFileSystemProvider(final Path path) {
45+
return Objects.requireNonNull(path, "path").getFileSystem().provider();
46+
}
47+
48+
/**
49+
* Returns the instance for the installed providers.
50+
*
51+
* @return the instance for the installed providers.
52+
* @see FileSystemProvider#installedProviders()
53+
*/
54+
public static FileSystemProviders installed() {
55+
return INSTALLED;
56+
}
57+
58+
private final List<FileSystemProvider> providers;
59+
60+
/*
61+
* Might make public later.
62+
*/
63+
private FileSystemProviders(List<FileSystemProvider> providers) {
64+
super();
65+
this.providers = providers;
66+
}
67+
68+
/**
69+
* Gets the {@link FileSystemProvider} for the given scheme.
70+
*
71+
* @param scheme The scheme to query.
72+
* @return the {@link FileSystemProvider} for the given URI or null.
73+
*/
74+
@SuppressWarnings("resource") // FileSystems.getDefault() returns a constant.
75+
public FileSystemProvider getFileSystemProvider(final String scheme) {
76+
Objects.requireNonNull(scheme, "scheme");
77+
// Check default provider first to avoid loading of installed providers.
78+
if (scheme.equalsIgnoreCase("file")) {
79+
return FileSystems.getDefault().provider();
80+
}
81+
// Find provider.
82+
if (providers != null) {
83+
for (final FileSystemProvider provider : providers) {
84+
if (provider.getScheme().equalsIgnoreCase(scheme)) {
85+
return provider;
86+
}
87+
}
88+
}
89+
return null;
90+
}
91+
92+
/**
93+
* Gets the {@link FileSystemProvider} for the given URI.
94+
*
95+
* @param uri The URI to query
96+
* @return the {@link FileSystemProvider} for the given URI or null.
97+
*/
98+
public FileSystemProvider getFileSystemProvider(final URI uri) {
99+
return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme());
100+
}
101+
102+
/**
103+
* Gets the {@link FileSystemProvider} for the given URL.
104+
*
105+
* @param url The URL to query
106+
* @return the {@link FileSystemProvider} for the given URI or null.
107+
*/
108+
public FileSystemProvider getFileSystemProvider(final URL url) {
109+
return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol());
110+
}
111+
112+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<html>
19+
<body>
20+
<p>This package provides extensions in the realm of java.nio.file.spi.</p>
21+
</body>
22+
</html>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.file.spi;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
import java.net.MalformedURLException;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
import java.net.URL;
27+
import java.nio.file.Paths;
28+
import java.nio.file.spi.FileSystemProvider;
29+
import java.util.List;
30+
31+
import org.junit.jupiter.api.Test;
32+
33+
public class FileSystemProvidersTest {
34+
35+
private static final String FILE_PATH = "file:///foo.txt";
36+
37+
@Test
38+
public void testGetFileSystemProvider_all() throws URISyntaxException {
39+
final List<FileSystemProvider> installedProviders = FileSystemProvider.installedProviders();
40+
for (FileSystemProvider fileSystemProvider : installedProviders) {
41+
final String scheme = fileSystemProvider.getScheme();
42+
final URI uri = new URI(scheme, "ssp", "fragment");
43+
assertEquals(scheme, FileSystemProviders.installed().getFileSystemProvider(uri).getScheme());
44+
}
45+
}
46+
47+
@Test
48+
public void testGetFileSystemProvider_filePath() {
49+
assertNotNull(FileSystemProviders.getFileSystemProvider(Paths.get(URI.create(FILE_PATH))));
50+
}
51+
52+
@Test
53+
public void testGetFileSystemProvider_fileScheme() {
54+
assertNotNull(FileSystemProviders.installed().getFileSystemProvider("file"));
55+
}
56+
57+
@Test
58+
public void testGetFileSystemProvider_fileURI() {
59+
assertNotNull(FileSystemProviders.installed().getFileSystemProvider(URI.create(FILE_PATH)));
60+
}
61+
62+
@Test
63+
public void testGetFileSystemProvider_fileURL() throws MalformedURLException {
64+
assertNotNull(FileSystemProviders.installed().getFileSystemProvider(new URL(FILE_PATH)));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)