diff --git a/src/share/classes/sun/nio/fs/Util.java b/src/share/classes/sun/nio/fs/Util.java index 1fe134f9af87bef3e87662f2a84e6639d6c544c7..e9abc6f06b592bda39d169ae6345ee96768b8945 100644 --- a/src/share/classes/sun/nio/fs/Util.java +++ b/src/share/classes/sun/nio/fs/Util.java @@ -27,6 +27,9 @@ package sun.nio.fs; 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 @@ import java.nio.file.*; 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 diff --git a/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java b/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java index af3a47440e8ba7067dceb0139f333733a46cde48..907f884444b8155541a8da8813e4b05419263342 100644 --- a/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java +++ b/src/solaris/classes/sun/nio/fs/GnomeFileTypeDetector.java @@ -70,12 +70,12 @@ public class GnomeFileTypeDetector // 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 { diff --git a/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java b/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java index 4a97e094243ab60fe7260996ff38f19e5924af44..0a3f32e2f660b413146e976385f8bb21d138db15 100644 --- a/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java +++ b/src/solaris/classes/sun/nio/fs/LinuxDosFileAttributeView.java @@ -51,7 +51,7 @@ class LinuxDosFileAttributeView 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 @@ class LinuxDosFileAttributeView 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 @@ class LinuxDosFileAttributeView 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, diff --git a/src/solaris/classes/sun/nio/fs/LinuxFileStore.java b/src/solaris/classes/sun/nio/fs/LinuxFileStore.java index 3f0bcb9976d54364f1afe318b5b0158b9e6c07fb..607361336ee2f9749e0dd879351870d710ee8c82 100644 --- a/src/solaris/classes/sun/nio/fs/LinuxFileStore.java +++ b/src/solaris/classes/sun/nio/fs/LinuxFileStore.java @@ -98,7 +98,8 @@ class LinuxFileStore 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 diff --git a/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java b/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java index eff7b5884193d68dea6a1f9702554df2522ed05c..54d8347a2f6062737e5ecb5271ae0fa277701b3d 100644 --- a/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java +++ b/src/solaris/classes/sun/nio/fs/LinuxFileSystem.java @@ -78,7 +78,7 @@ class LinuxFileSystem extends UnixFileSystem { Iterable getMountEntries(String fstab) { ArrayList 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(); diff --git a/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java b/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java index 46e7f2cf6f946a27ca0f45b6c2d6472a65446dbb..fec8d5eb3746c32d3a9a25943b14e9a3d37870d5 100644 --- a/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java +++ b/src/solaris/classes/sun/nio/fs/LinuxUserDefinedFileAttributeView.java @@ -53,7 +53,7 @@ class LinuxUserDefinedFileAttributeView 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 @@ class LinuxUserDefinedFileAttributeView 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); diff --git a/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java b/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java index 2412e04d98787c6537b431c8145fa283cd71d7fa..334aac548d68e329f2d6ca5a66665c00df73c811 100644 --- a/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java +++ b/src/solaris/classes/sun/nio/fs/SolarisUserDefinedFileAttributeView.java @@ -42,16 +42,15 @@ import static sun.nio.fs.SolarisConstants.*; 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 @@ class SolarisUserDefinedFileAttributeView 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 @@ class SolarisUserDefinedFileAttributeView 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 @@ class SolarisUserDefinedFileAttributeView 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 @@ class SolarisUserDefinedFileAttributeView 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); diff --git a/src/solaris/classes/sun/nio/fs/UnixException.java b/src/solaris/classes/sun/nio/fs/UnixException.java index b4925c35da74577dbb70480bac0d571595cfe17f..80174c76b489fabf0e4ba0d1f59f705eb47b64ee 100644 --- a/src/solaris/classes/sun/nio/fs/UnixException.java +++ b/src/solaris/classes/sun/nio/fs/UnixException.java @@ -61,7 +61,7 @@ class UnixException extends Exception { if (msg != null) { return msg; } else { - return new String(UnixNativeDispatcher.strerror(errno())); + return Util.toString(UnixNativeDispatcher.strerror(errno())); } } diff --git a/src/solaris/classes/sun/nio/fs/UnixFileStore.java b/src/solaris/classes/sun/nio/fs/UnixFileStore.java index 4299b2668d0be5bfbd9fb3d701a4fe146f545677..38465fbdad2e87e792913f5c5a83a78a7418412e 100644 --- a/src/solaris/classes/sun/nio/fs/UnixFileStore.java +++ b/src/solaris/classes/sun/nio/fs/UnixFileStore.java @@ -196,7 +196,7 @@ abstract class UnixFileStore @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(")"); diff --git a/src/solaris/classes/sun/nio/fs/UnixFileSystem.java b/src/solaris/classes/sun/nio/fs/UnixFileSystem.java index 21aefc183df53ea8c8bf8cc6c320b1cb2715db70..c7111e963cbeee0d1871245351d42e56e071d2f9 100644 --- a/src/solaris/classes/sun/nio/fs/UnixFileSystem.java +++ b/src/solaris/classes/sun/nio/fs/UnixFileSystem.java @@ -49,7 +49,7 @@ abstract class UnixFileSystem // 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 @@ abstract class UnixFileSystem SecurityManager sm = System.getSecurityManager(); if (sm != null) { try { - sm.checkRead(new String(entry.dir())); + sm.checkRead(Util.toString(entry.dir())); } catch (SecurityException x) { continue; } diff --git a/src/solaris/classes/sun/nio/fs/UnixMountEntry.java b/src/solaris/classes/sun/nio/fs/UnixMountEntry.java index 8a46e4570b94684fb7fe37b905d1af193544fffb..af23322d9e58d67c089d0276d1a1084b88f86f71 100644 --- a/src/solaris/classes/sun/nio/fs/UnixMountEntry.java +++ b/src/solaris/classes/sun/nio/fs/UnixMountEntry.java @@ -43,12 +43,12 @@ class UnixMountEntry { } 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 @@ class UnixMountEntry { */ 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; diff --git a/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java b/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java index ec21e6df8efc5d0e11eb9b79f8623a8b7a213497..043a7c0f8ee82eddf4656e4717966654ef18c68d 100644 --- a/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java +++ b/src/solaris/classes/sun/nio/fs/UnixNativeDispatcher.java @@ -100,7 +100,7 @@ class UnixNativeDispatcher { */ 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 @@ class UnixNativeDispatcher { * @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 @@ class UnixNativeDispatcher { * @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 { diff --git a/src/solaris/classes/sun/nio/fs/UnixPath.java b/src/solaris/classes/sun/nio/fs/UnixPath.java index 5738292027d1ea26be16b8981aab8ef039574c02..aa04538cb20e106ca2740ba6c4aedd585a556a20 100644 --- a/src/solaris/classes/sun/nio/fs/UnixPath.java +++ b/src/solaris/classes/sun/nio/fs/UnixPath.java @@ -120,7 +120,7 @@ class UnixPath SoftReference 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(ce)); @@ -186,7 +186,7 @@ class UnixPath // 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 @@ class UnixPath 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; } diff --git a/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java b/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java index 163dbddb475ee7aeec55f884e0c44d1199b5a019..187bb23ac89d54896637a7324b8096b79ae7ef5a 100644 --- a/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java +++ b/src/solaris/classes/sun/nio/fs/UnixUserPrincipals.java @@ -115,7 +115,7 @@ class UnixUserPrincipals { 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 @@ class UnixUserPrincipals { 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); }