环形队列 java。
2020-05-10 04:19
176 查看
package test;
/*
* 环形数据队列
* */
public class Fifobytebuff {
byte[] mBuff;
int head/*指向最后一个有效数据的下一个*/, tail/*指向第一个有效数据*/;
boolean istheSameSide;
public Fifobytebuff(int bufferSize) {
mBuff = new byte[bufferSize];
head = 0;
tail = 0;
istheSameSide = true;
}
boolean isEmpty() {
return istheSameSide && head == tail;
}
boolean isFull() {
return !istheSameSide && head == tail;
}
public int addData(byte data[], int pos, int len) {
int dataLen = len - pos;
if (dataLen > getFreeLength()) {
dataLen = getFreeLength();
}
/*超过最大*/
if (dataLen + head > mBuff.length) {
int tmppos = mBuff.length - head;
System.arraycopy(data, pos, mBuff, head,tmppos);
System.arraycopy(data, pos + tmppos, mBuff, 0, dataLen - tmppos);
head = dataLen - tmppos;
istheSameSide = false;
} else if(dataLen + head < mBuff.length){
System.arraycopy(data, pos, mBuff, head, dataLen);
head += data.length;
}else {
System.arraycopy(data, pos, mBuff, head, dataLen);
head = 0;
istheSameSide = false;
}
return data.length;
}
public int addData(byte data[], int len) {
return addData(data, 0, len);
}
public int addData(byte data[]) {
return addData(data, 0, data.length);
}
public int addData(byte data) {
if (isFull()) {
return -1;
}
mBuff[head++] = data;
if (head == mBuff.length) {
head = 0;
istheSameSide = false;
}
return 1;
}
public int getDataLen() {
if(istheSameSide){
return head - tail;
}else{
return head - tail + mBuff.length;
}
}
public int getFreeLength(){
return mBuff.length - getDataLen();
}
private int interGetData(byte data[], int datapos,int len,boolean isKeep) {
int tmpTail = tail;
boolean tmpSameSide = istheSameSide;
if (len > getDataLen()) {
len = getDataLen();
}
if (tail + len > mBuff.length) {
int tmppos = mBuff.length - tail;
System.arraycopy(mBuff, tail, data, datapos, tmppos);
System.arraycopy(mBuff, 0, data, datapos+tmppos, len - tmppos);
tail = len - tmppos;
istheSameSide = true;
} else if(tail + len < mBuff.length){
System.arraycopy(mBuff, tail, data, datapos, len);
tail += len;
}else {
System.arraycopy(mBuff, tail, data, datapos, len);
tail = 0;
istheSameSide = true;
}
if(isKeep){
tail = tmpTail;
istheSameSide = tmpSameSide;
}
return len;
}
public int getData(byte data[], int pos,int len) {
return interGetData(data,pos,len,false);
}
public int getData(byte data[],int len) {
return interGetData(data,0,len,false);
}
public byte[] getData(int len) {
if (len > getDataLen()) {
len = getDataLen();
}
byte tmp[] = new byte[len];
getData(tmp,0, tmp.length);
return tmp;
}
public void clear() {
}
public byte[] dump() {
byte[] data = new byte [mBuff.length];
interGetData(data,0,data.length,true);
return data;
}
public static void main(String[] args) {
Fifobytebuff fifoBuf = new Fifobytebuff(16);
for (int i = 0; i < 32; i++) {
fifoBuf.addData((byte) (i & 0xff));
}
byte []tt = fifoBuf.dump();
for (int i = 0; i < tt.length; i++) {
if (i % 16 == 0)
System.out.println("");
System.out.print(tt[i] + " ");
}
System.out.println("");
System.out.println(fifoBuf.isEmpty()?"fifobuf is empty":"fifobuf is not empty");
System.out.println(fifoBuf.isFull() ?"fifobuf is full":"fifobuf is not full");
System.out.println("data left"+fifoBuf.getDataLen());
byte []bb = fifoBuf.getData(fifoBuf.getDataLen());
System.out.println(fifoBuf.isEmpty()?"fifobuf is empty":"fifobuf is not empty");
System.out.println(fifoBuf.isFull() ?"fifobuf is full":"fifobuf is not full");
System.out.println("data left"+fifoBuf.getDataLen());
fifoBuf.addData((byte)0xff);
System.out.println("data left"+fifoBuf.getDataLen());
System.out.println(fifoBuf.isEmpty()?"fifobuf is empty":"fifobuf is not empty");
System.out.println(fifoBuf.isFull() ?"fifobuf is full":"fifobuf is not full");
}
}
[/code]
转载于:https://my.oschina.net/shangyu/blog/140948
chinong5099 原创文章 0获赞 0访问量 27 关注 私信相关文章推荐
- Java实现环形队列
- 用数组实现环形队列(java版)
- Java的循环队列(环形缓冲、RingBuffer)
- 环形队列 - java实现
- Java中利用数组模拟环形队列
- Java数据结构-003队列-数组模拟环形队列
- Java数据结构 环形队列
- java之环形队列的实现
- java 环形阻塞队列简单实现
- java的数据结构与算法(3)环形队列
- java数组实现队列及环形队列实现过程解析
- java实现数据结构中的环形队列
- Java学习——数据结构——数组实现环形队列
- Java数组模拟环形队列
- java 环形队列简单实现
- Java实现环形队列
- java 线程2-等待队列问题
- 队列 java
- 一步一步学习java数组学习(ch5)数3退1记录最后留下来的在队列中的位置
- 【JAVA】简单实现一个阻塞任务队列