@@ -1215,75 +1215,82 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_read__JLjava_nio_ByteBuff
1215
1215
return -1 ;
1216
1216
}
1217
1217
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
-
1226
1218
/* Only read up to maximum space we have in this ByteBuffer */
1227
1219
maxOutputSz = (limit - position );
1228
1220
if (outSz > maxOutputSz ) {
1229
1221
outSz = maxOutputSz ;
1230
1222
}
1231
1223
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 );
1235
1242
if ((* jenv )-> ExceptionOccurred (jenv )) {
1236
1243
(* jenv )-> ExceptionDescribe (jenv );
1237
1244
(* jenv )-> ExceptionClear (jenv );
1245
+ (* jenv )-> ThrowNew (jenv , excClass ,
1246
+ "Exception when calling ByteBuffer.array() in native read()" );
1247
+ return -1 ;
1238
1248
}
1239
- (* jenv )-> ThrowNew (jenv , excClass ,
1240
- "Failed to find ByteBuffer array() method in native read()" );
1241
- return -1 ;
1242
1249
}
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
+ }
1253
1257
}
1254
1258
1255
-
1256
1259
if (data != NULL ) {
1257
1260
size = SSLReadNonblockingWithSelectPoll (ssl , data + position ,
1258
- maxOutputSz , (int )timeout );
1261
+ maxOutputSz , (int )timeout );
1259
1262
1260
1263
/* 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
+ }
1264
1273
}
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 );
1270
1274
1275
+ /* Note: DirectByteBuffer doesn't need releasing data */
1276
+
1277
+ if (size > 0 ) {
1271
1278
/* Update ByteBuffer position() based on bytes written */
1272
1279
setPositionMeth = (* jenv )-> GetMethodID (jenv , buffClass ,
1273
- "position" , "(I)Ljava/nio/Buffer;" );
1280
+ "position" , "(I)Ljava/nio/Buffer;" );
1274
1281
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 ;
1283
1290
}
1284
1291
else {
1285
- (* jenv )-> CallVoidMethod (jenv , buf , setPositionMeth ,
1286
- position + size );
1292
+ (* jenv )-> CallVoidMethod (jenv , buf , setPositionMeth ,
1293
+ position + size );
1287
1294
}
1288
1295
}
1289
1296
}
0 commit comments