< prev index next >

src/java.base/share/classes/java/net/SocketPermission.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


 288      * @param action the action string.
 289      */
 290     public SocketPermission(String host, String action) {
 291         super(getHost(host));
 292         // name initialized to getHost(host); NPE detected in getHost()
 293         init(getName(), getMask(action));
 294     }
 295 
 296 
 297     SocketPermission(String host, int mask) {
 298         super(getHost(host));
 299         // name initialized to getHost(host); NPE detected in getHost()
 300         init(getName(), mask);
 301     }
 302 
 303     private void setDeny() {
 304         defaultDeny = true;
 305     }
 306 
 307     private static String getHost(String host) {
 308         if (host.equals("")) {
 309             return "localhost";
 310         } else {
 311             /* IPv6 literal address used in this context should follow
 312              * the format specified in RFC 2732;
 313              * if not, we try to solve the unambiguous case
 314              */
 315             int ind;
 316             if (host.charAt(0) != '[') {
 317                 if ((ind = host.indexOf(':')) != host.lastIndexOf(':')) {
 318                     /* More than one ":", meaning IPv6 address is not
 319                      * in RFC 2732 format;
 320                      * We will rectify user errors for all unambiguous cases
 321                      */
 322                     StringTokenizer st = new StringTokenizer(host, ":");
 323                     int tokens = st.countTokens();
 324                     if (tokens == 9) {
 325                         // IPv6 address followed by port
 326                         ind = host.lastIndexOf(':');
 327                         host = "[" + host.substring(0, ind) + "]" +
 328                             host.substring(ind);
 329                     } else if (tokens == 8 && host.indexOf("::") == -1) {
 330                         // IPv6 address only, not followed by port
 331                         host = "[" + host + "]";
 332                     } else {
 333                         // could be ambiguous
 334                         throw new IllegalArgumentException("Ambiguous"+
 335                                                            " hostport part");
 336                     }
 337                 }
 338             }
 339             return host;
 340         }
 341     }
 342 
 343     private int[] parsePort(String port)
 344         throws Exception
 345     {
 346 
 347         if (port == null || port.equals("") || port.equals("*")) {
 348             return new int[] {PORT_MIN, PORT_MAX};
 349         }
 350 
 351         int dash = port.indexOf('-');
 352 
 353         if (dash == -1) {
 354             int p = Integer.parseInt(port);
 355             return new int[] {p, p};
 356         } else {
 357             String low = port.substring(0, dash);
 358             String high = port.substring(dash+1);
 359             int l,h;
 360 
 361             if (low.equals("")) {
 362                 l = PORT_MIN;
 363             } else {
 364                 l = Integer.parseInt(low);
 365             }
 366 
 367             if (high.equals("")) {
 368                 h = PORT_MAX;
 369             } else {
 370                 h = Integer.parseInt(high);
 371             }
 372             if (l < 0 || h < 0 || h<l)
 373                 throw new IllegalArgumentException("invalid port range");
 374 
 375             return new int[] {l, h};
 376         }
 377     }
 378 
 379     /**
 380      * Returns true if the permission has specified zero
 381      * as its value (or lower bound) signifying the ephemeral range
 382      */
 383     private boolean includesEphemerals() {
 384         return portrange[0] == 0;
 385     }
 386 
 387     /**


 479                             invalid = true;
 480                         }
 481                     }
 482                 }
 483             }
 484         }
 485     }
 486 
 487     /**
 488      * Convert an action string to an integer actions mask.
 489      *
 490      * @param action the action string
 491      * @return the action mask
 492      */
 493     private static int getMask(String action) {
 494 
 495         if (action == null) {
 496             throw new NullPointerException("action can't be null");
 497         }
 498 
 499         if (action.equals("")) {
 500             throw new IllegalArgumentException("action can't be empty");
 501         }
 502 
 503         int mask = NONE;
 504 
 505         // Use object identity comparison against known-interned strings for
 506         // performance benefit (these values are used heavily within the JDK).
 507         if (action == SecurityConstants.SOCKET_RESOLVE_ACTION) {
 508             return RESOLVE;
 509         } else if (action == SecurityConstants.SOCKET_CONNECT_ACTION) {
 510             return CONNECT;
 511         } else if (action == SecurityConstants.SOCKET_LISTEN_ACTION) {
 512             return LISTEN;
 513         } else if (action == SecurityConstants.SOCKET_ACCEPT_ACTION) {
 514             return ACCEPT;
 515         } else if (action == SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION) {
 516             return CONNECT|ACCEPT;
 517         }
 518 
 519         char[] a = action.toCharArray();




 288      * @param action the action string.
 289      */
 290     public SocketPermission(String host, String action) {
 291         super(getHost(host));
 292         // name initialized to getHost(host); NPE detected in getHost()
 293         init(getName(), getMask(action));
 294     }
 295 
 296 
 297     SocketPermission(String host, int mask) {
 298         super(getHost(host));
 299         // name initialized to getHost(host); NPE detected in getHost()
 300         init(getName(), mask);
 301     }
 302 
 303     private void setDeny() {
 304         defaultDeny = true;
 305     }
 306 
 307     private static String getHost(String host) {
 308         if (host.isEmpty()) {
 309             return "localhost";
 310         } else {
 311             /* IPv6 literal address used in this context should follow
 312              * the format specified in RFC 2732;
 313              * if not, we try to solve the unambiguous case
 314              */
 315             int ind;
 316             if (host.charAt(0) != '[') {
 317                 if ((ind = host.indexOf(':')) != host.lastIndexOf(':')) {
 318                     /* More than one ":", meaning IPv6 address is not
 319                      * in RFC 2732 format;
 320                      * We will rectify user errors for all unambiguous cases
 321                      */
 322                     StringTokenizer st = new StringTokenizer(host, ":");
 323                     int tokens = st.countTokens();
 324                     if (tokens == 9) {
 325                         // IPv6 address followed by port
 326                         ind = host.lastIndexOf(':');
 327                         host = "[" + host.substring(0, ind) + "]" +
 328                             host.substring(ind);
 329                     } else if (tokens == 8 && host.indexOf("::") == -1) {
 330                         // IPv6 address only, not followed by port
 331                         host = "[" + host + "]";
 332                     } else {
 333                         // could be ambiguous
 334                         throw new IllegalArgumentException("Ambiguous"+
 335                                                            " hostport part");
 336                     }
 337                 }
 338             }
 339             return host;
 340         }
 341     }
 342 
 343     private int[] parsePort(String port)
 344         throws Exception
 345     {
 346 
 347         if (port == null || port.isEmpty() || port.equals("*")) {
 348             return new int[] {PORT_MIN, PORT_MAX};
 349         }
 350 
 351         int dash = port.indexOf('-');
 352 
 353         if (dash == -1) {
 354             int p = Integer.parseInt(port);
 355             return new int[] {p, p};
 356         } else {
 357             String low = port.substring(0, dash);
 358             String high = port.substring(dash+1);
 359             int l,h;
 360 
 361             if (low.isEmpty()) {
 362                 l = PORT_MIN;
 363             } else {
 364                 l = Integer.parseInt(low);
 365             }
 366 
 367             if (high.isEmpty()) {
 368                 h = PORT_MAX;
 369             } else {
 370                 h = Integer.parseInt(high);
 371             }
 372             if (l < 0 || h < 0 || h<l)
 373                 throw new IllegalArgumentException("invalid port range");
 374 
 375             return new int[] {l, h};
 376         }
 377     }
 378 
 379     /**
 380      * Returns true if the permission has specified zero
 381      * as its value (or lower bound) signifying the ephemeral range
 382      */
 383     private boolean includesEphemerals() {
 384         return portrange[0] == 0;
 385     }
 386 
 387     /**


 479                             invalid = true;
 480                         }
 481                     }
 482                 }
 483             }
 484         }
 485     }
 486 
 487     /**
 488      * Convert an action string to an integer actions mask.
 489      *
 490      * @param action the action string
 491      * @return the action mask
 492      */
 493     private static int getMask(String action) {
 494 
 495         if (action == null) {
 496             throw new NullPointerException("action can't be null");
 497         }
 498 
 499         if (action.isEmpty()) {
 500             throw new IllegalArgumentException("action can't be empty");
 501         }
 502 
 503         int mask = NONE;
 504 
 505         // Use object identity comparison against known-interned strings for
 506         // performance benefit (these values are used heavily within the JDK).
 507         if (action == SecurityConstants.SOCKET_RESOLVE_ACTION) {
 508             return RESOLVE;
 509         } else if (action == SecurityConstants.SOCKET_CONNECT_ACTION) {
 510             return CONNECT;
 511         } else if (action == SecurityConstants.SOCKET_LISTEN_ACTION) {
 512             return LISTEN;
 513         } else if (action == SecurityConstants.SOCKET_ACCEPT_ACTION) {
 514             return ACCEPT;
 515         } else if (action == SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION) {
 516             return CONNECT|ACCEPT;
 517         }
 518 
 519         char[] a = action.toCharArray();


< prev index next >