src/java.base/share/classes/java/nio/channels/UnixDomainSocketAddress.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
child 58847 692de65ab293
equal deleted inserted replaced
58799:eb491334113f 58801:119ac9128c1b
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    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.channels;
       
    27 
       
    28 import java.net.SocketAddress;
       
    29 import java.nio.file.Path;
       
    30 import java.security.AccessController;
       
    31 import java.security.PrivilegedAction;
       
    32 import java.util.Objects;
       
    33 
       
    34 /**
       
    35  * An address for a Unix domain socket or server socket channel. These
       
    36  * addresses contain a String path name, which when bound to a channel,
       
    37  * have an associated file in the file-system with the same name.
       
    38  * <p>
       
    39  * If a channel is automatically bound to Unix domain address then its address
       
    40  * is unnamed, has an empty path field, and therefore has no associated
       
    41  * file in the file-system.
       
    42  * <p>
       
    43  * Note, not all channel types support Unix domain addresses.
       
    44  *
       
    45  * @since 14
       
    46  */
       
    47 public class UnixDomainSocketAddress extends SocketAddress {
       
    48 
       
    49     static final long serialVersionUID = 9829020419651288L;
       
    50 
       
    51     static {
       
    52         if (System.getSecurityManager() == null) {
       
    53             System.loadLibrary("nio");
       
    54         } else {
       
    55             AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
       
    56                 System.loadLibrary("nio");
       
    57                 return null;
       
    58             });
       
    59         }
       
    60         init();
       
    61     }
       
    62 
       
    63     private final String path;
       
    64 
       
    65     /**
       
    66      * Create a named UnixDomainSocketAddress for the given path string.
       
    67      *
       
    68      * @param path the pathname to the socket.
       
    69      *
       
    70      * @throws NullPointerException if path is null
       
    71      */
       
    72     public UnixDomainSocketAddress(String path) {
       
    73         Objects.requireNonNull(path);
       
    74         this.path = path;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Create a named UnixDomainSocketAddress for the given path.
       
    79      *
       
    80      * @param path the path to the socket.
       
    81      *
       
    82      * @throws NullPointerException if path is null
       
    83      */
       
    84     public UnixDomainSocketAddress(Path path) {
       
    85         Objects.requireNonNull(path);
       
    86         this.path = path.toString();
       
    87     }
       
    88 
       
    89     /**
       
    90      * Return this address's path.
       
    91      *
       
    92      * @return this address's path
       
    93      */
       
    94     public String getPath() {
       
    95         return path;
       
    96     }
       
    97 
       
    98     static native void init();
       
    99 
       
   100     /**
       
   101      * Returns a hashcode computed from this object's path string.
       
   102      */
       
   103     @Override
       
   104     public int hashCode() {
       
   105         return path.hashCode();
       
   106     }
       
   107 
       
   108     /**
       
   109      * Compares this address with another object.
       
   110      *
       
   111      * @return true if the path fields are equal
       
   112      */
       
   113     @Override
       
   114     public boolean equals(Object o) {
       
   115         if (! (o instanceof UnixDomainSocketAddress))
       
   116             return false;
       
   117         UnixDomainSocketAddress that = (UnixDomainSocketAddress)o;
       
   118         return this.path.equals(that.path);
       
   119     }
       
   120 
       
   121     @Override
       
   122     public String toString() {
       
   123         StringBuilder sb = new StringBuilder();
       
   124         sb.append("af_unix:");
       
   125         if (path != null)
       
   126             sb.append(path);
       
   127         return sb.toString();
       
   128     }
       
   129 }