Skip to content

Commit ba081e0

Browse files
committed
Apply PR feedback
- Drop legacy function `writeBytes1()` #195 (comment) - Add superfluous comment as requested #195 (comment) - Also change all the other `write..` related calls to `int` #195 (comment) (wip @ ff8ede7)
1 parent a7ec5da commit ba081e0

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

src/main/cpp/_nix_based/jssc.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@
6060
#include <jssc_SerialNativeInterface.h>
6161
#include "version.h"
6262

63-
#if defined(_MSC_VER) && _MSC_VER < 1800
64-
# define PRIsz "Iu"
65-
# define PRIssz "Id"
66-
#elif defined(__MINGW32__) && !defined(__MINGW64__)
67-
# define PRIsz "u"
68-
# define PRIssz "d"
69-
#else
70-
# define PRIsz "zu"
71-
# define PRIssz "zd"
72-
#endif
73-
7463
/*
7564
* Get native library version
7665
*/
@@ -681,7 +670,7 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
681670
lpBuffer = (jbyte*)malloc(byteCount*sizeof*lpBuffer);
682671
if( !lpBuffer ){
683672
char emsg[32]; emsg[0] = '\0';
684-
snprintf(emsg, sizeof emsg, "malloc(%" PRIsz ") failed", byteCount*sizeof*lpBuffer);
673+
snprintf(emsg, sizeof emsg, "malloc(%zu) failed", byteCount*sizeof*lpBuffer);
685674
jclass exClz = env->FindClass("java/lang/RuntimeException");
686675
if( exClz ) env->ThrowNew(exClz, emsg);
687676
returnArray = NULL; goto Finally;

src/main/cpp/windows/jssc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
#include <jssc_SerialNativeInterface.h>
3030
#include "version.h"
3131

32+
// For snprintf formatting
3233
#if defined(_MSC_VER) && _MSC_VER < 1800
3334
# define PRIsz "Iu"
3435
# define PRIssz "Id"
3536
#elif defined(__MINGW32__) && !defined(__MINGW64__)
3637
# define PRIsz "u"
3738
# define PRIssz "d"
3839
#else
39-
# define PRIsz "zu"
40-
# define PRIssz "zd"
40+
# error "PRIsz and PRIssz definitions not configured for this target"
4141
#endif
4242

4343
#define MAX_PORT_NAME_STR_LEN 32

src/main/java/jssc/SerialPort.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -398,25 +398,6 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
398398
return serialInterface.setDTR(portHandle, enabled);
399399
}
400400

