初始化队列
ArrayList<FrameBean> datas = new ArrayList<FrameBean>(m_nMaxDataNum);
ArrayList<MyInt> indexEmpty = new ArrayList<MyInt>(m_nMaxDataNum);
ArrayList<MyInt> indexs = new ArrayList<MyInt>(0);
Lock lock = new ReentrantLock();
public YuvDataModel() {
Init();
}
void Init() {
for (int i = 0; i < m_nMaxDataNum; i++) {
indexEmpty.add(new MyInt(i));
datas.add(new FrameBean());
}
}
获取可用的空元素,以便从datas中获取数据
MyInt GetEmptyIndex() {
MyInt nIndex = null;
Lock();
// 鍙栧嚭鏈夋晥鐨刬d
if (indexEmpty.size() > 0) {
MyInt nTemp = indexEmpty.get(0);
indexEmpty.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid empty index!"));
}
{
nIndex = nTemp;
// break;
}
}
unlock();
return nIndex;
}
获取indexs 数值,将对应此数据下标的FrameBean元素提供出去
MyInt GetDataIndex() {
MyInt nIndex = null;
try {
Lock();
if (indexs.size() > 0) {
MyInt nTemp = indexs.get(0);
indexs.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid data index!"));
}
{
nIndex = nTemp;
//break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
return nIndex;
}
void AddDataIndex(MyInt nIndex) {
try {
Lock();
indexs.add(nIndex);
} catch (Exception e) {
} finally {
unlock();
}
}
void AddEmptyIndex(MyInt nIndex) {
try {
Lock();
indexEmpty.add(nIndex);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
}
获取空元素队列indexEmpty 中的数值将对应此数据的下标的数据取出来放入一个FrameBean对象中,此时indexEmpty需要移除一个元素,indexs中添加一个元素使用AddDataIndex()方法,接着通过pop方法将取到的FrameBean对象提供出去,
public void push()
{
/*
int size=data.length;
if (size < 1) {
return;
}
*/
MyInt nIndex = GetEmptyIndex();
if (nIndex == null) {
return;
}
if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);
if (dd.databyte == null ) {
dd.databyte = new byte[m_YuvLen];
}
System.arraycopy(m_YuvDirectBuf.array(), 0, dd.databyte, 0, m_YuvLen);
dd.width = m_width;
dd.height = m_height;
}
AddDataIndex(nIndex);
}
pop方法中indexEmpty 需要添加一个元素,indexs中移除一个元素
public int pop(FrameBean data) {
int ret = -1;
MyInt nIndex = GetDataIndex();
if (nIndex == null) {
return -1;
}
if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);
if (dd.databyte == null || dd.databyte.length < 1) {
} else {
if (data.databyte == null
|| data.databyte.length != dd.databyte.length) {
data.databyte = new byte[dd.databyte.length];
}
System.arraycopy(dd.databyte, 0, data.databyte, 0,
dd.databyte.length);
data.width = dd.width;
data.height = dd.height;
ret = dd.databyte.length;
}
}
AddEmptyIndex(nIndex);
return ret;
}