< prev index next >

src/java.base/share/classes/java/nio/channels/UnixDomainSocketAddress.java

Print this page
M UnixDomainSocketAddress.java


  31 import java.nio.file.FileSystem;
  32 import java.nio.file.FileSystems;
  33 import java.nio.file.InvalidPathException;
  34 import java.nio.file.Path;
  35 
  36 /**
  37  * A <a href="package-summary.html#unixdomain">Unix domain</a> socket address.
  38  * A Unix domain socket address encapsulates a file path that Unix domain sockets
  39  * bind or connect to.
  40  *
  41  * <p> An <a id="unnamed"></a><i>unnamed</i> {@code UnixDomainSocketAddress} has
  42  * an empty path. The local address of a Unix domain socket that is automatically
  43  * bound will be unnamed.
  44  *
  45  * <p> {@link Path} objects used to create instances of this class must be obtained
  46  * from the {@linkplain FileSystems#getDefault system-default} file system.
  47  *
  48  * @since 16
  49  */
  50 public final class UnixDomainSocketAddress extends SocketAddress {

  51     static final long serialVersionUID = 92902496589351288L;
  52 
  53     private final Path path;
  54 
  55     static class SerializationProxy implements Serializable {
  56         static final long serialVersionUID = 9829020419651288L;








  57 
  58         private String pathname;




  59 
  60         SerializationProxy(UnixDomainSocketAddress addr) {
  61             this.pathname = addr.path.toString();
  62         }
  63 






  64         private Object readResolve() {
  65             return UnixDomainSocketAddress.of(pathname);
  66         }
  67     }
  68 









  69     private Object writeReplace() throws ObjectStreamException {
  70         return new SerializationProxy(this);























  71     }
  72 
  73     private UnixDomainSocketAddress(Path path) {
  74         FileSystem fs = path.getFileSystem();
  75         if (fs != FileSystems.getDefault()) {
  76             throw new IllegalArgumentException(); // fix message
  77         }
  78         if (fs.getClass().getModule() != Object.class.getModule()) {
  79             throw new IllegalArgumentException();  // fix message
  80         }
  81         this.path = path;
  82     }
  83 
  84     /**
  85      * Create a named UnixDomainSocketAddress from the given path string.
  86      *
  87      * @param  pathname
  88      *         The path string
  89      *
  90      * @return A UnixDomainSocketAddress




  31 import java.nio.file.FileSystem;
  32 import java.nio.file.FileSystems;
  33 import java.nio.file.InvalidPathException;
  34 import java.nio.file.Path;
  35 
  36 /**
  37  * A <a href="package-summary.html#unixdomain">Unix domain</a> socket address.
  38  * A Unix domain socket address encapsulates a file path that Unix domain sockets
  39  * bind or connect to.
  40  *
  41  * <p> An <a id="unnamed"></a><i>unnamed</i> {@code UnixDomainSocketAddress} has
  42  * an empty path. The local address of a Unix domain socket that is automatically
  43  * bound will be unnamed.
  44  *
  45  * <p> {@link Path} objects used to create instances of this class must be obtained
  46  * from the {@linkplain FileSystems#getDefault system-default} file system.
  47  *
  48  * @since 16
  49  */
  50 public final class UnixDomainSocketAddress extends SocketAddress {
  51     @java.io.Serial
  52     static final long serialVersionUID = 92902496589351288L;
  53 
  54     private final transient Path path;
  55 
  56     /**
  57      * A serial proxy for all {@link UnixDomainSocketAddress} instances.
  58      * It captures the file path name and reconstructs using the public static
  59      * {@link #of(String) factory}.
  60      *
  61      * @serial include
  62      */
  63     private static final class SerialProxy implements Serializable {
  64         @java.io.Serial
  65         static final long serialVersionUID = -7955684448513979814L;
  66 
  67         /**
  68          * The path name.
  69          * @serial
  70          */
  71         private final String pathname;
  72 
  73         SerialProxy(String pathname) {
  74             this.pathname = pathname;
  75         }
  76 
  77         /**
  78          * Creates a {@link UnixDomainSocketAddress} instance, by an invocation
  79          * of the {@link #of(String) factory} method passing the path name.
  80          * @return a UnixDomainSocketAddress
  81          */
  82         @java.io.Serial
  83         private Object readResolve() {
  84             return UnixDomainSocketAddress.of(pathname);
  85         }
  86     }
  87 
  88     /**
  89      * Returns a
  90      * <a href="{@docRoot}/serialized-form.html#java.nio.channels.UnixDomainSocketAddress.SerialProxy">
  91      * SerialProxy</a> containing the path name of this instance.
  92      *
  93      * @return a {@link SerialProxy}
  94      * representing the path name of this instance
  95      */
  96     @java.io.Serial
  97     private Object writeReplace() throws ObjectStreamException {
  98         return new SerialProxy(path.toString());
  99     }
 100 
 101     /**
 102      * Throws InvalidObjectException, always.
 103      * @param s the stream
 104      * @throws java.io.InvalidObjectException always
 105      */
 106     @java.io.Serial
 107     private void readObject(java.io.ObjectInputStream s)
 108         throws java.io.InvalidObjectException
 109     {
 110         throw new java.io.InvalidObjectException("Proxy required");
 111     }
 112 
 113     /**
 114      * Throws InvalidObjectException, always.
 115      * @throws java.io.InvalidObjectException always
 116      */
 117     @java.io.Serial
 118     private void readObjectNoData()
 119         throws java.io.InvalidObjectException
 120     {
 121         throw new java.io.InvalidObjectException("Proxy required");
 122     }
 123 
 124     private UnixDomainSocketAddress(Path path) {
 125         FileSystem fs = path.getFileSystem();
 126         if (fs != FileSystems.getDefault()) {
 127             throw new IllegalArgumentException(); // fix message
 128         }
 129         if (fs.getClass().getModule() != Object.class.getModule()) {
 130             throw new IllegalArgumentException();  // fix message
 131         }
 132         this.path = path;
 133     }
 134 
 135     /**
 136      * Create a named UnixDomainSocketAddress from the given path string.
 137      *
 138      * @param  pathname
 139      *         The path string
 140      *
 141      * @return A UnixDomainSocketAddress


< prev index next >