401-
/**
402-
* Write byte array to port
403-
*
404-
* @param buffer <code>byte[]</code> array to write
405-
*
406-
* @return If the operation is successfully completed, the method returns true, otherwise false
407-
*
408-
* @throws SerialPortException if exception occurred
409-
*
410-
* @deprecated use {@link #writeBytes(byte[])}.
411-
*/
412-
@Deprecated
413-
public boolean writeBytes1(byte[] buffer) throws SerialPortException {
414-
/* Delegate to new method and translate result to what original method
415-
* did return. */
416-
int numWrittenBytes = writeBytes(buffer);
417-
return numWrittenBytes == buffer.length;
418-
}
419-
420401
/**
421402
* Write byte array to port.
422403
*
@@ -440,86 +421,105 @@ public int writeBytes(byte[] buffer) throws SerialPortException {
440421
*
441422
* @param singleByte single <code>byte</code> value to write
442423
*
443-
* @return If the operation is successfully completed, the method returns true, otherwise false
424+
* @return Number of bytes written.
444425
*
445426
* @throws SerialPortException if exception occurred
446427
*
447428
* @since 0.8
448429
*/
449-
public boolean writeByte(byte singleByte) throws SerialPortException {
430+
public int writeByte(byte singleByte) throws SerialPortException {
450431
checkPortOpened("writeByte()");
451-
return writeBytes(new byte[]{singleByte}) == 1;
432+
return writeBytes(new byte[]{singleByte});
452433
}
453434

454435
/**
455436
* Write String to port
456437
*
457438
* @param string <code>String</code> value to write
458439
*
459-
* @return If the operation is successfully completed, the method returns true, otherwise false
440+
* @return
441+
* Number of bytes written. WARN: Number of BYTES, NOT number of CHARS!
442+
* This may differ from <code>string.length()</code> if passed in string
443+
* contains chars outside the english alphabeth. If you need reliable
444+
* return lengths, consider calling
445+
* {@link java.lang.String#getBytes(java.nio.charset.Charset)}
446+
* yourself and then passing result to
447+
* {@link #writeBytes(byte[])}
448+
* instead. This gives you the chance to verify the expected return
449+
* value correctly.
460450
*
461451
* @throws SerialPortException if exception occurred
462452
*
463453
* @since 0.8
464454
*/
465-
public boolean writeString(String string) throws SerialPortException {
455+
public int writeString(String string) throws SerialPortException {
466456
checkPortOpened("writeString()");
467457
byte[] bytes = string.getBytes();
468-
return writeBytes(bytes) == bytes.length;
458+
return writeBytes(bytes);
469459
}
470460

471461
/**
472462
* Write String to port
473463
*
474464
* @param string <code>String</code> value to write
475465
* @param charsetName valid <code>Charset</code> name, e.g. <code>"UTF-8"</code>
476-
* @return If the operation is successfully completed, the method returns true, otherwise false
466+
*
467+
* @return
468+
* Number of bytes written. WARN: Number of BYTES, NOT number of CHARS!
469+
* This may differ from <code>string.length()</code> if passed in string
470+
* contains chars outside the english alphabeth. If you need reliable
471+
* return lengths, consider calling
472+
* {@link java.lang.String#getBytes(java.nio.charset.Charset)}
473+
* yourself and then passing result to
474+
* {@link #writeBytes(byte[])}
475+
* instead. This gives you the chance to verify the expected return
476+
* value correctly.
477477
*
478478
* @throws SerialPortException if exception occurred
479479
* @throws UnsupportedEncodingException if encoding exception occurred
480480
*
481481
* @since 2.8.0
482482
*/
483-
public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException {
483+
public int writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException {
484484
checkPortOpened("writeString()");
485485
byte[] bytes = string.getBytes(charsetName);
486-
return writeBytes(bytes) == bytes.length;
486+
return writeBytes(bytes);
487487
}
488488

489489
/**
490490
* Write int value (in range from 0 to 255 (0x00 - 0xFF)) to port
491491
*
492492
* @param singleInt single <code>int</code> value to write
493493
*
494-
* @return If the operation is successfully completed, the method returns true, otherwise false
494+
* @return Number of bytes written.
495495
*
496496
* @throws SerialPortException if exception occurred
497497
*
498498
* @since 0.8
499499
*/
500-
public boolean writeInt(int singleInt) throws SerialPortException {
500+
public int writeInt(int singleInt) throws SerialPortException {
501501
checkPortOpened("writeInt()");
502-
return writeBytes(new byte[]{(byte)singleInt}) == 1;
502+
return writeBytes(new byte[]{(byte)singleInt});
503503
}
504504

505505
/**
506506
* Write int array (in range from 0 to 255 (0x00 - 0xFF)) to port
507507
*
508508
* @param buffer <code>int[]</code> array to write
509509
*
510-
* @return If the operation is successfully completed, the method returns true, otherwise false
510+
* @return Number of bytes written.
511511
*
512512
* @throws SerialPortException if exception occurred
513513
*
514514
* @since 0.8
515515
*/
516-
public boolean writeIntArray(int[] buffer) throws SerialPortException {
516+
public int writeIntArray(int[] buffer) throws SerialPortException {
517517
checkPortOpened("writeIntArray()");
518518
byte[] byteArray = new byte[buffer.length];
519519
for(int i = 0; i < buffer.length; i++){
520520
byteArray[i] = (byte)buffer[i];
521521
}
522-
return writeBytes(byteArray) == byteArray.length;
522+
return writeBytes(byteArray);
523523
}
524524

525525
/**

0 commit comments

Comments
 (0)