您的位置:首页 > 编程语言 > Java开发

环形队列 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 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: