zipfs and relative paths

Joel Uckelman uckelman at nomic.net
Sun Nov 29 07:57:17 PST 2009


Thus spake Joel Uckelman:
> 
> More patches for zipfs:
> 

Here's two more zipfs patches:

* Simplified subpath(). (This was overcomplex.)
* normalize() now returns null for paths equivalent to "."

-------------- next part --------------
# HG changeset patch
# User Joel Uckelman <uckelman at nomic.net>
# Date 1259509387 -3600
# Node ID cdf7fdb56c04d9e60d1f1e7a670069d86eaa6a94
# Parent  e05c7be3271e4d74c10a4b2672de3025245bad19
Simplified subpath().

diff --git a/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java b/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
--- a/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
+++ b/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
@@ -345,34 +345,33 @@
 
     @Override
     public ZipFilePath subpath(int beginIndex, int endIndex) {
+        initOffsets();
 
-        initOffsets();
         if (beginIndex < 0) {
             throw new IllegalArgumentException();
         }
+
         if (beginIndex >= (1 + offsets.size())) {
             throw new IllegalArgumentException();
         }
+
         if (endIndex > (1 + offsets.size())) {
             throw new IllegalArgumentException();
         }
+
         if (beginIndex >= endIndex) {
             throw new IllegalArgumentException();
         }
 
-        int elements = endIndex - beginIndex;
-        String result = null;
-        StringBuilder result1 = new StringBuilder("");
-        int index = beginIndex;
-        for (; elements-- != 0;) {
-            if (endIndex == offsets.size() && elements == 0) {
-                result1.append(subString(offsets.get(index), path.length));
-                break;
-            }
-            result1.append(subString(offsets.get(index), offsets.get(++index)));
+        String result = subString(
+            offsets.get(beginIndex),
+            endIndex < offsets.size() ? offsets.get(endIndex) : path.length
+        );
+
+        if (result.endsWith("/")) {
+            result = result.substring(0, result.length()-1);
         }
-        result = result1.toString();
-        result = (result.endsWith("/")) ? result.substring(0, result.length() - 1) : result;
+
         return new ZipFilePath(fileSystem, result.getBytes());
     }
 
-------------- next part --------------
# HG changeset patch
# User Joel Uckelman <uckelman at nomic.net>
# Date 1259509878 -3600
# Node ID d5401dc6df718abde155439521ee5a5fd9090b6c
# Parent  cdf7fdb56c04d9e60d1f1e7a670069d86eaa6a94
normalize() now returns null for paths equivalent to "."

diff --git a/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java b/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
--- a/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
+++ b/src/share/demo/nio/ZipFileSystem/com/sun/nio/zipfs/ZipFilePath.java
@@ -1198,11 +1198,10 @@
      **/
     @Override
     public Path normalize() {
-        return new ZipFilePath(
-            fileSystem,
-            ZipPathParser.resolve(new String(path)).getBytes(),
-            pathForZip
-        );
+        final String parsed = ZipPathParser.resolve(new String(path));
+
+        return parsed.equals("") ? null :
+            new ZipFilePath(fileSystem, parsed.getBytes(), pathForZip);
     }
 
     @Override


More information about the nio-dev mailing list