7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
authoralanb
Fri, 18 Oct 2013 13:51:12 +0100
changeset 21290 db5e0cad1102
parent 21289 95fcae9543b4
child 21291 64a9ea14762c
child 21292 f55838e2d1e0
7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037 Reviewed-by: sherman, ulfzibis
jdk/src/share/classes/sun/nio/fs/Util.java
jdk/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java
jdk/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java
jdk/src/solaris/classes/sun/nio/fs/LinuxFileStore.java
jdk/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java
jdk/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java
jdk/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java
jdk/src/solaris/classes/sun/nio/fs/UnixException.java
jdk/src/solaris/classes/sun/nio/fs/UnixFileStore.java
jdk/src/solaris/classes/sun/nio/fs/UnixFileSystem.java
jdk/src/solaris/classes/sun/nio/fs/UnixMountEntry.java
jdk/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java
jdk/src/solaris/classes/sun/nio/fs/UnixPath.java
jdk/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java
--- a/jdk/src/share/classes/sun/nio/fs/Util.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/share/classes/sun/nio/fs/Util.java	Fri Oct 18 13:51:12 2013 +0100
@@ -27,6 +27,9 @@
 
 import java.util.*;
 import java.nio.file.*;
+import java.nio.charset.Charset;
+import java.security.*;
+import sun.security.action.*;
 
 /**
  * Utility methods
@@ -35,6 +38,33 @@
 class Util {
     private Util() { }
 
+    private static final Charset jnuEncoding = Charset.forName(
+        AccessController.doPrivileged(new GetPropertyAction("sun.jnu.encoding")));
+
+    /**
+     * Returns {@code Charset} corresponding to the sun.jnu.encoding property
+     */
+    static Charset jnuEncoding() {
+        return jnuEncoding;
+    }
+
+    /**
+     * Encodes the given String into a sequence of bytes using the {@code Charset}
+     * specified by the sun.jnu.encoding property.
+     */
+    static byte[] toBytes(String s) {
+        return s.getBytes(jnuEncoding);
+    }
+
+    /**
+     * Constructs a new String by decoding the specified array of bytes using the
+     * {@code Charset} specified by the sun.jnu.encoding property.
+     */
+    static String toString(byte[] bytes) {
+        return new String(bytes, jnuEncoding);
+    }
+
+
     /**
      * Splits a string around the given character. The array returned by this
      * method contains each substring that is terminated by the character. Use
--- a/jdk/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java	Fri Oct 18 13:51:12 2013 +0100
@@ -70,12 +70,12 @@
                 // GIO may access file so need permission check
                 path.checkRead();
                 byte[] type = probeUsingGio(buffer.address());
-                return (type == null) ? null : new String(type);
+                return (type == null) ? null : Util.toString(type);
             } else {
                 byte[] type = probeUsingGnomeVfs(buffer.address());
                 if (type == null)
                     return null;
-                String s = new String(type);
+                String s = Util.toString(type);
                 return s.equals(GNOME_VFS_MIME_TYPE_UNKNOWN) ? null : s;
             }
         } finally {
--- a/jdk/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java	Fri Oct 18 13:51:12 2013 +0100
@@ -51,7 +51,7 @@
     private static final String HIDDEN_NAME = "hidden";
 
     private static final String DOS_XATTR_NAME = "user.DOSATTRIB";
-    private static final byte[] DOS_XATTR_NAME_AS_BYTES = DOS_XATTR_NAME.getBytes();
+    private static final byte[] DOS_XATTR_NAME_AS_BYTES = Util.toBytes(DOS_XATTR_NAME);
 
     private static final int DOS_XATTR_READONLY = 0x01;
     private static final int DOS_XATTR_HIDDEN   = 0x02;
@@ -225,7 +225,7 @@
                 byte[] buf = new byte[len];
                 unsafe.copyMemory(null, buffer.address(), buf,
                     Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
-                String value = new String(buf); // platform encoding
+                String value = Util.toString(buf);
 
                 // should be something like 0x20
                 if (value.length() >= 3 && value.startsWith("0x")) {
@@ -263,7 +263,7 @@
                 newValue &= ~flag;
             }
             if (newValue != oldValue) {
-                byte[] value = ("0x" + Integer.toHexString(newValue)).getBytes();
+                byte[] value = Util.toBytes("0x" + Integer.toHexString(newValue));
                 NativeBuffer buffer = NativeBuffers.asNativeBuffer(value);
                 try {
                     LinuxNativeDispatcher.fsetxattr(fd, DOS_XATTR_NAME_AS_BYTES,
--- a/jdk/src/solaris/classes/sun/nio/fs/LinuxFileStore.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/LinuxFileStore.java	Fri Oct 18 13:51:12 2013 +0100
@@ -98,7 +98,8 @@
             int fd = path.openForAttributeAccess(false);
             try {
                 // fgetxattr returns size if called with size==0
-                LinuxNativeDispatcher.fgetxattr(fd, "user.java".getBytes(), 0L, 0);
+                byte[] name = Util.toBytes("user.java");
+                LinuxNativeDispatcher.fgetxattr(fd, name, 0L, 0);
                 return true;
             } catch (UnixException e) {
                 // attribute does not exist
--- a/jdk/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java	Fri Oct 18 13:51:12 2013 +0100
@@ -78,7 +78,7 @@
     Iterable<UnixMountEntry> getMountEntries(String fstab) {
         ArrayList<UnixMountEntry> entries = new ArrayList<>();
         try {
-            long fp = setmntent(fstab.getBytes(), "r".getBytes());
+            long fp = setmntent(Util.toBytes(fstab), Util.toBytes("r"));
             try {
                 for (;;) {
                     UnixMountEntry entry = new UnixMountEntry();
--- a/jdk/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java	Fri Oct 18 13:51:12 2013 +0100
@@ -53,7 +53,7 @@
         if (name == null)
             throw new NullPointerException("'name' is null");
         name = USER_NAMESPACE + name;
-        byte[] bytes = name.getBytes();
+        byte[] bytes = Util.toBytes(name);
         if (bytes.length > XATTR_NAME_MAX) {
             throw new FileSystemException(file.getPathForExceptionMessage(),
                 null, "'" + name + "' is too big");
@@ -72,7 +72,7 @@
                 byte[] value = new byte[len];
                 unsafe.copyMemory(null, address+start, value,
                     Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
-                String s = new String(value);
+                String s = Util.toString(value);
                 if (s.startsWith(USER_NAMESPACE)) {
                     s = s.substring(USER_NAMESPACE.length());
                     list.add(s);
--- a/jdk/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java	Fri Oct 18 13:51:12 2013 +0100
@@ -42,16 +42,15 @@
 class SolarisUserDefinedFileAttributeView
     extends AbstractUserDefinedFileAttributeView
 {
+    private static final byte[] HERE = { '.' };
+
     private byte[] nameAsBytes(UnixPath file, String name) throws IOException {
-        byte[] bytes = name.getBytes();
+        byte[] bytes = Util.toBytes(name);
         //  "", "." and ".." not allowed
-        if (bytes.length == 0 || bytes[0] == '.') {
-            if (bytes.length <= 1 ||
-                (bytes.length == 2 && bytes[1] == '.'))
-            {
-                throw new FileSystemException(file.getPathForExceptionMessage(),
-                    null, "'" + name + "' is not a valid name");
-            }
+        if ((bytes.length == 0 || bytes[0] == '.') &&
+            ((bytes.length <= 1 || (bytes.length == 2 && bytes[1] == '.')) {
+            throw new FileSystemException(file.getPathForExceptionMessage(),
+                null, "'" + name + "' is not a valid name");
         }
         return bytes;
     }
@@ -73,7 +72,7 @@
         try {
             try {
                 // open extended attribute directory
-                int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
+                int dfd = openat(fd, HERE, (O_RDONLY|O_XATTR), 0);
                 long dp;
                 try {
                     dp = fdopendir(dfd);
@@ -87,7 +86,7 @@
                 try {
                     byte[] name;
                     while ((name = readdir(dp)) != null) {
-                        String s = new String(name);
+                        String s = Util.toString(name);
                         if (!s.equals(".") && !s.equals(".."))
                             list.add(s);
                     }
@@ -217,7 +216,7 @@
 
         int fd = file.openForAttributeAccess(followLinks);
         try {
-            int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
+            int dfd = openat(fd, HERE, (O_RDONLY|O_XATTR), 0);
             try {
                 unlinkat(dfd, nameAsBytes(file,name), 0);
             } finally {
@@ -243,7 +242,7 @@
     static void copyExtendedAttributes(int ofd, int nfd) {
         try {
             // open extended attribute directory
-            int dfd = openat(ofd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
+            int dfd = openat(ofd, HERE, (O_RDONLY|O_XATTR), 0);
             long dp = 0L;
             try {
                 dp = fdopendir(dfd);
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixException.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixException.java	Fri Oct 18 13:51:12 2013 +0100
@@ -61,7 +61,7 @@
         if (msg != null) {
             return msg;
         } else {
-            return new String(UnixNativeDispatcher.strerror(errno()));
+            return Util.toString(UnixNativeDispatcher.strerror(errno()));
         }
     }
 
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixFileStore.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixFileStore.java	Fri Oct 18 13:51:12 2013 +0100
@@ -196,7 +196,7 @@
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(new String(entry.dir()));
+        StringBuilder sb = new StringBuilder(Util.toString(entry.dir()));
         sb.append(" (");
         sb.append(entry.name());
         sb.append(")");
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixFileSystem.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixFileSystem.java	Fri Oct 18 13:51:12 2013 +0100
@@ -49,7 +49,7 @@
     // package-private
     UnixFileSystem(UnixFileSystemProvider provider, String dir) {
         this.provider = provider;
-        this.defaultDirectory = UnixPath.normalizeAndCheck(dir).getBytes();
+        this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
         if (this.defaultDirectory[0] != '/') {
             throw new RuntimeException("default directory must be absolute");
         }
@@ -204,7 +204,7 @@
                 SecurityManager sm = System.getSecurityManager();
                 if (sm != null) {
                     try {
-                        sm.checkRead(new String(entry.dir()));
+                        sm.checkRead(Util.toString(entry.dir()));
                     } catch (SecurityException x) {
                         continue;
                     }
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixMountEntry.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixMountEntry.java	Fri Oct 18 13:51:12 2013 +0100
@@ -43,12 +43,12 @@
     }
 
     String name() {
-        return new String(name);
+        return Util.toString(name);
     }
 
     String fstype() {
         if (fstypeAsString == null)
-            fstypeAsString = new String(fstype);
+            fstypeAsString = Util.toString(fstype);
         return fstypeAsString;
     }
 
@@ -65,7 +65,7 @@
      */
     boolean hasOption(String requested) {
         if (optionsAsString == null)
-            optionsAsString = new String(opts);
+            optionsAsString = Util.toString(opts);
         for (String opt: Util.split(optionsAsString, ',')) {
             if (opt.equals(requested))
                 return true;
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java	Fri Oct 18 13:51:12 2013 +0100
@@ -100,7 +100,7 @@
      */
     static long fopen(UnixPath filename, String mode) throws UnixException {
         NativeBuffer pathBuffer = copyToNativeBuffer(filename);
-        NativeBuffer modeBuffer = NativeBuffers.asNativeBuffer(mode.getBytes());
+        NativeBuffer modeBuffer = NativeBuffers.asNativeBuffer(Util.toBytes(mode));
         try {
             return fopen0(pathBuffer.address(), modeBuffer.address());
         } finally {
@@ -473,7 +473,7 @@
      * @return  passwd->pw_uid
      */
     static int getpwnam(String name) throws UnixException {
-        NativeBuffer buffer = NativeBuffers.asNativeBuffer(name.getBytes());
+        NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));
         try {
             return getpwnam0(buffer.address());
         } finally {
@@ -488,7 +488,7 @@
      * @return  group->gr_name
      */
     static int getgrnam(String name) throws UnixException {
-        NativeBuffer buffer = NativeBuffers.asNativeBuffer(name.getBytes());
+        NativeBuffer buffer = NativeBuffers.asNativeBuffer(Util.toBytes(name));
         try {
             return getgrnam0(buffer.address());
         } finally {
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixPath.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixPath.java	Fri Oct 18 13:51:12 2013 +0100
@@ -120,7 +120,7 @@
         SoftReference<CharsetEncoder> ref = encoder.get();
         CharsetEncoder ce = (ref != null) ? ref.get() : null;
         if (ce == null) {
-            ce = Charset.defaultCharset().newEncoder()
+            ce = Util.jnuEncoding().newEncoder()
                 .onMalformedInput(CodingErrorAction.REPORT)
                 .onUnmappableCharacter(CodingErrorAction.REPORT);
             encoder.set(new SoftReference<CharsetEncoder>(ce));
@@ -186,7 +186,7 @@
     // use this path for permission checks
     String getPathForPermissionCheck() {
         if (getFileSystem().needToResolveAgainstDefaultDirectory()) {
-            return new String(getByteArrayForSysCalls());
+            return Util.toString(getByteArrayForSysCalls());
         } else {
             return toString();
         }
@@ -758,7 +758,7 @@
     public String toString() {
         // OK if two or more threads create a String
         if (stringValue == null) {
-            stringValue = fs.normalizeJavaPath(new String(path));     // platform encoding
+            stringValue = fs.normalizeJavaPath(Util.toString(path));     // platform encoding
         }
         return stringValue;
     }
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java	Fri Oct 18 16:06:20 2013 +0400
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java	Fri Oct 18 13:51:12 2013 +0100
@@ -115,7 +115,7 @@
     static User fromUid(int uid) {
         String name = null;
         try {
-            name = new String(getpwuid(uid));
+            name = Util.toString(getpwuid(uid));
         } catch (UnixException x) {
             name = Integer.toString(uid);
         }
@@ -126,7 +126,7 @@
     static Group fromGid(int gid) {
         String name = null;
         try {
-            name = new String(getgrgid(gid));
+            name = Util.toString(getgrgid(gid));
         } catch (UnixException x) {
             name = Integer.toString(gid);
         }