src/solaris/classes/java/io/UnixFileSystem.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2010, 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


  38     public UnixFileSystem() {
  39         slash = AccessController.doPrivileged(
  40             new GetPropertyAction("file.separator")).charAt(0);
  41         colon = AccessController.doPrivileged(
  42             new GetPropertyAction("path.separator")).charAt(0);
  43         javaHome = AccessController.doPrivileged(
  44             new GetPropertyAction("java.home"));
  45     }
  46 
  47 
  48     /* -- Normalization and construction -- */
  49 
  50     public char getSeparator() {
  51         return slash;
  52     }
  53 
  54     public char getPathSeparator() {
  55         return colon;
  56     }
  57 
  58     /* A normal Unix pathname contains no duplicate slashes and does not end
  59        with a slash.  It may be the empty string. */
  60 
  61     /* Normalize the given pathname, whose length is len, starting at the given
  62        offset; everything before this offset is already normal. */
  63     private String normalize(String pathname, int len, int off) {
  64         if (len == 0) return pathname;
  65         int n = len;
  66         while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--;
  67         if (n == 0) return "/";
  68         StringBuffer sb = new StringBuffer(pathname.length());
  69         if (off > 0) sb.append(pathname.substring(0, off));
  70         char prevChar = 0;
  71         for (int i = off; i < n; i++) {

  72             char c = pathname.charAt(i);

  73             if ((prevChar == '/') && (c == '/')) continue;

  74             sb.append(c);
  75             prevChar = c;
  76         }
  77         return sb.toString();
  78     }
  79 
  80     /* Check that the given pathname is normal.  If not, invoke the real
  81        normalizer on the part of the pathname that requires normalization.
  82        This way we iterate through the whole pathname string only once. */
  83     public String normalize(String pathname) {
  84         int n = pathname.length();
  85         char prevChar = 0;
  86         for (int i = 0; i < n; i++) {
  87             char c = pathname.charAt(i);
  88             if ((prevChar == '/') && (c == '/'))
  89                 return normalize(pathname, n, i - 1);
  90             prevChar = c;
  91         }
  92         if (prevChar == '/') return normalize(pathname, n, n - 1);
  93         return pathname;
  94     }
  95 
  96     public int prefixLength(String pathname) {
  97         if (pathname.length() == 0) return 0;
  98         return (pathname.charAt(0) == '/') ? 1 : 0;
  99     }
 100 
 101     public String resolve(String parent, String child) {
 102         if (child.equals("")) return parent;
 103         if (child.charAt(0) == '/') {
 104             if (parent.equals("/")) return child;
 105             return parent + child;
 106         }
 107         if (parent.equals("/")) return parent + child;
 108         return parent + '/' + child;
 109     }
 110 
 111     public String getDefaultParent() {
 112         return "/";
 113     }


   1 /*
   2  * Copyright (c) 1998, 2013, 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


  38     public UnixFileSystem() {
  39         slash = AccessController.doPrivileged(
  40             new GetPropertyAction("file.separator")).charAt(0);
  41         colon = AccessController.doPrivileged(
  42             new GetPropertyAction("path.separator")).charAt(0);
  43         javaHome = AccessController.doPrivileged(
  44             new GetPropertyAction("java.home"));
  45     }
  46 
  47 
  48     /* -- Normalization and construction -- */
  49 
  50     public char getSeparator() {
  51         return slash;
  52     }
  53 
  54     public char getPathSeparator() {
  55         return colon;
  56     }
  57 
  58     /*
  59      * A normal Unix path name contains no duplicate slashes and does not end
  60      * with a slash.  It may be the empty string. Check and normalize the
  61      * given pathname. The whole pathname string is only iterated once.
  62      */
  63     @Override
  64     public String normalize(String pathname) {
  65         final int len = pathname.length();
  66         StringBuilder sb = new StringBuilder(len);



  67         char prevChar = 0;
  68 
  69         for (int i = 0; i < len; i++) {
  70             char c = pathname.charAt(i);
  71             if (c == CHAR_NUL) continue;
  72             if ((prevChar == '/') && (c == '/')) continue;
  73 
  74             sb.append(c);
  75             prevChar = c;
  76         }


  77 
  78         if (prevChar == '/' && sb.length() > 1)
  79             sb.setLength(sb.length() - 1);
  80 
  81         return sb.toString();










  82     }
  83 
  84     public int prefixLength(String pathname) {
  85         if (pathname.length() == 0) return 0;
  86         return (pathname.charAt(0) == '/') ? 1 : 0;
  87     }
  88 
  89     public String resolve(String parent, String child) {
  90         if (child.equals("")) return parent;
  91         if (child.charAt(0) == '/') {
  92             if (parent.equals("/")) return child;
  93             return parent + child;
  94         }
  95         if (parent.equals("/")) return parent + child;
  96         return parent + '/' + child;
  97     }
  98 
  99     public String getDefaultParent() {
 100         return "/";
 101     }