3333import org .neo4j .driver .v1 .exceptions .ClientException ;
3434import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
3535
36+ import static java .lang .String .format ;
3637import static javax .net .ssl .SSLEngineResult .HandshakeStatus .FINISHED ;
3738import static javax .net .ssl .SSLEngineResult .HandshakeStatus .NOT_HANDSHAKING ;
3839
@@ -80,8 +81,7 @@ public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEn
8081 }
8182 catch ( SSLHandshakeException e )
8283 {
83- throw new ClientException ( "Failed to establish secured connection with the server: " + e .getMessage (),
84- e .getCause () );
84+ throw new ClientException ( "Failed to establish secured connection with the server: " + e .getMessage (), e );
8585 }
8686 return tlsChannel ;
8787 }
@@ -153,12 +153,10 @@ private HandshakeStatus runDelegatedTasks()
153153 * @param toBuffer the destination where the data read from the socket channel are saved
154154 * @throws IOException when failed to read from channel
155155 */
156- void channelRead ( ByteBuffer toBuffer ) throws IOException
156+ int channelRead ( ByteBuffer toBuffer ) throws IOException
157157 {
158- /**
159- * This is the only place to read from the underlying channel
160- */
161- if ( channel .read ( toBuffer ) < 0 )
158+ int read = channel .read ( toBuffer );
159+ if ( read < 0 )
162160 {
163161 try
164162 {
@@ -172,16 +170,18 @@ void channelRead( ByteBuffer toBuffer ) throws IOException
172170 "SSL Connection terminated while receiving data. " +
173171 "This can happen due to network instabilities, or due to restarts of the database." );
174172 }
173+ return read ;
175174 }
176175
177176 /**
178177 * Write the data saved in the buffer to the socket channel
179178 * @param fromBuffer the source where the data written to the socket channel are saved
180179 * @throws IOException when failed to write to channel
181180 */
182- void channelWrite ( ByteBuffer fromBuffer ) throws IOException
181+ int channelWrite ( ByteBuffer fromBuffer ) throws IOException
183182 {
184- if ( channel .write ( fromBuffer ) < 0 )
183+ int written = channel .write ( fromBuffer );
184+ if ( written < 0 )
185185 {
186186 try
187187 {
@@ -195,6 +195,7 @@ void channelWrite( ByteBuffer fromBuffer ) throws IOException
195195 "SSL Connection terminated while writing data. " +
196196 "This can happen due to network instabilities, or due to restarts of the database." );
197197 }
198+ return written ;
198199 }
199200
200201 /**
@@ -259,7 +260,7 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
259260 if ( newAppSize > appSize * 2 )
260261 {
261262 throw new ClientException (
262- String . format ( "Failed ro enlarge application input buffer from %s to %s, as the maximum " +
263+ format ( "Failed ro enlarge application input buffer from %s to %s, as the maximum " +
263264 "buffer size allowed is %s. The content in the buffer is: %s\n " ,
264265 curAppSize , newAppSize , appSize * 2 , BytePrinter .hex ( plainIn ) ) );
265266 }
@@ -311,7 +312,7 @@ private HandshakeStatus unwrap( ByteBuffer buffer ) throws IOException
311312 * @return The status of the current handshake
312313 * @throws IOException
313314 */
314- private HandshakeStatus wrap ( ByteBuffer buffer ) throws IOException
315+ private HandshakeStatus wrap ( ByteBuffer buffer ) throws IOException , ClientException
315316 {
316317 HandshakeStatus handshakeStatus = sslEngine .getHandshakeStatus ();
317318 Status status = sslEngine .wrap ( buffer , cipherOut ).getStatus ();
@@ -345,7 +346,14 @@ private HandshakeStatus wrap( ByteBuffer buffer ) throws IOException
345346 {
346347 // flush as much data as possible
347348 cipherOut .flip ();
348- channelWrite ( cipherOut );
349+ if ( channelWrite ( cipherOut ) == 0 )
350+ {
351+ throw new ClientException ( format (
352+ "Failed to enlarge network buffer from %s to %s. This is either because the " +
353+ "new size is however less than the old size, or because the application " +
354+ "buffer size %s is so big that the application data still cannot fit into the " +
355+ "new network buffer." , curNetSize , netSize , buffer .capacity () ) );
356+ }
349357 cipherOut .compact ();
350358 logger .debug ( "Network output buffer couldn't be enlarged, flushing data to the channel instead." );
351359 }
0 commit comments