26
26
import java .nio .ByteBuffer ;
27
27
import java .nio .channels .SelectionKey ;
28
28
import java .nio .channels .SocketChannel ;
29
+ import java .util .concurrent .atomic .AtomicBoolean ;
30
+
29
31
30
32
/**
31
33
*
@@ -70,6 +72,8 @@ public class SocketChannelFrameHandlerState {
70
72
71
73
final FrameBuilder frameBuilder ;
72
74
75
+ private final AtomicBoolean isUnderflowHandlingEnabled = new AtomicBoolean (false );
76
+
73
77
public SocketChannelFrameHandlerState (SocketChannel channel , NioLoopContext nioLoopsState , NioParams nioParams , SSLEngine sslEngine ) {
74
78
this .channel = channel ;
75
79
this .readSelectorState = nioLoopsState .readSelectorState ;
@@ -105,7 +109,7 @@ public SocketChannelFrameHandlerState(SocketChannel channel, NioLoopContext nioL
105
109
this .outputStream = new DataOutputStream (
106
110
new SslEngineByteBufferOutputStream (sslEngine , plainOut , cipherOut , channel )
107
111
);
108
- this .frameBuilder = new SslEngineFrameBuilder (sslEngine , plainIn , cipherIn , channel );
112
+ this .frameBuilder = new SslEngineFrameBuilder (sslEngine , plainIn , cipherIn , channel , isUnderflowHandlingEnabled );
109
113
}
110
114
111
115
}
@@ -176,11 +180,14 @@ void endWriteSequence() {
176
180
177
181
void prepareForReadSequence () throws IOException {
178
182
if (ssl ) {
179
- cipherIn .clear ();
180
- plainIn .clear ();
183
+ if (!isUnderflowHandlingEnabled .get ()) {
184
+ cipherIn .clear ();
185
+ cipherIn .flip ();
186
+ }
181
187
182
- cipherIn . flip ();
188
+ plainIn . clear ();
183
189
plainIn .flip ();
190
+
184
191
} else {
185
192
NioHelper .read (channel , plainIn );
186
193
plainIn .flip ();
@@ -189,6 +196,15 @@ void prepareForReadSequence() throws IOException {
189
196
190
197
boolean continueReading () throws IOException {
191
198
if (ssl ) {
199
+ if (isUnderflowHandlingEnabled .get ()) {
200
+ int bytesRead = NioHelper .read (channel , cipherIn );
201
+ if (bytesRead == 0 ) {
202
+ return false ;
203
+ } else {
204
+ cipherIn .flip ();
205
+ return true ;
206
+ }
207
+ }
192
208
if (!plainIn .hasRemaining () && !cipherIn .hasRemaining ()) {
193
209
// need to try to read something
194
210
cipherIn .clear ();
0 commit comments