Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
* correctly unwrap DelegatingPath
* improve javadoc
* remove useless catch blocks
  • Loading branch information
Postremus committed Dec 26, 2021
1 parent 903ab7b commit b92c7d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
38 changes: 23 additions & 15 deletions src/main/java/io/quarkus/fs/util/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -127,8 +127,6 @@ public static FileSystem newZip(Path zipFile) throws IOException {
}
try {
return FileSystemProviders.ZIP_PROVIDER.newFileSystem(toZipUri(zipFile), env);
} catch (FileSystemAlreadyExistsException e) {
throw new IOException("fs already exists " + zipFile, e);
} catch (IOException ioe) {
// include the URI for which the filesystem creation failed
throw new IOException("Failed to create a new filesystem for " + zipFile, ioe);
Expand Down Expand Up @@ -164,13 +162,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

/**
* This call is not thread safe, a single of FileSystem can be created for the
* profided uri until it is closed.
* provided uri until it is closed.
*
* @param uri The uri to the zip file.
* @param env Env map.
* @return A new FileSystem.
* @throws IOException in case of a failure
* @deprecated Use {@link #newFileSystem(Path)} or {@link #newZip(Path)}.
*/
public static FileSystem newFileSystem(URI uri, Map<String, Object> env) throws IOException {
env = new HashMap<>(env);
Expand All @@ -183,29 +180,39 @@ public static FileSystem newFileSystem(URI uri, Map<String, Object> env) throws
return FileSystemProviders.ZIP_PROVIDER.newFileSystem(uri, env);
} catch (FileSystemAlreadyExistsException e) {
throw new IOException("fs already exists " + uri, e);
} catch (IOException | ZipError ioe) {
// TODO: (at a later date) Get rid of the ZipError catching (and instead only catch IOException)
// since it's a JDK bug which threw the undeclared ZipError instead of an IOException.
// Java 9 fixes it https://bugs.openjdk.java.net/browse/JDK-8062754

} catch (IOException ioe) {
// include the URI for which the filesystem creation failed
throw new IOException("Failed to create a new filesystem for " + uri, ioe);
}
}

/**
* This call is thread safe, a new FS is created for each invocation.
* Constructs a new FileSystem to access the contents of a zip as a file system.
*
* @param path The zip file.
* @return A new FileSystem instance
* @throws IOException in case of a failure
*/
public static FileSystem newFileSystem(Path path) throws IOException {
return newFileSystem(path, Collections.emptyMap());
}

/**
* Constructs a new FileSystem to access the contents of a zip as a file system.
*
* @param path The zip file.
* @param env Property map to configure the constructed FileSystem.
* @return A new FileSystem instance
* @throws IOException in case of a failure
*/
private static FileSystem newFileSystem(Path path, Map<String, Object> env) throws IOException {
final Map<String, Object> tmp = new HashMap<>(DEFAULT_OWNER_ENV);
tmp.putAll(env);
env = tmp;

try {
path = FileSystemHelper.ignoreFileWriteability(path);
return FileSystemProviders.ZIP_PROVIDER.newFileSystem(path, DEFAULT_OWNER_ENV);
} catch (FileSystemAlreadyExistsException e) {
throw new IOException("fs already exists " + path, e);
return FileSystemProviders.ZIP_PROVIDER.newFileSystem(path, env);
} catch (IOException ioe) {
// include the path for which the filesystem creation failed
throw new IOException("Failed to create a new filesystem for " + path, ioe);
Expand All @@ -216,6 +223,7 @@ public static FileSystem newFileSystem(Path path) throws IOException {
* This call is thread safe, a new FS is created for each invocation.
*
* @param path The zip file.
* @param classLoader the classloader to locate the appropriate FileSystemProvider to open the path
* @return A new FileSystem instance
* @throws IOException in case of a failure
* @deprecated Use {@link #newFileSystem(Path)}. Providing a classLoader makes no difference, since the
Expand All @@ -224,7 +232,7 @@ public static FileSystem newFileSystem(Path path) throws IOException {
*/
@Deprecated
public static FileSystem newFileSystem(final Path path, ClassLoader classLoader) throws IOException {
return newFileSystem(path);
return newFileSystem(path, Collections.emptyMap());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/quarkus/fs/util/base/DelegatingPath.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.fs.util.base;

import io.quarkus.fs.util.sysfs.PathWrapper;
import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -39,8 +38,8 @@ protected DelegatingPath(Path delegate) {
* @return unwrapped path, or the original one if not an instance of DelegatingPath
*/
public static Path unwrap(Path path) {
if (path instanceof PathWrapper) {
return ((PathWrapper) path).getDelegate();
if (path instanceof DelegatingPath) {
return ((DelegatingPath) path).getDelegate();
}
return path;
}
Expand Down

0 comments on commit b92c7d9

Please sign in to comment.