B4J Question how to catch AsyncStreams error ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All and @Erel especially,

Question about the catching error from stream (here https://www.b4x.com/android/forum/threads/asyncstream-com-port-buffer.143231/), log is so:

23
Result:COM3: 11 226.47 235.77 203.85 235.75 202.79 230.94 244.50 204.64 243.45 222.89
Result:COM3: 11 106.17 111.57 113.02 117.86 109.46 120.81 113.15 109.89 124.25 105.63
23
java.lang.NegativeArraySizeException: -1
at jssc.SerialNativeInterface.readBytes(Native Method)
at jssc.SerialPort.readBytes(SerialPort.java:510)
at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:165)
at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:157)
at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:232)
at java.base/java.lang.Thread.run(Thread.java:834)
java.lang.RuntimeException: java.lang.NegativeArraySizeException: -1
at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:172)
at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:157)
at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:232)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NegativeArraySizeException: -1
at jssc.SerialNativeInterface.readBytes(Native Method)
at jssc.SerialPort.readBytes(SerialPort.java:510)
at anywheresoftware.b4j.serial.Serial$1.read(Serial.java:165)
... 3 more
Result:COM3: 11 209.65 247.73 214.29 210.02 237.06 211.67 207.77 209.42 235.60 232.64
45
Result:COM3: 11 52.66 61.35 55.88 60.34 53.40 53.40 52.26 59.46 57.78 55.31
Result:COM3: 11 212.12 231.24 218.44 206.78 218.92 211.70 240.13 205.57 214.68 239.04
61
Result:COM3: 11 75.58 73.87 74.71 71.43 68.17 81.88 76.05 75.80 72.12 71.42

Error does not stop the process, but it occured (when device is unplugged) ! I guess, in another thread.
AStream_Error occured only after whole buffer is empty.
And to work correctly - i need to catch it and stop the stream. But how to do it ?
 

peacemaker

Expert
Licensed User
Longtime User
handle the Error
This one is raised in B4J, but only all the system serial buffer is empty, 20-30 seconds processing is going on.
But is it possible to catch "java.lang.NegativeArraySizeException" error ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This one is raised in B4J, but only all the system serial buffer is empty, 20-30 seconds processing is going on.
But is it possible to catch "java.lang.NegativeArraySizeException" error ?
This error, which happens on a different thread, should be caught by AsyncStreams and lead to the Terminated or Error events being raised. Are you handling both events?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If internally in the lib to catch NegativeArraySizeException error and raise the _Error or _Terminated immediately - it will be correct, i think - as the port is immediately out.
But ... it will lead to incompatibility with old lib behaviour when this error did not crash the app. Maybe a new event to be added ?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
BTW, never seen that _Terminated is raised independently.
 
Upvote 0
Top