< prev index next >

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

Print this page
rev 49136 : [mq]: nio-state2

*** 146,161 **** this.remoteAddress = isa; this.state = ST_CONNECTED; } } ! // @throws ClosedChannelException if channel is closed private void ensureOpen() throws ClosedChannelException { if (!isOpen()) throw new ClosedChannelException(); } @Override public Socket socket() { synchronized (stateLock) { if (socket == null) socket = SocketAdaptor.create(this); --- 146,186 ---- this.remoteAddress = isa; this.state = ST_CONNECTED; } } ! /** ! * Checks that the channel is open. ! * ! * @throws ClosedChannelException if channel is closed (or closing) ! */ private void ensureOpen() throws ClosedChannelException { if (!isOpen()) throw new ClosedChannelException(); } + /** + * Checks that the channel is open and connected. + * + * @apiNote This method uses the "state" field to check if the channel is + * open. It should never be used in conjuncion with isOpen or ensureOpen + * as these methods check AbstractInterruptibleChannel's closed field - that + * field is set before implCloseSelectableChannel is called and so before + * the state is changed. + * + * @throws ClosedChannelException if channel is closed (or closing) + * @throws NotYetConnectedException if open and not connected + */ + private void ensureOpenAndConnected() throws ClosedChannelException { + int state = this.state; + if (state < ST_CONNECTED) { + throw new NotYetConnectedException(); + } else if (state > ST_CONNECTED) { + throw new ClosedChannelException(); + } + } + @Override public Socket socket() { synchronized (stateLock) { if (socket == null) socket = SocketAdaptor.create(this);
*** 273,290 **** */ private void beginRead(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); ! } synchronized (stateLock) { ! ensureOpen(); ! if (state != ST_CONNECTED) ! throw new NotYetConnectedException(); ! if (blocking) readerThread = NativeThread.current(); } } /** * Marks the end of a read operation that may have blocked. * --- 298,316 ---- */ private void beginRead(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); ! synchronized (stateLock) { ! ensureOpenAndConnected(); ! // record thread so it can be signalled if needed readerThread = NativeThread.current(); } + } else { + ensureOpenAndConnected(); + } } /** * Marks the end of a read operation that may have blocked. *
*** 383,402 **** */ private void beginWrite(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); ! } synchronized (stateLock) { ! ensureOpen(); if (isOutputClosed) throw new ClosedChannelException(); ! if (state != ST_CONNECTED) ! throw new NotYetConnectedException(); ! if (blocking) writerThread = NativeThread.current(); } } /** * Marks the end of a write operation that may have blocked. * --- 409,429 ---- */ private void beginWrite(boolean blocking) throws ClosedChannelException { if (blocking) { // set hook for Thread.interrupt begin(); ! synchronized (stateLock) { ! ensureOpenAndConnected(); if (isOutputClosed) throw new ClosedChannelException(); ! // record thread so it can be signalled if needed writerThread = NativeThread.current(); } + } else { + ensureOpenAndConnected(); + } } /** * Marks the end of a write operation that may have blocked. *
*** 610,623 **** if (localAddress == null) NetHooks.beforeTcpConnect(fd, isa.getAddress(), isa.getPort()); remoteAddress = isa; ! if (blocking) readerThread = NativeThread.current(); } } /** * Marks the end of a connect operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this --- 637,652 ---- if (localAddress == null) NetHooks.beforeTcpConnect(fd, isa.getAddress(), isa.getPort()); remoteAddress = isa; ! if (blocking) { ! // record thread so it can be signalled if needed readerThread = NativeThread.current(); } } + } /** * Marks the end of a connect operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this
*** 693,706 **** } synchronized (stateLock) { ensureOpen(); if (state != ST_CONNECTIONPENDING) throw new NoConnectionPendingException(); ! if (blocking) readerThread = NativeThread.current(); } } /** * Marks the end of a finishConnect operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this --- 722,737 ---- } synchronized (stateLock) { ensureOpen(); if (state != ST_CONNECTIONPENDING) throw new NoConnectionPendingException(); ! if (blocking) { ! // record thread so it can be signalled if needed readerThread = NativeThread.current(); } } + } /** * Marks the end of a finishConnect operation that may have blocked. * * @throws AsynchronousCloseException if the channel was closed due to this
< prev index next >