Skip to content

Commit 8f276ce

Browse files
committed
improve some code implementation
1 parent a15251e commit 8f276ce

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

lib/java/src/main/java/org/apache/thrift/transport/TNonblockingSSLSocket.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,31 @@ protected TNonblockingSSLSocket(String host, int port, int timeout, SSLContext s
6868
isHandshakeCompleted = false;
6969
}
7070

71-
/**
72-
* Register the new SocketChannel with our Selector, indicating we'd like to be notified when it's
73-
* ready for I/O.
74-
*
75-
* @param selector
76-
* @return the selection key for this socket.
77-
*/
71+
/** {@inheritDoc} */
72+
@Override
7873
public SelectionKey registerSelector(Selector selector, int interests) throws IOException {
7974
selectionKey = super.registerSelector(selector, interests);
8075
return selectionKey;
8176
}
8277

83-
/** Checks whether the socket is connected. */
78+
/** {@inheritDoc} */
79+
@Override
8480
public boolean isOpen() {
8581
// isConnected() does not return false after close(), but isOpen() does
8682
return super.isOpen() && isHandshakeCompleted;
8783
}
8884

89-
/** Do not call, the implementation provides its own lazy non-blocking connect. */
85+
/** {@inheritDoc} */
86+
@Override
9087
public void open() throws TTransportException {
9188
throw new RuntimeException("open() is not implemented for TNonblockingSSLSocket");
9289
}
9390

94-
/** Perform a nonblocking read into buffer. */
95-
public int read(ByteBuffer buffer) throws TTransportException {
91+
/** {@inheritDoc} */
92+
@Override
93+
public synchronized int read(ByteBuffer buffer) throws TTransportException {
9694
int numBytes = buffer.limit();
9795
while (decodedBytes.remaining() < numBytes) {
98-
HandshakeStatus hs = sslEngine_.getHandshakeStatus();
99-
if (hs == HandshakeStatus.FINISHED)
100-
throw new TTransportException(
101-
TTransportException.UNKNOWN, "Read operation is terminated. Handshake is completed");
10296
try {
10397
if (doUnwrap() == -1) {
10498
throw new IOException("Unable to read " + numBytes + " bytes");
@@ -133,8 +127,9 @@ public int read(ByteBuffer buffer) throws TTransportException {
133127
return numBytes;
134128
}
135129

136-
/** Perform a nonblocking write of the data in buffer; */
137-
public int write(ByteBuffer buffer) throws TTransportException {
130+
/** {@inheritDoc} */
131+
@Override
132+
public synchronized int write(ByteBuffer buffer) throws TTransportException {
138133
int numBytes = 0;
139134

140135
if (buffer.position() > 0) buffer.flip();
@@ -162,13 +157,15 @@ public int write(ByteBuffer buffer) throws TTransportException {
162157
return numBytes;
163158
}
164159

165-
/** Closes the socket. */
160+
/** {@inheritDoc} */
161+
@Override
166162
public void close() {
167163
sslEngine_.closeOutbound();
168164
super.close();
169165
}
170166

171167
/** {@inheritDoc} */
168+
@Override
172169
public boolean startConnect() throws IOException {
173170
if (this.isOpen()) {
174171
return true;
@@ -178,6 +175,7 @@ public boolean startConnect() throws IOException {
178175
}
179176

180177
/** {@inheritDoc} */
178+
@Override
181179
public boolean finishConnect() throws IOException {
182180
return super.finishConnect() && doHandShake();
183181
}
@@ -222,15 +220,7 @@ private synchronized boolean doTask() {
222220
runnable.run();
223221
}
224222
HandshakeStatus hs = sslEngine_.getHandshakeStatus();
225-
if (hs == HandshakeStatus.NEED_TASK) {
226-
try {
227-
throw new TTransportException(
228-
TTransportException.UNKNOWN, "handshake shouldn't need additional tasks");
229-
} catch (TTransportException e) {
230-
return false;
231-
}
232-
}
233-
return true;
223+
return hs != HandshakeStatus.NEED_TASK;
234224
}
235225

236226
private synchronized int doUnwrap() throws IOException {

0 commit comments

Comments
 (0)