1 /* 2 * Copyright (c) 2008, 2015, 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 sun.nio.fs; 27 28 import java.nio.*; 29 import java.nio.file.*; 30 import java.nio.charset.*; 31 import java.io.*; 32 import java.net.URI; 33 import java.util.*; 34 import java.lang.ref.SoftReference; 35 36 import static sun.nio.fs.UnixNativeDispatcher.*; 37 import static sun.nio.fs.UnixConstants.*; 38 39 /** 40 * Solaris/Linux implementation of java.nio.file.Path 41 */ 42 43 class UnixPath implements Path { 44 private static ThreadLocal<SoftReference<CharsetEncoder>> encoder = 45 new ThreadLocal<SoftReference<CharsetEncoder>>(); 46 47 // FIXME - eliminate this reference to reduce space 48 private final UnixFileSystem fs; 49 50 // internal representation 51 private final byte[] path; 52 53 // String representation (created lazily) 54 private volatile String stringValue; 55 56 // cached hashcode (created lazily, no need to be volatile) 57 private int hash; 722 } else { 723 // this path has more elements so given path must be relative 724 if (that.isAbsolute()) 725 return false; 726 } 727 } 728 729 // compare bytes 730 int thisPos = offsets[thisOffsetCount - thatOffsetCount]; 731 int thatPos = that.offsets[0]; 732 if ((thatLen - thatPos) != (thisLen - thisPos)) 733 return false; 734 while (thatPos < thatLen) { 735 if (this.path[thisPos++] != that.path[thatPos++]) 736 return false; 737 } 738 739 return true; 740 } 741 742 @Override 743 public int compareTo(Path other) { 744 int len1 = path.length; 745 int len2 = ((UnixPath) other).path.length; 746 747 int n = Math.min(len1, len2); 748 byte v1[] = path; 749 byte v2[] = ((UnixPath) other).path; 750 751 int k = 0; 752 while (k < n) { 753 int c1 = v1[k] & 0xff; 754 int c2 = v2[k] & 0xff; 755 if (c1 != c2) { 756 return c1 - c2; 757 } 758 k++; 759 } 760 return len1 - len2; 761 } | 1 /* 2 * Copyright (c) 2008, 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 sun.nio.fs; 27 28 import java.io.IOException; 29 import java.lang.ref.SoftReference; 30 import java.net.URI; 31 import java.nio.ByteBuffer; 32 import java.nio.CharBuffer; 33 import java.nio.charset.CharsetEncoder; 34 import java.nio.charset.CoderResult; 35 import java.nio.charset.CodingErrorAction; 36 import java.nio.file.InvalidPathException; 37 import java.nio.file.LinkOption; 38 import java.nio.file.ProviderMismatchException; 39 import java.nio.file.Path; 40 import java.nio.file.WatchEvent; 41 import java.nio.file.WatchKey; 42 import java.nio.file.WatchService; 43 import java.util.Arrays; 44 import java.util.Objects; 45 import java.util.stream.Stream; 46 47 import static sun.nio.fs.UnixConstants.EINVAL; 48 import static sun.nio.fs.UnixConstants.ELOOP; 49 import static sun.nio.fs.UnixConstants.O_NOFOLLOW; 50 import static sun.nio.fs.UnixConstants.O_RDONLY; 51 import static sun.nio.fs.UnixNativeDispatcher.open; 52 import static sun.nio.fs.UnixNativeDispatcher.realpath; 53 54 /** 55 * Solaris/Linux implementation of java.nio.file.Path 56 */ 57 58 class UnixPath implements Path { 59 private static ThreadLocal<SoftReference<CharsetEncoder>> encoder = 60 new ThreadLocal<SoftReference<CharsetEncoder>>(); 61 62 // FIXME - eliminate this reference to reduce space 63 private final UnixFileSystem fs; 64 65 // internal representation 66 private final byte[] path; 67 68 // String representation (created lazily) 69 private volatile String stringValue; 70 71 // cached hashcode (created lazily, no need to be volatile) 72 private int hash; 737 } else { 738 // this path has more elements so given path must be relative 739 if (that.isAbsolute()) 740 return false; 741 } 742 } 743 744 // compare bytes 745 int thisPos = offsets[thisOffsetCount - thatOffsetCount]; 746 int thatPos = that.offsets[0]; 747 if ((thatLen - thatPos) != (thisLen - thisPos)) 748 return false; 749 while (thatPos < thatLen) { 750 if (this.path[thisPos++] != that.path[thatPos++]) 751 return false; 752 } 753 754 return true; 755 } 756 757 private String getExtension() { 758 final int length = path.length; 759 int index = length; 760 int lastDotIndex = -1; 761 while (--index >= 0) { 762 byte b = path[index]; 763 if (b == '.') { 764 lastDotIndex = index; 765 break; 766 } else if (b == '/') { 767 break; 768 } 769 } 770 771 if (lastDotIndex < 0 || lastDotIndex == length - 1) { 772 return null; 773 } 774 775 int offset = lastDotIndex + 1; 776 return Util.toString(path, offset, length - offset); 777 } 778 779 @Override 780 public boolean hasExtension(String... extensions) { 781 Objects.requireNonNull(extensions); 782 783 String extension = getExtension(); 784 if (extension == null) { 785 return false; 786 } 787 788 return Stream.of(extensions).anyMatch(extension::equals); 789 } 790 791 @Override 792 public int compareTo(Path other) { 793 int len1 = path.length; 794 int len2 = ((UnixPath) other).path.length; 795 796 int n = Math.min(len1, len2); 797 byte v1[] = path; 798 byte v2[] = ((UnixPath) other).path; 799 800 int k = 0; 801 while (k < n) { 802 int c1 = v1[k] & 0xff; 803 int c2 = v2[k] & 0xff; 804 if (c1 != c2) { 805 return c1 - c2; 806 } 807 k++; 808 } 809 return len1 - len2; 810 } |