1
0
mirror of https://github.com/mik3y/usb-serial-for-android.git synced 2026-08-14 09:52:58 +00:00

check read buffer size

This commit is contained in:
kai-morich
2020-09-06 09:11:35 +02:00
parent 6f4cd0313c
commit c53c3ed0ae
4 changed files with 130 additions and 20 deletions
@@ -134,6 +134,9 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
if(mConnection == null) {
throw new IOException("Connection closed");
}
if(dest.length <= 0) {
throw new IllegalArgumentException("read buffer to small");
}
final int nread;
if (timeout != 0) {
// bulkTransfer will cause data loss with short timeout + high baud rates + continuous transfer
@@ -137,6 +137,12 @@ public class FtdiSerialDriver implements UsbSerialDriver {
@Override
public int read(final byte[] dest, final int timeout) throws IOException {
if(dest.length <= READ_HEADER_LENGTH) {
throw new IllegalArgumentException("read buffer to small");
// could allocate larger buffer, including space for 2 header bytes, but this would
// result in buffers not being 64 byte aligned any more, causing data loss at continuous
// data transfer at high baud rates when buffers are fully filled.
}
int nread;
if (timeout != 0) {
long endTime = System.currentTimeMillis() + timeout;