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