kopia lustrzana https://github.com/felHR85/UsbSerial
Working on the buffers
rodzic
5754c972cf
commit
c2f044de5d
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
|
@ -1,20 +1,22 @@
|
|||
package com.felhr.usbserial;
|
||||
|
||||
import java.nio.BufferOverflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SerialBuffer
|
||||
{
|
||||
private static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
||||
private static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||
|
||||
private byte[] writeBuffer;
|
||||
private byte[] readBuffer;
|
||||
|
||||
private ByteBuffer writeBuffer;
|
||||
private ByteBuffer readBuffer;
|
||||
private Object mReadLock;
|
||||
private Object mWriteLock;
|
||||
|
||||
public SerialBuffer(Object mReadLock, Object mWriteLock)
|
||||
{
|
||||
writeBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
|
||||
readBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
|
||||
writeBuffer = ByteBuffer.allocate(DEFAULT_WRITE_BUFFER_SIZE);
|
||||
readBuffer = ByteBuffer.allocate(DEFAULT_READ_BUFFER_SIZE);
|
||||
this.mReadLock = mReadLock;
|
||||
this.mWriteLock = mWriteLock;
|
||||
}
|
||||
|
@ -23,11 +25,39 @@ public class SerialBuffer
|
|||
{
|
||||
synchronized(mWriteLock)
|
||||
{
|
||||
// TO-DO
|
||||
try
|
||||
{
|
||||
writeBuffer.put(ByteBuffer.wrap(data));
|
||||
}catch(BufferOverflowException e)
|
||||
{
|
||||
if(data.length > DEFAULT_WRITE_BUFFER_SIZE && writeBuffer.position() == 0)
|
||||
{
|
||||
writeBuffer.put(data, 0, DEFAULT_WRITE_BUFFER_SIZE);
|
||||
}else if(data.length > DEFAULT_WRITE_BUFFER_SIZE && writeBuffer.position() > 0)
|
||||
{
|
||||
writeBuffer.put(data, 0, DEFAULT_WRITE_BUFFER_SIZE -
|
||||
writeBuffer.position());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getWriteBuffer()
|
||||
public ByteBuffer getWriteBuffer()
|
||||
{
|
||||
synchronized(mWriteLock)
|
||||
{
|
||||
ByteBuffer buff = writeBuffer;
|
||||
writeBuffer.clear();
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
|
||||
public void putReadBuffer(ByteBuffer data)
|
||||
{
|
||||
// TO-DO
|
||||
}
|
||||
|
||||
public byte[] getReadBuffer()
|
||||
{
|
||||
// TO-DO
|
||||
return null;
|
||||
|
|
Ładowanie…
Reference in New Issue