< prev index next >

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Print this page

        

@@ -57,105 +57,114 @@
 
 /**
  * An implementation of SocketChannels
  */
 
-class SocketChannelImpl
+public class SocketChannelImpl
     extends SocketChannel
     implements SelChImpl
 {
     // Used to make native read and write calls
-    private static NativeDispatcher nd;
+    protected static NativeDispatcher nd;
 
     // Our file descriptor object
-    private final FileDescriptor fd;
-    private final int fdVal;
+    protected FileDescriptor fd;
+    protected final int fdVal;
 
     // Lock held by current reading or connecting thread
-    private final ReentrantLock readLock = new ReentrantLock();
+    protected final ReentrantLock readLock = new ReentrantLock();
 
     // Lock held by current writing or connecting thread
-    private final ReentrantLock writeLock = new ReentrantLock();
+    protected final ReentrantLock writeLock = new ReentrantLock();
 
     // Lock held by any thread that modifies the state fields declared below
     // DO NOT invoke a blocking I/O operation while holding this lock!
-    private final Object stateLock = new Object();
+    protected final Object stateLock = new Object();
 
     // Input/Output closed
-    private volatile boolean isInputClosed;
-    private volatile boolean isOutputClosed;
+    protected volatile boolean isInputClosed;
+    protected volatile boolean isOutputClosed;
 
     // -- The following fields are protected by stateLock
 
     // set true when exclusive binding is on and SO_REUSEADDR is emulated
-    private boolean isReuseAddress;
+    protected boolean isReuseAddress;
 
     // State, increases monotonically
-    private static final int ST_UNCONNECTED = 0;
-    private static final int ST_CONNECTIONPENDING = 1;
-    private static final int ST_CONNECTED = 2;
-    private static final int ST_CLOSING = 3;
-    private static final int ST_KILLPENDING = 4;
-    private static final int ST_KILLED = 5;
-    private volatile int state;  // need stateLock to change
+    protected static final int ST_UNCONNECTED = 0;
+    protected static final int ST_CONNECTIONPENDING = 1;
+    protected static final int ST_CONNECTED = 2;
+    protected static final int ST_CLOSING = 3;
+    protected static final int ST_KILLPENDING = 4;
+    protected static final int ST_KILLED = 5;
+    protected volatile int state;  // need stateLock to change
 
     // IDs of native threads doing reads and writes, for signalling
-    private long readerThread;
-    private long writerThread;
+    protected long readerThread;
+    protected long writerThread;
 
     // Binding
-    private InetSocketAddress localAddress;
-    private InetSocketAddress remoteAddress;
+    protected InetSocketAddress localAddress;
+    protected InetSocketAddress remoteAddress;
 
     // Socket adaptor, created on demand
-    private Socket socket;
+    protected Socket socket;
 
     // -- End of fields protected by stateLock
 
 
     // Constructor for normal connecting sockets
     //
-    SocketChannelImpl(SelectorProvider sp) throws IOException {
+    protected SocketChannelImpl(SelectorProvider sp) throws IOException {
         super(sp);
-        this.fd = Net.socket(true);
+        this.fd = createFD();
         this.fdVal = IOUtil.fdVal(fd);
     }
 
-    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
+    protected SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound)
         throws IOException
     {
         super(sp);
         this.fd = fd;
         this.fdVal = IOUtil.fdVal(fd);
         if (bound) {
             synchronized (stateLock) {
-                this.localAddress = Net.localAddress(fd);
+                this.localAddress = createLocalAddress(fd);
             }
         }
     }
 
     // Constructor for sockets obtained from server sockets
     //
-    SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa)
+    protected SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa)
         throws IOException
     {
         super(sp);
         this.fd = fd;
         this.fdVal = IOUtil.fdVal(fd);
         synchronized (stateLock) {
-            this.localAddress = Net.localAddress(fd);
+            this.localAddress = createLocalAddress(fd);
             this.remoteAddress = isa;
             this.state = ST_CONNECTED;
         }
     }
 
+    protected FileDescriptor createFD() throws IOException {
+        return Net.socket(true);
+    }
+
+    protected InetSocketAddress createLocalAddress(FileDescriptor fd)
+        throws IOException {
+        return Net.localAddress(fd);
+    }
+
     /**
      * Checks that the channel is open.
      *
      * @throws ClosedChannelException if channel is closed (or closing)
      */
