@@ -532,31 +532,29 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR
532532 */
533533JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_writeBytes
534534 (JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
535- if ( buffer == NULL ){
535+ ssize_t ret = 0 ;
536+ jbyte *jBuffer = NULL ;
537+ if ( !buffer ){
536538 jclass exClz = env->FindClass (" java/lang/NullPointerException" );
537- if ( exClz != NULL ) env->ThrowNew (exClz, " buffer" );
538- return 0 ;
539+ if ( exClz ) env->ThrowNew (exClz, " buffer" );
540+ goto Finally ;
539541 }
540- jboolean ret = JNI_FALSE;
541- jbyte* jBuffer = env->GetByteArrayElements (buffer, JNI_FALSE);
542- if ( jBuffer == NULL ){
543- jclass exClz = env->FindClass (" java/lang/RuntimeException" );
544- if ( exClz != NULL ) env->ThrowNew (exClz, " jni->GetByteArrayElements() failed" );
545- return 0 ;
542+ jBuffer = env->GetByteArrayElements (buffer, NULL );
543+ if ( !jBuffer ){
544+ jclass exClz = env->ExceptionCheck () ? NULL : env->FindClass (" java/lang/RuntimeException" );
545+ if ( exClz ) env->ThrowNew (exClz, " jni->GetByteArrayElements() failed" );
546+ goto Finally;
546547 }
547- jint bufferSize = env->GetArrayLength (buffer);
548- jint result = write (portHandle, jBuffer, (size_t )bufferSize);
549- if ( result == -1 ){
550- int err = errno; /* bakup errno*/
548+ ret = write (portHandle, jBuffer, env->GetArrayLength (buffer));
549+ if ( ret == -1 ){
550+ int err = errno;
551551 jclass exClz = env->FindClass (" java/io/IOException" );
552- assert (exClz != NULL );
553- env->ThrowNew (exClz, strerror (err));
552+ if ( exClz ) env->ThrowNew (exClz, strerror (err));
554553 goto Finally;
555554 }
556- ret = (result == bufferSize) ? JNI_TRUE : JNI_FALSE;
557555Finally:
558- env->ReleaseByteArrayElements (buffer, jBuffer, 0 );
559- return ret;
556+ if ( jBuffer ) env->ReleaseByteArrayElements (buffer, jBuffer, 0 );
557+ return (jint) ret;
560558}
561559
562560/* *
@@ -677,7 +675,7 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
677675 lpBuffer = (jbyte*)malloc (byteCount*sizeof *lpBuffer);
678676 if ( !lpBuffer ){
679677 char emsg[32 ]; emsg[0 ] = ' \0 ' ;
680- snprintf (emsg, sizeof emsg, " malloc(%d ) failed" , byteCount*sizeof *lpBuffer);
678+ snprintf (emsg, sizeof emsg, " malloc(%ld ) failed" , byteCount*sizeof *lpBuffer);
681679 jclass exClz = env->FindClass (" java/lang/RuntimeException" );
682680 if ( exClz ) env->ThrowNew (exClz, emsg);
683681 returnArray = NULL ; goto Finally;
0 commit comments