1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-11 01:46:15 +00:00

SerialInputOutputManager: fix write butter synchronization

This commit is contained in:
mike wakerly 2012-01-09 13:49:37 -08:00
parent bd50024ebc
commit 7a690f3ad2

View File

@ -45,6 +45,8 @@ public class SerialInputOutputManager implements Runnable {
private final UsbSerialDriver mDriver; private final UsbSerialDriver mDriver;
private final ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ); private final ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ);
// Synchronized by 'mWriteBuffer'
private final ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ); private final ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);
private enum State { private enum State {
@ -96,7 +98,9 @@ public class SerialInputOutputManager implements Runnable {
} }
public void writeAsync(byte[] data) { public void writeAsync(byte[] data) {
mWriteBuffer.put(data); synchronized (mWriteBuffer) {
mWriteBuffer.put(data);
}
} }
public synchronized void stop() { public synchronized void stop() {
@ -164,16 +168,19 @@ public class SerialInputOutputManager implements Runnable {
} }
// Handle outgoing data. // Handle outgoing data.
if (mWriteBuffer.position() > 0) { byte[] outBuff = null;
final byte[] outBuff; synchronized (mWriteBuffer) {
synchronized (mWriteBuffer) { if (mWriteBuffer.position() > 0) {
len = mWriteBuffer.position(); len = mWriteBuffer.position();
outBuff = new byte[len]; outBuff = new byte[len];
mWriteBuffer.get(outBuff, 0, len); mWriteBuffer.get(outBuff, 0, len);
mWriteBuffer.clear(); mWriteBuffer.clear();
} }
if (DEBUG) }
if (outBuff != null) {
if (DEBUG) {
Log.d(TAG, "Writing data len=" + len); Log.d(TAG, "Writing data len=" + len);
}
mDriver.write(outBuff, READ_WAIT_MILLIS); mDriver.write(outBuff, READ_WAIT_MILLIS);
} }
} }