Skip to content

Commit a1da581

Browse files
authored
Allow zero-length reads (#193)
Treat zero-length read as legal case Fixes: #192
1 parent 6a384da commit a1da581

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/main/cpp/_nix_based/jssc.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,15 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
662662
jbyteArray returnArray = NULL;
663663
int byteRemains = byteCount;
664664

665-
if( byteCount <= 0 ){
665+
if( byteCount < 0 ){
666666
char emsg[64]; emsg[0] = '\0';
667-
snprintf(emsg, sizeof emsg, "byteCount %d. Expected range: 1..2147483647", byteCount);
667+
snprintf(emsg, sizeof emsg, "byteCount %d. Expected range: 0..2147483647", byteCount);
668668
jclass exClz = env->FindClass("java/lang/IllegalArgumentException");
669669
if( exClz ) env->ThrowNew(exClz, emsg);
670670
returnArray = NULL; goto Finally;
671+
}else if( byteCount == 0 ){
672+
returnArray = env->NewByteArray(0);
673+
goto Finally;
671674
}
672675

673676
lpBuffer = (jbyte*)malloc(byteCount*sizeof*lpBuffer);

src/main/cpp/windows/jssc.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,15 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
281281
jbyte *lpBuffer = NULL;
282282
OVERLAPPED *overlapped = NULL;
283283

284-
if( byteCount <= 0 ){
284+
if( byteCount < 0 ){
285285
char emsg[64]; emsg[0] = '\0';
286-
snprintf(emsg, sizeof emsg, "byteCount %d. Expected range: 1..2147483647", byteCount);
286+
snprintf(emsg, sizeof emsg, "byteCount %d. Expected range: 0..2147483647", byteCount);
287287
jclass exClz = env->FindClass("java/lang/IllegalArgumentException");
288288
if( exClz ) env->ThrowNew(exClz, emsg);
289289
returnArray = NULL; goto Finally;
290+
}else if( byteCount == 0 ){
291+
returnArray = env->NewByteArray(0);
292+
goto Finally;
290293
}
291294

292295
returnArray = env->NewByteArray(byteCount);

src/test/java/jssc/SerialNativeInterfaceTest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.hamcrest.CoreMatchers.is;
1010
import static org.hamcrest.CoreMatchers.not;
1111
import static org.hamcrest.CoreMatchers.nullValue;
12+
import static org.junit.Assert.assertNotNull;
1213
import static org.junit.Assert.assertThat;
1314
import static org.junit.Assert.assertTrue;
1415
import static org.junit.Assert.fail;
@@ -110,16 +111,25 @@ public void throwsIfCountNegative() throws Exception {
110111
}
111112
}
112113

114+
/**
115+
* I think this case should just throw an exception, as trying to read zero
116+
* bytes doesn't make much sense to me. But it seems we need to accept a
117+
* "read of zero bytes" as a legal case. So jssc will respond with an empty
118+
* array, exactly as caller did request.
119+
* See also "https://github.com/java-native/jssc/issues/192".
120+
*
121+
* Update: According to
122+
* https://github.com/java-native/jssc/issues/192#issuecomment-2960137775
123+
* there seems to exist some other issue related to events, which occasionly
124+
* provocates zero-length reads. So as long this other issue exists, jssc
125+
* probably should handle zero-length reads, as it seems to cause them
126+
* itself. */
113127
@Test
114-
public void throwsIfCountZero() throws Exception {
128+
public void returnsAnEmptyArrayIfCountIsZero() throws Exception {
115129
SerialNativeInterface testTarget = new SerialNativeInterface();
116-
byte[] ret;
117-
try{
118-
ret = testTarget.readBytes(0, 0);
119-
fail("Where's the exception?");
120-
}catch( IllegalArgumentException ex ){
121-
assertTrue(ex.getMessage().contains("0"));
122-
}
130+
byte[] ret = testTarget.readBytes(0, 0);
131+
assertNotNull(ret);
132+
assertTrue(ret.length == 0);
123133
}
124134

125135
@Test

0 commit comments

Comments
 (0)