src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipPath.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2009, 2014, 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 --- 1,7 ---- /* ! * Copyright (c) 2009, 2017, 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
*** 57,82 **** if (normalized) { this.path = path; } else { if (zfs.zc.isUTF8()) { this.path = normalize(path); ! } else { ! // see normalize(String); this.path = normalize(zfs.getString(path)); } } } ZipPath(ZipFileSystem zfs, String path) { this.zfs = zfs; - if (zfs.zc.isUTF8()) { - this.path = normalize(zfs.getBytes(path)); - } else { - // see normalize(String); this.path = normalize(path); } - } @Override public ZipPath getRoot() { if (this.isAbsolute()) return zfs.getRootDir(); --- 57,76 ---- if (normalized) { this.path = path; } else { if (zfs.zc.isUTF8()) { this.path = normalize(path); ! } else { // see normalize(String); this.path = normalize(zfs.getString(path)); } } } ZipPath(ZipFileSystem zfs, String path) { this.zfs = zfs; this.path = normalize(path); } @Override public ZipPath getRoot() { if (this.isAbsolute()) return zfs.getRootDir();
*** 84,118 **** return null; } @Override public Path getFileName() { ! initOffsets(); ! int count = offsets.length; ! if (count == 0) ! return null; // no elements so no name ! if (count == 1 && path[0] != '/') return this; ! int lastOffset = offsets[count-1]; ! int len = path.length - lastOffset; ! byte[] result = new byte[len]; ! System.arraycopy(path, lastOffset, result, 0, len); ! return new ZipPath(zfs, result); } @Override public ZipPath getParent() { ! initOffsets(); ! int count = offsets.length; ! if (count == 0) // no elements so no parent return null; ! int len = offsets[count-1] - 1; ! if (len <= 0) // parent is root only (may be null) return getRoot(); ! byte[] result = new byte[len]; ! System.arraycopy(path, 0, result, 0, len); ! return new ZipPath(zfs, result); } @Override public int getNameCount() { initOffsets(); --- 78,110 ---- return null; } @Override public Path getFileName() { ! int off = path.length; ! if (off == 0 || off == 1 && path[0] == '/') ! return null; ! while (--off >= 0 && path[off] != '/') {} ! if (off < 0) return this; ! off++; ! byte[] result = new byte[path.length - off]; ! System.arraycopy(path, off, result, 0, result.length); ! return new ZipPath(getFileSystem(), result, true); } @Override public ZipPath getParent() { ! int off = path.length; ! if (off == 0 || off == 1 && path[0] == '/') return null; ! while (--off >= 0 && path[off] != '/') {} ! if (off <= 0) return getRoot(); ! byte[] result = new byte[off]; ! System.arraycopy(path, 0, result, 0, off); ! return new ZipPath(getFileSystem(), result, true); } @Override public int getNameCount() { initOffsets();
*** 275,308 **** return zfs; } @Override public boolean isAbsolute() { ! return (this.path.length > 0 && path[0] == '/'); } @Override public ZipPath resolve(Path other) { ! final ZipPath o = checkPath(other); ! int tlen = this.path.length; ! if (tlen == 0 || o.isAbsolute()) ! return o; ! int olen = o.path.length; ! if (olen == 0) return this; byte[] resolved = null; ! if (this.path[tlen - 1] == '/') { resolved = new byte[tlen + olen]; ! System.arraycopy(path, 0, resolved, 0, tlen); ! System.arraycopy(o.path, 0, resolved, tlen, olen); } else { resolved = new byte[tlen + 1 + olen]; ! System.arraycopy(path, 0, resolved, 0, tlen); resolved[tlen] = '/'; ! System.arraycopy(o.path, 0, resolved, tlen + 1, olen); } ! return new ZipPath(zfs, resolved); } @Override public Path resolveSibling(Path other) { Objects.requireNonNull(other, "other"); --- 267,306 ---- return zfs; } @Override public boolean isAbsolute() { ! return path.length > 0 && path[0] == '/'; } @Override public ZipPath resolve(Path other) { ! ZipPath o = checkPath(other); ! if (o.path.length == 0) return this; + if (o.isAbsolute() || this.path.length == 0) + return o; + return resolve(o.path); + } + + // opath is normalized, just concat + private ZipPath resolve(byte[] opath) { byte[] resolved = null; ! byte[] tpath = this.path; ! int tlen = tpath.length; ! int olen = opath.length; ! if (path[tlen - 1] == '/') { resolved = new byte[tlen + olen]; ! System.arraycopy(tpath, 0, resolved, 0, tlen); ! System.arraycopy(opath, 0, resolved, tlen, olen); } else { resolved = new byte[tlen + 1 + olen]; ! System.arraycopy(tpath, 0, resolved, 0, tlen); resolved[tlen] = '/'; ! System.arraycopy(opath, 0, resolved, tlen + 1, olen); } ! return new ZipPath(zfs, resolved, true); } @Override public Path resolveSibling(Path other) { Objects.requireNonNull(other, "other");
*** 349,359 **** last == -1 || this.path[last] == '/'; } @Override public ZipPath resolve(String other) { ! return resolve(zfs.getPath(other)); } @Override public final Path resolveSibling(String other) { return resolveSibling(zfs.getPath(other)); --- 347,362 ---- last == -1 || this.path[last] == '/'; } @Override public ZipPath resolve(String other) { ! byte[] opath = normalize(other); ! if (opath.length == 0) ! return this; ! if (opath[0] == '/' || this.path.length == 0) ! return new ZipPath(zfs, opath, true); ! return resolve(opath); } @Override public final Path resolveSibling(String other) { return resolveSibling(zfs.getPath(other));
*** 453,464 **** return normalize(path, i); if (c == (byte)'/' && prevC == '/') return normalize(path, i - 1); prevC = c; } ! if (len > 1 && prevC == '/') return Arrays.copyOf(path, len - 1); return path; } private byte[] normalize(byte[] path, int off) { byte[] to = new byte[path.length]; --- 456,468 ---- return normalize(path, i); if (c == (byte)'/' && prevC == '/') return normalize(path, i - 1); prevC = c; } ! if (len > 1 && prevC == '/') { return Arrays.copyOf(path, len - 1); + } return path; } private byte[] normalize(byte[] path, int off) { byte[] to = new byte[path.length];
*** 488,497 **** --- 492,503 ---- // if zfs is NOT in utf8, normalize the path as "String" // to avoid incorrectly normalizing byte '0x5c' (as '\') // to '/'. private byte[] normalize(String path) { + if (zfs.zc.isUTF8()) + return normalize(zfs.getBytes(path)); int len = path.length(); if (len == 0) return new byte[0]; char prevC = 0; for (int i = 0; i < len; i++) {
*** 531,541 **** } // Remove DotSlash(./) and resolve DotDot (..) components private byte[] getResolved() { for (int i = 0; i < path.length; i++) { ! if (path[i] == (byte)'.') { return resolve0(); } } return path; } --- 537,548 ---- } // Remove DotSlash(./) and resolve DotDot (..) components private byte[] getResolved() { for (int i = 0; i < path.length; i++) { ! if (path[i] == (byte)'.' && ! (i + 1 == path.length || path[i + 1] == '/')) { return resolve0(); } } return path; }
*** 974,980 **** } sb.append(new String(bb, 0, nb, UTF_8)); } return sb.toString(); } - } --- 981,986 ----