コード例 #1
0
  /**
   * Open/creates file, returning FileChannel to access the file
   *
   * @param pathForWindows The path of the file to open/create
   * @param pathToCheck The path used for permission checks (if security manager)
   */
  static FileChannel newFileChannel(
      String pathForWindows,
      String pathToCheck,
      Set<? extends OpenOption> options,
      long pSecurityDescriptor)
      throws WindowsException {
    Flags flags = Flags.toFlags(options);

    // default is reading; append => writing
    if (!flags.read && !flags.write) {
      if (flags.append) {
        flags.write = true;
      } else {
        flags.read = true;
      }
    }

    // validation
    if (flags.read && flags.append) throw new IllegalArgumentException("READ + APPEND not allowed");
    if (flags.append && flags.truncateExisting)
      throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");

    FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
    return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
  }
コード例 #2
0
 static Flags toFlags(Set<? extends OpenOption> options) {
   Flags flags = new Flags();
   for (OpenOption option : options) {
     if (option instanceof StandardOpenOption) {
       switch ((StandardOpenOption) option) {
         case READ:
           flags.read = true;
           break;
         case WRITE:
           flags.write = true;
           break;
         case APPEND:
           flags.append = true;
           break;
         case TRUNCATE_EXISTING:
           flags.truncateExisting = true;
           break;
         case CREATE:
           flags.create = true;
           break;
         case CREATE_NEW:
           flags.createNew = true;
           break;
         case DELETE_ON_CLOSE:
           flags.deleteOnClose = true;
           break;
         case SPARSE:
           flags.sparse = true;
           break;
         case SYNC:
           flags.sync = true;
           break;
         case DSYNC:
           flags.dsync = true;
           break;
         default:
           throw new UnsupportedOperationException();
       }
       continue;
     }
     if (option instanceof ExtendedOpenOption) {
       switch ((ExtendedOpenOption) option) {
         case NOSHARE_READ:
           flags.shareRead = false;
           break;
         case NOSHARE_WRITE:
           flags.shareWrite = false;
           break;
         case NOSHARE_DELETE:
           flags.shareDelete = false;
           break;
         default:
           throw new UnsupportedOperationException();
       }
       continue;
     }
     if (option == LinkOption.NOFOLLOW_LINKS) {
       flags.noFollowLinks = true;
       continue;
     }
     if (option == OPEN_REPARSE_POINT) {
       flags.openReparsePoint = true;
       continue;
     }
     if (option == null) throw new NullPointerException();
     throw new UnsupportedOperationException();
   }
   return flags;
 }