Skip to content

Commit f5296a2

Browse files
matthijskooijmancmaglie
authored andcommitted
Prevent bytes from lingering in the serial buffer
This fixes a problem with the Serial UTF-8 decoder. This decoding moves data from char[] buf, into a ByteBuffer inFromSerial, then decodes them into a CharBuffer outToMessage and converts to a char[] to pass on. When the buf read contained just over a full buffer worth of bytes and contained some multi-byte characters, a situation could arise where two decodes were needed to fill up outToMessage, leaving some data in inFromSerial. If in this case no data would be left in buf, decoding would stop until more data came in from serial. This commit fixes this problem by: - Changing the outer loop to continue running when buf is empty, but inFromSerial is not. - Changing the inner loop to run at least once (so it runs when buf is empty, but inFromSerial is no). - Breaking out of the outer loop when no characters were produced (this handles the case where only an incomplete UTF-8 character remains in inFromSerial, which would otherwise prevent the loop from terminating. - Removes a `if (outToMessage.hasRemaining()` check that is now necessarily true if the break was not done. This fixes arduino#9808.
1 parent 8bf8bdf commit f5296a2

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Diff for: arduino-core/src/processing/app/Serial.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,41 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
189189

190190
public void processSerialEvent(byte[] buf) {
191191
int next = 0;
192-
while(next < buf.length) {
193-
while(next < buf.length && outToMessage.hasRemaining()) {
192+
// This uses a CharsetDecoder to convert from bytes to UTF-8 in
193+
// a streaming fashion (i.e. where characters might be split
194+
// over multiple reads). This needs the data to be in a
195+
// ByteBuffer (inFromSerial, which we also use to store leftover
196+
// incomplete characters for the nexst run) and produces a
197+
// CharBuffer (outToMessage), which we then convert to char[] to
198+
// pass onwards.
199+
// Note that these buffers switch from input to output mode
200+
// using flip/compact/clear
201+
while (next < buf.length || inFromSerial.position() > 0) {
202+
do {
203+
// This might be 0 when all data was already read from buf
204+
// (but then there will be data in inFromSerial left to
205+
// decode).
194206
int copyNow = Math.min(buf.length - next, inFromSerial.remaining());
195207
inFromSerial.put(buf, next, copyNow);
196208
next += copyNow;
209+
197210
inFromSerial.flip();
198211
bytesToStrings.decode(inFromSerial, outToMessage, false);
199212
inFromSerial.compact();
200-
}
213+
214+
// When there are multi-byte characters, outToMessage might
215+
// still have room, so add more bytes if we have any.
216+
} while (next < buf.length && outToMessage.hasRemaining());
217+
218+
// If no output was produced, the input only contained
219+
// incomplete characters, so we're done processing
220+
if (outToMessage.position() == 0)
221+
break;
222+
201223
outToMessage.flip();
202-
if(outToMessage.hasRemaining()) {
203-
char[] chars = new char[outToMessage.remaining()];
204-
outToMessage.get(chars);
205-
message(chars, chars.length);
206-
}
224+
char[] chars = new char[outToMessage.remaining()];
225+
outToMessage.get(chars);
226+
message(chars, chars.length);
207227
outToMessage.clear();
208228
}
209229
}

0 commit comments

Comments
 (0)