环形队列 java。

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");
	}
}

转载于:https://my.oschina.net/shangyu/blog/140948

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值