kopia lustrzana https://github.com/felHR85/UsbSerial
added okio buffer for write thread
rodzic
04cfbdf76e
commit
538a0d2a38
|
@ -36,6 +36,7 @@ android {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.annimon:stream:1.2.1'
|
implementation 'com.annimon:stream:1.2.1'
|
||||||
|
implementation 'com.squareup.okio:okio:2.1.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
createJar.dependsOn(deleteJar, build)
|
createJar.dependsOn(deleteJar, build)
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
package com.felhr.usbserial;
|
package com.felhr.usbserial;
|
||||||
|
|
||||||
import java.nio.BufferOverflowException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import okio.Buffer;
|
||||||
|
|
||||||
public class SerialBuffer
|
public class SerialBuffer
|
||||||
{
|
{
|
||||||
public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||||
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
|
||||||
private ByteBuffer readBuffer;
|
private ByteBuffer readBuffer;
|
||||||
private SynchronizedBuffer writeBuffer;
|
private SynchronizedBuffer writeBuffer;
|
||||||
private byte[] readBuffer_compatible; // Read buffer for android < 4.2
|
private byte[] readBufferCompatible; // Read buffer for android < 4.2
|
||||||
private boolean debugging = false;
|
private boolean debugging = false;
|
||||||
|
|
||||||
public SerialBuffer(boolean version)
|
SerialBuffer(boolean version)
|
||||||
{
|
{
|
||||||
writeBuffer = new SynchronizedBuffer();
|
writeBuffer = new SynchronizedBuffer();
|
||||||
if(version)
|
if(version)
|
||||||
|
@ -22,7 +22,7 @@ public class SerialBuffer
|
||||||
|
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
readBuffer_compatible = new byte[DEFAULT_READ_BUFFER_SIZE];
|
readBufferCompatible = new byte[DEFAULT_READ_BUFFER_SIZE];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,21 +34,7 @@ public class SerialBuffer
|
||||||
debugging = value;
|
debugging = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putReadBuffer(ByteBuffer data)
|
ByteBuffer getReadBuffer()
|
||||||
{
|
|
||||||
synchronized(this)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
readBuffer.put(data);
|
|
||||||
}catch(BufferOverflowException e)
|
|
||||||
{
|
|
||||||
// TO-DO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ByteBuffer getReadBuffer()
|
|
||||||
{
|
{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
|
@ -57,7 +43,7 @@ public class SerialBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public byte[] getDataReceived()
|
byte[] getDataReceived()
|
||||||
{
|
{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
|
@ -70,7 +56,7 @@ public class SerialBuffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearReadBuffer()
|
void clearReadBuffer()
|
||||||
{
|
{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
|
@ -78,68 +64,50 @@ public class SerialBuffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getWriteBuffer()
|
byte[] getWriteBuffer()
|
||||||
{
|
{
|
||||||
return writeBuffer.get();
|
return writeBuffer.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putWriteBuffer(byte[]data)
|
void putWriteBuffer(byte[]data)
|
||||||
{
|
{
|
||||||
writeBuffer.put(data);
|
writeBuffer.put(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void resetWriteBuffer()
|
byte[] getBufferCompatible()
|
||||||
{
|
{
|
||||||
writeBuffer.reset();
|
return readBufferCompatible;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getBufferCompatible()
|
byte[] getDataReceivedCompatible(int numberBytes)
|
||||||
{
|
{
|
||||||
return readBuffer_compatible;
|
return Arrays.copyOfRange(readBufferCompatible, 0, numberBytes);
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getDataReceivedCompatible(int numberBytes)
|
|
||||||
{
|
|
||||||
byte[] tempBuff = Arrays.copyOfRange(readBuffer_compatible, 0, numberBytes);
|
|
||||||
return tempBuff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SynchronizedBuffer
|
private class SynchronizedBuffer
|
||||||
{
|
{
|
||||||
private byte[] buffer;
|
private Buffer buffer;
|
||||||
private int position;
|
|
||||||
|
|
||||||
public SynchronizedBuffer()
|
SynchronizedBuffer()
|
||||||
{
|
{
|
||||||
this.buffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
|
this.buffer = new Buffer();
|
||||||
position = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void put(byte[] src)
|
synchronized void put(byte[] src)
|
||||||
{
|
{
|
||||||
if(src == null || src.length == 0) return;
|
if(src == null || src.length == 0) return;
|
||||||
if(position == -1)
|
|
||||||
position = 0;
|
|
||||||
if(debugging)
|
if(debugging)
|
||||||
UsbSerialDebugger.printLogPut(src, true);
|
UsbSerialDebugger.printLogPut(src, true);
|
||||||
if(position + src.length > DEFAULT_WRITE_BUFFER_SIZE - 1) //Checking bounds. Source data does not fit in buffer
|
|
||||||
{
|
buffer.write(src);
|
||||||
if(position < DEFAULT_WRITE_BUFFER_SIZE)
|
|
||||||
System.arraycopy(src, 0, buffer, position, DEFAULT_WRITE_BUFFER_SIZE - position);
|
|
||||||
position = DEFAULT_WRITE_BUFFER_SIZE;
|
|
||||||
notify();
|
notify();
|
||||||
}else // Source data fits in buffer
|
|
||||||
{
|
|
||||||
System.arraycopy(src, 0, buffer, position, src.length);
|
|
||||||
position += src.length;
|
|
||||||
notify();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized byte[] get()
|
synchronized byte[] get()
|
||||||
{
|
{
|
||||||
if(position == -1)
|
if(buffer.size() > 0)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -149,17 +117,13 @@ public class SerialBuffer
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(position <= -1 ) return new byte[0];
|
|
||||||
byte[] dst = Arrays.copyOfRange(buffer, 0, position);
|
byte[] dst = buffer.readByteArray();
|
||||||
|
|
||||||
if(debugging)
|
if(debugging)
|
||||||
UsbSerialDebugger.printLogGet(dst, true);
|
UsbSerialDebugger.printLogGet(dst, true);
|
||||||
position = -1;
|
|
||||||
return dst;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void reset()
|
return dst;
|
||||||
{
|
|
||||||
position = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -544,7 +544,6 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
|
||||||
{
|
{
|
||||||
writeThread.stopWriteThread();
|
writeThread.stopWriteThread();
|
||||||
writeThread = null;
|
writeThread = null;
|
||||||
serialBuffer.resetWriteBuffer();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -208,7 +208,6 @@ public abstract class UsbSpiDevice implements UsbSpiInterface
|
||||||
{
|
{
|
||||||
writeThread.stopWriteThread();
|
writeThread.stopWriteThread();
|
||||||
writeThread = null;
|
writeThread = null;
|
||||||
serialBuffer.resetWriteBuffer();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue