< prev index next >

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

Print this page
rev 53034 : 8215472: 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 static java.nio.charset.StandardCharsets.UTF_8;
  29 import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
  30 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  31 import static java.nio.file.StandardOpenOption.CREATE;
  32 import static java.nio.file.StandardOpenOption.READ;
  33 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  34 import static java.nio.file.StandardOpenOption.WRITE;
  35 
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.io.OutputStream;
  40 import java.net.URI;
  41 import java.nio.channels.FileChannel;
  42 import java.nio.channels.SeekableByteChannel;
  43 import java.nio.file.AccessDeniedException;
  44 import java.nio.file.AccessMode;
  45 import java.nio.file.CopyOption;
  46 import java.nio.file.DirectoryNotEmptyException;
  47 import java.nio.file.DirectoryStream;
  48 import java.nio.file.DirectoryStream.Filter;
  49 import java.nio.file.FileAlreadyExistsException;
  50 import java.nio.file.FileStore;
  51 import java.nio.file.Files;
  52 import java.nio.file.InvalidPathException;
  53 import java.nio.file.LinkOption;
  54 import java.nio.file.NoSuchFileException;
  55 import java.nio.file.OpenOption;
  56 import java.nio.file.Path;
  57 import java.nio.file.ProviderMismatchException;
  58 import java.nio.file.ReadOnlyFileSystemException;
  59 import java.nio.file.WatchEvent;
  60 import java.nio.file.WatchKey;
  61 import java.nio.file.WatchService;
  62 import java.nio.file.attribute.BasicFileAttributeView;
  63 import java.nio.file.attribute.FileAttribute;
  64 import java.nio.file.attribute.FileTime;
  65 import java.util.Arrays;
  66 import java.util.Iterator;
  67 import java.util.Map;
  68 import java.util.NoSuchElementException;
  69 import java.util.Objects;
  70 import java.util.Set;
  71 
  72 /**
  73  * @author Xueming Shen, Rajendra Gutupalli,Jaya Hangal

  74  */

  75 final class ZipPath implements Path {
  76 
  77     private final ZipFileSystem zfs;
  78     private final byte[] path;
  79     private volatile int[] offsets;
  80     private int hashcode = 0;  // cached hashcode (created lazily)
  81 
  82     ZipPath(ZipFileSystem zfs, byte[] path) {
  83         this(zfs, path, false);
  84     }
  85 
  86     ZipPath(ZipFileSystem zfs, byte[] path, boolean normalized) {
  87         this.zfs = zfs;
  88         if (normalized) {
  89             this.path = path;
  90         } else {
  91             if (zfs.zc.isUTF8()) {
  92                 this.path = normalize(path);
  93             } else {    // see normalize(String);
  94                 this.path = normalize(zfs.getString(path));


 536         int len = path.length();
 537         if (len == 0)
 538             return new byte[0];
 539         char prevC = 0;
 540         for (int i = 0; i < len; i++) {
 541             char c = path.charAt(i);
 542             if (c == '\\' || c == '\u0000')
 543                 return normalize(path, i, len);
 544             if (c == '/' && prevC == '/')
 545                 return normalize(path, i - 1, len);
 546             prevC = c;
 547         }
 548         if (len > 1 && prevC == '/')
 549             path = path.substring(0, len - 1);
 550         return zfs.getBytes(path);
 551     }
 552 
 553     private byte[] normalize(String path, int off, int len) {
 554         StringBuilder to = new StringBuilder(len);
 555         to.append(path, 0, off);

 556         char prevC = 0;
 557         while (off < len) {
 558             char c = path.charAt(off++);
 559             if (c == '\\')
 560                 c = '/';
 561             if (c == '/' && prevC == '/')
 562                 continue;
 563             if (c == '\u0000')
 564                 throw new InvalidPathException(path,
 565                                                "Path: nul character not allowed");
 566             to.append(c);
 567             prevC = c;
 568         }
 569         len = to.length();
 570         if (len > 1 && prevC == '/')
 571             to.delete(len -1, len);
 572         return zfs.getBytes(to.toString());
 573     }
 574 
 575     // Remove DotSlash(./) and resolve DotDot (..) components


< prev index next >