@@ -1215,75 +1215,82 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
12151215 return -1 ;
12161216 }
12171217
1218- /* ByteBuffer.hasArray() does not throw any exceptions */
1219- hasArray = (* jenv )-> CallBooleanMethod (jenv , buf , hasArrayMeth );
1220- if (!hasArray ) {
1221- (* jenv )-> ThrowNew (jenv , excClass ,
1222- "ByteBuffer.hasArray() is false in native read()" );
1223- return BAD_FUNC_ARG ;
1224- }
1225-
12261218 /* Only read up to maximum space we have in this ByteBuffer */
12271219 maxOutputSz = (limit - position );
12281220 if (outSz > maxOutputSz ) {
12291221 outSz = maxOutputSz ;
12301222 }
12311223
1232- /* Get reference to underlying byte[] from ByteBuffer */
1233- arrayMeth = (* jenv )-> GetMethodID (jenv , buffClass , "array" , "()[B" );
1234- if (arrayMeth == NULL ) {
1224+ hasArray = (* jenv )-> CallBooleanMethod (jenv , buf , hasArrayMeth );
1225+
1226+ if (hasArray ) {
1227+ /* Get reference to underlying byte[] from ByteBuffer */
1228+ arrayMeth = (* jenv )-> GetMethodID (jenv , buffClass , "array" , "()[B" );
1229+ if (arrayMeth == NULL ) {
1230+ if ((* jenv )-> ExceptionOccurred (jenv )) {
1231+ (* jenv )-> ExceptionDescribe (jenv );
1232+ (* jenv )-> ExceptionClear (jenv );
1233+ }
1234+ (* jenv )-> ThrowNew (jenv , excClass ,
1235+ "Failed to find ByteBuffer array() method in native read()" );
1236+ return -1 ;
1237+ }
1238+ bufArr = (jbyteArray )(* jenv )-> CallObjectMethod (jenv , buf , arrayMeth );
1239+
1240+ /* Get array elements */
1241+ data = (byte * )(* jenv )-> GetByteArrayElements (jenv , bufArr , NULL );
12351242 if ((* jenv )-> ExceptionOccurred (jenv )) {
12361243 (* jenv )-> ExceptionDescribe (jenv );
12371244 (* jenv )-> ExceptionClear (jenv );
1245+ (* jenv )-> ThrowNew (jenv , excClass ,
1246+ "Exception when calling ByteBuffer.array() in native read()" );
1247+ return -1 ;
12381248 }
1239- (* jenv )-> ThrowNew (jenv , excClass ,
1240- "Failed to find ByteBuffer array() method in native read()" );
1241- return -1 ;
12421249 }
1243- bufArr = (jbyteArray )(* jenv )-> CallObjectMethod (jenv , buf , arrayMeth );
1244-
1245- /* Get array elements */
1246- data = (byte * )(* jenv )-> GetByteArrayElements (jenv , bufArr , NULL );
1247- if ((* jenv )-> ExceptionOccurred (jenv )) {
1248- (* jenv )-> ExceptionDescribe (jenv );
1249- (* jenv )-> ExceptionClear (jenv );
1250- (* jenv )-> ThrowNew (jenv , excClass ,
1251- "Exception when calling ByteBuffer.array() in native read()" );
1252- return -1 ;
1250+ else {
1251+ data = (byte * )(* jenv )-> GetDirectBufferAddress (jenv , buf );
1252+ if (data == NULL ) {
1253+ (* jenv )-> ThrowNew (jenv , excClass ,
1254+ "Failed to get DirectBuffer address in native read()" );
1255+ return BAD_FUNC_ARG ;
1256+ }
12531257 }
12541258
1255-
12561259 if (data != NULL ) {
12571260 size = SSLReadNonblockingWithSelectPoll (ssl , data + position ,
1258- maxOutputSz , (int )timeout );
1261+ maxOutputSz , (int )timeout );
12591262
12601263 /* Relase array elements */
1261- if (size < 0 ) {
1262- (* jenv )-> ReleaseByteArrayElements (jenv , bufArr , (jbyte * )data ,
1263- JNI_ABORT );
1264+ if (hasArray ) {
1265+ if (size < 0 ) {
1266+ (* jenv )-> ReleaseByteArrayElements (jenv , bufArr , (jbyte * )data ,
1267+ JNI_ABORT );
1268+ }
1269+ else {
1270+ (* jenv )-> ReleaseByteArrayElements (jenv , bufArr ,
1271+ (jbyte * )data , 0 );
1272+ }
12641273 }
1265- else {
1266- /* JNI_COMMIT commits the data but does not free the local array
1267- * 0 is used here to both commit and free */
1268- (* jenv )-> ReleaseByteArrayElements (jenv , bufArr ,
1269- (jbyte * )data , 0 );
12701274
1275+ /* Note: DirectByteBuffer doesn't need releasing data */
1276+
1277+ if (size > 0 ) {
12711278 /* Update ByteBuffer position() based on bytes written */
12721279 setPositionMeth = (* jenv )-> GetMethodID (jenv , buffClass ,
1273- "position" , "(I)Ljava/nio/Buffer;" );
1280+ "position" , "(I)Ljava/nio/Buffer;" );
12741281 if (setPositionMeth == NULL ) {
1275- if ((* jenv )-> ExceptionOccurred (jenv )) {
1276- (* jenv )-> ExceptionDescribe (jenv );
1277- (* jenv )-> ExceptionClear (jenv );
1278- }
1279- (* jenv )-> ThrowNew (jenv , excClass ,
1280- "Failed to set ByteBuffer position() from "
1281- "native read()" );
1282- size = -1 ;
1282+ if ((* jenv )-> ExceptionOccurred (jenv )) {
1283+ (* jenv )-> ExceptionDescribe (jenv );
1284+ (* jenv )-> ExceptionClear (jenv );
1285+ }
1286+ (* jenv )-> ThrowNew (jenv , excClass ,
1287+ "Failed to set ByteBuffer position() from "
1288+ "native read()" );
1289+ size = -1 ;
12831290 }
12841291 else {
1285- (* jenv )-> CallVoidMethod (jenv , buf , setPositionMeth ,
1286- position + size );
1292+ (* jenv )-> CallVoidMethod (jenv , buf , setPositionMeth ,
1293+ position + size );
12871294 }
12881295 }
12891296 }
0 commit comments