< prev index next >

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

Print this page




  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.privilegedGetProperty("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         String s = prefix + Long.toUnsignedString(n) + suffix;
  59         Path name = dir.getFileSystem().getPath(s);
  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));




  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         Path.get(GetPropertyAction.privilegedGetProperty("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         String s = prefix + Long.toUnsignedString(n) + suffix;
  59         Path name = dir.getFileSystem().getPath(s);
  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 >