-    private void ensureOpen() throws ClosedChannelException {
+    protected void ensureOpen() throws ClosedChannelException {
         if (!isOpen())
             throw new ClosedChannelException();
     }
 
     /**

@@ -284,21 +293,21 @@
             return Collections.unmodifiableSet(set);
         }
     }
 
     @Override
-    public final Set<SocketOption<?>> supportedOptions() {
+    public Set<SocketOption<?>> supportedOptions() {
         return DefaultOptionsHolder.defaultOptions;
     }
 
     /**
      * Marks the beginning of a read operation that might block.
      *
      * @throws ClosedChannelException if the channel is closed
      * @throws NotYetConnectedException if the channel is not yet connected
      */
-    private void beginRead(boolean blocking) throws ClosedChannelException {
+    protected void beginRead(boolean blocking) throws ClosedChannelException {
         if (blocking) {
             // set hook for Thread.interrupt
             begin();
 
             synchronized (stateLock) {

@@ -315,11 +324,11 @@
      * Marks the end of a read operation that may have blocked.
      *
      * @throws AsynchronousCloseException if the channel was closed due to this
      * thread being interrupted on a blocking read operation.
      */
-    private void endRead(boolean blocking, boolean completed)
+    protected void endRead(boolean blocking, boolean completed)
         throws AsynchronousCloseException
     {
         if (blocking) {
             synchronized (stateLock) {
                 readerThread = 0;

@@ -405,11 +414,11 @@
      * Marks the beginning of a write operation that might block.
      *
      * @throws ClosedChannelException if the channel is closed or output shutdown
      * @throws NotYetConnectedException if the channel is not yet connected
      */
-    private void beginWrite(boolean blocking) throws ClosedChannelException {
+    protected void beginWrite(boolean blocking) throws ClosedChannelException {
         if (blocking) {
             // set hook for Thread.interrupt
             begin();
 
             synchronized (stateLock) {

@@ -428,11 +437,11 @@
      * Marks the end of a write operation that may have blocked.
      *
      * @throws AsynchronousCloseException if the channel was closed due to this
      * thread being interrupted on a blocking write operation.
      */
-    private void endWrite(boolean blocking, boolean completed)
+    protected void endWrite(boolean blocking, boolean completed)
         throws AsynchronousCloseException
     {
         if (blocking) {
             synchronized (stateLock) {
                 writerThread = 0;

@@ -505,11 +514,11 @@
     }
 
     /**
      * Writes a byte of out of band data.
      */
-    int sendOutOfBandData(byte b) throws IOException {
+    protected int sendOutOfBandData(byte b) throws IOException {
         writeLock.lock();
         try {
             boolean blocking = isBlocking();
             int n = 0;
             try {

@@ -551,20 +560,20 @@
     }
 
     /**
      * Returns the local address, or null if not bound
      */
-    InetSocketAddress localAddress() {
+    protected InetSocketAddress localAddress() {
         synchronized (stateLock) {
             return localAddress;
         }
     }
 
     /**
      * Returns the remote address, or null if not connected
      */
-    InetSocketAddress remoteAddress() {
+    protected InetSocketAddress remoteAddress() {
         synchronized (stateLock) {
             return remoteAddress;
         }
     }
 

@@ -616,11 +625,11 @@
      * @throws ClosedChannelException if the channel is closed
      * @throws AlreadyConnectedException if already connected
      * @throws ConnectionPendingException is a connection is pending
      * @throws IOException if the pre-connect hook fails
      */
-    private void beginConnect(boolean blocking, InetSocketAddress isa)
+    protected void beginConnect(boolean blocking, InetSocketAddress isa)
         throws IOException
     {
         if (blocking) {
             // set hook for Thread.interrupt
             begin();

@@ -651,11 +660,11 @@
      *
      * @throws AsynchronousCloseException if the channel was closed due to this
      * thread being interrupted on a blocking connect operation.
      * @throws IOException if completed and unable to obtain the local address
      */
-    private void endConnect(boolean blocking, boolean completed)
+    protected void endConnect(boolean blocking, boolean completed)
         throws IOException
     {
         endRead(blocking, completed);
 
         if (completed) {

@@ -713,11 +722,11 @@
      * Marks the beginning of a finishConnect operation that might block.
      *
      * @throws ClosedChannelException if the channel is closed
      * @throws NoConnectionPendingException if no connection is pending
      */
-    private void beginFinishConnect(boolean blocking) throws ClosedChannelException {
+    protected void beginFinishConnect(boolean blocking) throws ClosedChannelException {
         if (blocking) {
             // set hook for Thread.interrupt
             begin();
         }
         synchronized (stateLock) {

@@ -736,11 +745,11 @@
      *
      * @throws AsynchronousCloseException if the channel was closed due to this
      * thread being interrupted on a blocking connect operation.
      * @throws IOException if completed and unable to obtain the local address
      */
-    private void endFinishConnect(boolean blocking, boolean completed)
+    protected void endFinishConnect(boolean blocking, boolean completed)
         throws IOException
     {
         endRead(blocking, completed);
 
         if (completed) {

@@ -928,15 +937,15 @@
             }
             return this;
         }
     }
 
-    boolean isInputOpen() {
+    protected boolean isInputOpen() {
         return !isInputClosed;
     }
 
-    boolean isOutputOpen() {
+    protected boolean isOutputOpen() {
         return !isOutputClosed;
     }
 
     /**
      * Poll this channel's socket for reading up to the given timeout.
< prev index next >