15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.*;
29 import java.net.URI;
30 import java.nio.channels.*;
31 import java.nio.file.*;
32 import java.nio.file.DirectoryStream.Filter;
33 import java.nio.file.attribute.*;
34 import java.util.*;
35 import static java.nio.file.StandardOpenOption.*;
36 import static java.nio.file.StandardCopyOption.*;
37
38
39 /**
40 *
41 * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal
42 */
43
44 class ZipPath implements Path {
45
46 private final ZipFileSystem zfs;
47 private final byte[] path;
48 private volatile int[] offsets;
49 private int hashcode = 0; // cached hashcode (created lazily)
50
51 ZipPath(ZipFileSystem zfs, byte[] path) {
52 this(zfs, path, false);
53 }
54
55 ZipPath(ZipFileSystem zfs, byte[] path, boolean normalized)
56 {
57 this.zfs = zfs;
58 if (normalized)
163 byte[] defaultdir = zfs.getDefaultDir().path;
164 int defaultlen = defaultdir.length;
165 boolean endsWith = (defaultdir[defaultlen - 1] == '/');
166 byte[] t = null;
167 if (endsWith)
168 t = new byte[defaultlen + path.length];
169 else
170 t = new byte[defaultlen + 1 + path.length];
171 System.arraycopy(defaultdir, 0, t, 0, defaultlen);
172 if (!endsWith)
173 t[defaultlen++] = '/';
174 System.arraycopy(path, 0, t, defaultlen, path.length);
175 return new ZipPath(zfs, t, true); // normalized
176 }
177 }
178
179 @Override
180 public URI toUri() {
181 try {
182 return new URI("jar",
183 zfs.getZipFile().toUri() +
184 "!" +
185 zfs.getString(toAbsolutePath().path),
186 null);
187 } catch (Exception ex) {
188 throw new AssertionError(ex);
189 }
190 }
191
192 private boolean equalsNameAt(ZipPath other, int index) {
193 int mbegin = offsets[index];
194 int mlen = 0;
195 if (index == (offsets.length-1))
196 mlen = path.length - mbegin;
197 else
198 mlen = offsets[index + 1] - mbegin - 1;
199 int obegin = other.offsets[index];
200 int olen = 0;
201 if (index == (other.offsets.length - 1))
202 olen = other.path.length - obegin;
203 else
849 } finally {
850 is.close();
851 }
852 }
853 if (copyAttrs) {
854 BasicFileAttributeView view =
855 ZipFileAttributeView.get(target, BasicFileAttributeView.class);
856 try {
857 view.setTimes(zfas.lastModifiedTime(),
858 zfas.lastAccessTime(),
859 zfas.creationTime());
860 } catch (IOException x) {
861 // rollback?
862 try {
863 target.delete();
864 } catch (IOException ignore) { }
865 throw x;
866 }
867 }
868 }
869 }
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.*;
29 import java.net.URI;
30 import java.nio.channels.*;
31 import java.nio.file.*;
32 import java.nio.file.DirectoryStream.Filter;
33 import java.nio.file.attribute.*;
34 import java.util.*;
35 import static java.nio.charset.StandardCharsets.UTF_8;
36 import static java.nio.file.StandardOpenOption.*;
37 import static java.nio.file.StandardCopyOption.*;
38
39 /**
40 *
41 * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal
42 */
43
44 class ZipPath implements Path {
45
46 private final ZipFileSystem zfs;
47 private final byte[] path;
48 private volatile int[] offsets;
49 private int hashcode = 0; // cached hashcode (created lazily)
50
51 ZipPath(ZipFileSystem zfs, byte[] path) {
52 this(zfs, path, false);
53 }
54
55 ZipPath(ZipFileSystem zfs, byte[] path, boolean normalized)
56 {
57 this.zfs = zfs;
58 if (normalized)
163 byte[] defaultdir = zfs.getDefaultDir().path;
164 int defaultlen = defaultdir.length;
165 boolean endsWith = (defaultdir[defaultlen - 1] == '/');
166 byte[] t = null;
167 if (endsWith)
168 t = new byte[defaultlen + path.length];
169 else
170 t = new byte[defaultlen + 1 + path.length];
171 System.arraycopy(defaultdir, 0, t, 0, defaultlen);
172 if (!endsWith)
173 t[defaultlen++] = '/';
174 System.arraycopy(path, 0, t, defaultlen, path.length);
175 return new ZipPath(zfs, t, true); // normalized
176 }
177 }
178
179 @Override
180 public URI toUri() {
181 try {
182 return new URI("jar",
183 decodeUri(zfs.getZipFile().toUri().toString()) +
184 "!" +
185 zfs.getString(toAbsolutePath().path),
186 null);
187 } catch (Exception ex) {
188 throw new AssertionError(ex);
189 }
190 }
191
192 private boolean equalsNameAt(ZipPath other, int index) {
193 int mbegin = offsets[index];
194 int mlen = 0;
195 if (index == (offsets.length-1))
196 mlen = path.length - mbegin;
197 else
198 mlen = offsets[index + 1] - mbegin - 1;
199 int obegin = other.offsets[index];
200 int olen = 0;
201 if (index == (other.offsets.length - 1))
202 olen = other.path.length - obegin;
203 else
849 } finally {
850 is.close();
851 }
852 }
853 if (copyAttrs) {
854 BasicFileAttributeView view =
855 ZipFileAttributeView.get(target, BasicFileAttributeView.class);
856 try {
857 view.setTimes(zfas.lastModifiedTime(),
858 zfas.lastAccessTime(),
859 zfas.creationTime());
860 } catch (IOException x) {
861 // rollback?
862 try {
863 target.delete();
864 } catch (IOException ignore) { }
865 throw x;
866 }
867 }
868 }
869
870 private static int decode(char c) {
871 if ((c >= '0') && (c <= '9'))
872 return c - '0';
873 if ((c >= 'a') && (c <= 'f'))
874 return c - 'a' + 10;
875 if ((c >= 'A') && (c <= 'F'))
876 return c - 'A' + 10;
877 assert false;
878 return -1;
879 }
880
881 // to avoid double escape
882 static String decodeUri(String s) {
883 if (s == null)
884 return s;
885 int n = s.length();
886 if (n == 0)
887 return s;
888 if (s.indexOf('%') < 0)
889 return s;
890
891 StringBuilder sb = new StringBuilder(n);
892 byte[] bb = new byte[n];
893 boolean betweenBrackets = false;
894
895 for (int i = 0; i < n;) {
896 char c = s.charAt(i);
897 if (c == '[') {
898 betweenBrackets = true;
899 } else if (betweenBrackets && c == ']') {
900 betweenBrackets = false;
901 }
902 if (c != '%' || betweenBrackets ) {
903 sb.append(c);
904 i++;
905 continue;
906 }
907 int nb = 0;
908 while (c == '%') {
909 assert (n - i >= 2);
910 bb[nb++] = (byte)(((decode(s.charAt(++i)) & 0xf) << 4) |
911 (decode(s.charAt(++i)) & 0xf));
912 if (++i >= n) {
913 break;
914 }
915 c = s.charAt(i);
916 }
917 sb.append(new String(bb, 0, nb, UTF_8));
918 }
919 return sb.toString();
920 }
921
922 }
|