< prev index next >

src/java.base/share/classes/java/nio/file/TempFileHelper.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  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 java.nio.file;
  27 
  28 import java.util.Set;
  29 import java.util.EnumSet;
  30 import java.security.SecureRandom;
  31 import static java.security.AccessController.*;
  32 import java.io.IOException;
  33 import java.nio.file.attribute.FileAttribute;
  34 import java.nio.file.attribute.PosixFilePermission;
  35 import java.nio.file.attribute.PosixFilePermissions;
  36 import static java.nio.file.attribute.PosixFilePermission.*;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 
  40 /**
  41  * Helper class to support creation of temporary files and directories with
  42  * initial attributes.
  43  */
  44 
  45 class TempFileHelper {
  46     private TempFileHelper() { }
  47 
  48     // temporary directory location
  49     private static final Path tmpdir =
  50         Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));
  51 
  52     private static final boolean isPosix =
  53         FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
  54 
  55     // file name generation, same as java.io.File for now
  56     private static final SecureRandom random = new SecureRandom();
  57     private static Path generatePath(String prefix, String suffix, Path dir) {
  58         long n = random.nextLong();
  59         n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n);
  60         Path name = dir.getFileSystem().getPath(prefix + Long.toString(n) + suffix);
  61         // the generated name should be a simple file name
  62         if (name.getParent() != null)
  63             throw new IllegalArgumentException("Invalid prefix or suffix");
  64         return dir.resolve(name);
  65     }
  66 
  67     // default file and directory permissions (lazily initialized)
  68     private static class PosixPermissions {
  69         static final FileAttribute<Set<PosixFilePermission>> filePermissions =
  70             PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE));




  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 java.nio.file;
  27 
  28 import java.util.Set;
  29 import java.util.EnumSet;
  30 import java.security.SecureRandom;

  31 import java.io.IOException;
  32 import java.nio.file.attribute.FileAttribute;
  33 import java.nio.file.attribute.PosixFilePermission;
  34 import java.nio.file.attribute.PosixFilePermissions;
  35 import static java.nio.file.attribute.PosixFilePermission.*;
  36 import sun.security.action.GetPropertyAction;
  37 
  38 
  39 /**
  40  * Helper class to support creation of temporary files and directories with
  41  * initial attributes.
  42  */
  43 
  44 class TempFileHelper {
  45     private TempFileHelper() { }
  46 
  47     // temporary directory location
  48     private static final Path tmpdir =
  49         Paths.get(GetPropertyAction.getProperty("java.io.tmpdir"));
  50 
  51     private static final boolean isPosix =
  52         FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
  53 
  54     // file name generation, same as java.io.File for now
  55     private static final SecureRandom random = new SecureRandom();
  56     private static Path generatePath(String prefix, String suffix, Path dir) {
  57         long n = random.nextLong();
  58         n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n);
  59         Path name = dir.getFileSystem().getPath(prefix + Long.toString(n) + suffix);
  60         // the generated name should be a simple file name
  61         if (name.getParent() != null)
  62             throw new IllegalArgumentException("Invalid prefix or suffix");
  63         return dir.resolve(name);
  64     }
  65 
  66     // default file and directory permissions (lazily initialized)
  67     private static class PosixPermissions {
  68         static final FileAttribute<Set<PosixFilePermission>> filePermissions =
  69             PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE));


< prev index next >