jdk/src/share/classes/java/io/FilePermission.java
changeset 13795 73850c397272
parent 9035 1255eb81cc2f
child 14014 da3648e13e67
child 14226 3c870f6d9139
equal deleted inserted replaced
13794:73e854afd84d 13795:73850c397272
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    29 import java.util.Enumeration;
    29 import java.util.Enumeration;
    30 import java.util.List;
    30 import java.util.List;
    31 import java.util.ArrayList;
    31 import java.util.ArrayList;
    32 import java.util.Vector;
    32 import java.util.Vector;
    33 import java.util.Collections;
    33 import java.util.Collections;
    34 import java.io.ObjectStreamField;
       
    35 import java.io.ObjectOutputStream;
       
    36 import java.io.ObjectInputStream;
       
    37 import java.io.IOException;
       
    38 import sun.security.util.SecurityConstants;
    34 import sun.security.util.SecurityConstants;
    39 
    35 
    40 /**
    36 /**
    41  * This class represents access to a file or directory.  A FilePermission consists
    37  * This class represents access to a file or directory.  A FilePermission consists
    42  * of a pathname and a set of actions valid for that pathname.
    38  * of a pathname and a set of actions valid for that pathname.
   422     }
   418     }
   423 
   419 
   424     /**
   420     /**
   425      * Converts an actions String to an actions mask.
   421      * Converts an actions String to an actions mask.
   426      *
   422      *
   427      * @param action the action string.
   423      * @param actions the action string.
   428      * @return the actions mask.
   424      * @return the actions mask.
   429      */
   425      */
   430     private static int getMask(String actions) {
   426     private static int getMask(String actions) {
   431 
   427 
   432         int mask = NONE;
   428         int mask = NONE;
   433 
   429 
   434         // Null action valid?
   430         // Null action valid?
   435         if (actions == null) {
   431         if (actions == null) {
   436             return mask;
   432             return mask;
   437         }
   433         }
   438         // Check against use of constants (used heavily within the JDK)
   434 
       
   435         // Use object identity comparison against known-interned strings for
       
   436         // performance benefit (these values are used heavily within the JDK).
   439         if (actions == SecurityConstants.FILE_READ_ACTION) {
   437         if (actions == SecurityConstants.FILE_READ_ACTION) {
   440             return READ;
   438             return READ;
   441         } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
   439         } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
   442             return WRITE;
   440             return WRITE;
   443         } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
   441         } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
   529             boolean seencomma = false;
   527             boolean seencomma = false;
   530             while (i >= matchlen && !seencomma) {
   528             while (i >= matchlen && !seencomma) {
   531                 switch(a[i-matchlen]) {
   529                 switch(a[i-matchlen]) {
   532                 case ',':
   530                 case ',':
   533                     seencomma = true;
   531                     seencomma = true;
   534                     /*FALLTHROUGH*/
   532                     break;
   535                 case ' ': case '\r': case '\n':
   533                 case ' ': case '\r': case '\n':
   536                 case '\f': case '\t':
   534                 case '\f': case '\t':
   537                     break;
   535                     break;
   538                 default:
   536                 default:
   539                     throw new IllegalArgumentException(
   537                     throw new IllegalArgumentException(
   796      * container.
   794      * container.
   797      *
   795      *
   798      * @return an enumeration of all the FilePermission objects.
   796      * @return an enumeration of all the FilePermission objects.
   799      */
   797      */
   800 
   798 
   801     public Enumeration elements() {
   799     public Enumeration<Permission> elements() {
   802         // Convert Iterator into Enumeration
   800         // Convert Iterator into Enumeration
   803         synchronized (this) {
   801         synchronized (this) {
   804             return Collections.enumeration(perms);
   802             return Collections.enumeration(perms);
   805         }
   803         }
   806     }
   804     }
   841     }
   839     }
   842 
   840 
   843     /*
   841     /*
   844      * Reads in a Vector of FilePermissions and saves them in the perms field.
   842      * Reads in a Vector of FilePermissions and saves them in the perms field.
   845      */
   843      */
   846     @SuppressWarnings("unchecked")
       
   847     private void readObject(ObjectInputStream in) throws IOException,
   844     private void readObject(ObjectInputStream in) throws IOException,
   848     ClassNotFoundException {
   845     ClassNotFoundException {
   849         // Don't call defaultReadObject()
   846         // Don't call defaultReadObject()
   850 
   847 
   851         // Read in serialized fields
   848         // Read in serialized fields
   852         ObjectInputStream.GetField gfields = in.readFields();
   849         ObjectInputStream.GetField gfields = in.readFields();
   853 
   850 
   854         // Get the one we want
   851         // Get the one we want
       
   852         @SuppressWarnings("unchecked")
   855         Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
   853         Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
   856         perms = new ArrayList<>(permissions.size());
   854         perms = new ArrayList<>(permissions.size());
   857         perms.addAll(permissions);
   855         perms.addAll(permissions);
   858     }
   856     }
   859 }
   857 }