Working on the buffers

pull/4/head
Felipe Herranz 2014-03-21 18:36:00 +01:00
rodzic 5754c972cf
commit c2f044de5d
3 zmienionych plików z 37 dodań i 7 usunięć

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -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;