< prev index next >

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

Print this page
rev 53038 : 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
   1 /*
   2  * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  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 final 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         this.zfs = zfs;
  57         if (normalized) {
  58             this.path = path;
  59         } else {
  60             if (zfs.zc.isUTF8()) {
  61                 this.path = normalize(path);
  62             } else {    // see normalize(String);
  63                 this.path = normalize(zfs.getString(path));


 505         int len = path.length();
 506         if (len == 0)
 507             return new byte[0];
 508         char prevC = 0;
 509         for (int i = 0; i < len; i++) {
 510             char c = path.charAt(i);
 511             if (c == '\\' || c == '\u0000')
 512                 return normalize(path, i, len);
 513             if (c == '/' && prevC == '/')
 514                 return normalize(path, i - 1, len);
 515             prevC = c;
 516         }
 517         if (len > 1 && prevC == '/')
 518             path = path.substring(0, len - 1);
 519         return zfs.getBytes(path);
 520     }
 521 
 522     private byte[] normalize(String path, int off, int len) {
 523         StringBuilder to = new StringBuilder(len);
 524         to.append(path, 0, off);
 525         int m = off;
 526         char prevC = 0;
 527         while (off < len) {
 528             char c = path.charAt(off++);
 529             if (c == '\\')
 530                 c = '/';
 531             if (c == '/' && prevC == '/')
 532                 continue;
 533             if (c == '\u0000')
 534                 throw new InvalidPathException(path,
 535                                                "Path: nul character not allowed");
 536             to.append(c);
 537             prevC = c;
 538         }
 539         len = to.length();
 540         if (len > 1 && prevC == '/')
 541             to.delete(len -1, len);
 542         return zfs.getBytes(to.toString());
 543     }
 544 
 545     // Remove DotSlash(./) and resolve DotDot (..) components


   1 /*
   2  * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  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.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.OutputStream;
  32 import java.net.URI;
  33 import java.nio.channels.FileChannel;
  34 import java.nio.channels.SeekableByteChannel;
  35 import java.nio.file.*;
  36 import java.nio.file.DirectoryStream.Filter;
  37 import java.nio.file.attribute.BasicFileAttributeView;
  38 import java.nio.file.attribute.FileAttribute;
  39 import java.nio.file.attribute.FileTime;
  40 import java.util.Arrays;
  41 import java.util.Iterator;
  42 import java.util.Map;
  43 import java.util.NoSuchElementException;
  44 import java.util.Objects;
  45 import java.util.Set;
  46 
  47 import static java.nio.charset.StandardCharsets.UTF_8;
  48 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  49 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  50 import static java.nio.file.StandardOpenOption.CREATE;
  51 import static java.nio.file.StandardOpenOption.READ;
  52 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  53 import static java.nio.file.StandardOpenOption.WRITE;
  54 
  55 /**
  56  * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal

  57  */

  58 final class ZipPath implements Path {
  59 
  60     private final ZipFileSystem zfs;
  61     private final byte[] path;
  62     private volatile int[] offsets;
  63     private int hashcode = 0;  // cached hashcode (created lazily)
  64 
  65     ZipPath(ZipFileSystem zfs, byte[] path) {
  66         this(zfs, path, false);
  67     }
  68 
  69     ZipPath(ZipFileSystem zfs, byte[] path, boolean normalized) {
  70         this.zfs = zfs;
  71         if (normalized) {
  72             this.path = path;
  73         } else {
  74             if (zfs.zc.isUTF8()) {
  75                 this.path = normalize(path);
  76             } else {    // see normalize(String);
  77                 this.path = normalize(zfs.getString(path));


 519         int len = path.length();
 520         if (len == 0)
 521             return new byte[0];
 522         char prevC = 0;
 523         for (int i = 0; i < len; i++) {
 524             char c = path.charAt(i);
 525             if (c == '\\' || c == '\u0000')
 526                 return normalize(path, i, len);
 527             if (c == '/' && prevC == '/')
 528                 return normalize(path, i - 1, len);
 529             prevC = c;
 530         }
 531         if (len > 1 && prevC == '/')
 532             path = path.substring(0, len - 1);
 533         return zfs.getBytes(path);
 534     }
 535 
 536     private byte[] normalize(String path, int off, int len) {
 537         StringBuilder to = new StringBuilder(len);
 538         to.append(path, 0, off);

 539         char prevC = 0;
 540         while (off < len) {
 541             char c = path.charAt(off++);
 542             if (c == '\\')
 543                 c = '/';
 544             if (c == '/' && prevC == '/')
 545                 continue;
 546             if (c == '\u0000')
 547                 throw new InvalidPathException(path,
 548                                                "Path: nul character not allowed");
 549             to.append(c);
 550             prevC = c;
 551         }
 552         len = to.length();
 553         if (len > 1 && prevC == '/')
 554             to.delete(len -1, len);
 555         return zfs.getBytes(to.toString());
 556     }
 557 
 558     // Remove DotSlash(./) and resolve DotDot (..) components


< prev index next >