src/java.base/share/classes/java/net/Socket.java
branchniosocketimpl-branch
changeset 57344 8b621b0d921c
parent 57341 733e9746d615
child 57347 16c087c9103e
equal deleted inserted replaced
57342:5b512573ccb8 57344:8b621b0d921c
    33 import java.nio.channels.SocketChannel;
    33 import java.nio.channels.SocketChannel;
    34 import java.security.AccessController;
    34 import java.security.AccessController;
    35 import java.security.PrivilegedAction;
    35 import java.security.PrivilegedAction;
    36 import java.util.Set;
    36 import java.util.Set;
    37 import java.util.Collections;
    37 import java.util.Collections;
    38 import java.util.concurrent.locks.ReentrantLock;
       
    39 
    38 
    40 /**
    39 /**
    41  * This class implements client sockets (also called just
    40  * This class implements client sockets (also called just
    42  * "sockets"). A socket is an endpoint for communication
    41  * "sockets"). A socket is an endpoint for communication
    43  * between two machines.
    42  * between two machines.
    61      */
    60      */
    62     private boolean created = false;
    61     private boolean created = false;
    63     private boolean bound = false;
    62     private boolean bound = false;
    64     private boolean connected = false;
    63     private boolean connected = false;
    65     private boolean closed = false;
    64     private boolean closed = false;
    66     private final ReentrantLock closeLock = new ReentrantLock();
    65     private Object closeLock = new Object();
    67     private boolean shutIn = false;
    66     private boolean shutIn = false;
    68     private boolean shutOut = false;
    67     private boolean shutOut = false;
    69 
    68 
    70     /**
    69     /**
    71      * The implementation of this Socket.
    70      * The implementation of this Socket.
  1573      * @exception  IOException  if an I/O error occurs when closing this socket.
  1572      * @exception  IOException  if an I/O error occurs when closing this socket.
  1574      * @revised 1.4
  1573      * @revised 1.4
  1575      * @spec JSR-51
  1574      * @spec JSR-51
  1576      * @see #isClosed
  1575      * @see #isClosed
  1577      */
  1576      */
  1578     public void close() throws IOException {
  1577     public synchronized void close() throws IOException {
  1579         closeLock.lock();
  1578         synchronized(closeLock) {
  1580         try {
  1579             if (isClosed())
  1581             if (closed)
       
  1582                 return;
  1580                 return;
  1583             closed = true;
       
  1584             if (created)
  1581             if (created)
  1585                 impl.close();
  1582                 impl.close();
  1586         } finally {
  1583             closed = true;
  1587             closeLock.unlock();
       
  1588         }
  1584         }
  1589     }
  1585     }
  1590 
  1586 
  1591     /**
  1587     /**
  1592      * Places the input stream for this socket at "end of stream".
  1588      * Places the input stream for this socket at "end of stream".
  1703      * @return true if the socket has been closed
  1699      * @return true if the socket has been closed
  1704      * @since 1.4
  1700      * @since 1.4
  1705      * @see #close
  1701      * @see #close
  1706      */
  1702      */
  1707     public boolean isClosed() {
  1703     public boolean isClosed() {
  1708         closeLock.lock();
  1704         synchronized(closeLock) {
  1709         try {
       
  1710             return closed;
  1705             return closed;
  1711         } finally {
       
  1712             closeLock.unlock();
       
  1713         }
  1706         }
  1714     }
  1707     }
  1715 
  1708 
  1716     /**
  1709     /**
  1717      * Returns whether the read-half of the socket connection is closed.
  1710      * Returns whether the read-half of the socket connection is closed.