java 环形数组缓冲区

该博客介绍了如何使用Java创建一个环形数组缓冲区,包括构造函数、数据存取方法,展示了如何处理缓冲区满和空的情况,以及在不同边界条件下的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Server;
//环形数组缓冲区 缓冲区所能存放的数据为缓冲区大小的个数 减去1 为实际能存储的大小
public class Area_Buffer {
	private byte[] buf=null;
	private int start;
	private int end;
	public Area_Buffer(int s){//构造函数定义缓冲区的大小
		buf = new byte[s];
		this.start=this.end=0;
	}
//	public static void main(String[] args){//测试
//		Area_Buffer buf = new Area_Buffer(11);
//		byte[] ar = "aabbccddee".getBytes();
//		if(buf.put(ar)==false){
//			System.out.println("空间不够");
//			return ;
//		}
//		
//		byte[] ar1 = new byte[4];
//		ar1 = buf.get(ar1.length);
//		if(ar1!=null){
//			System.out.println(new String(ar1));			
//		}else{
//			System.out.println("内容不够,无法提取!");
//		}
//		
//		byte[] ara = "ff".getBytes();
//		if(buf.put(ara)==false){
//			System.out.println("空间不够");
//			return ;
//		}
//		
//		
//		byte[] ar3 = new byte[8];
//		ar3 = buf.get(ar3.length);
//		if(ar3!=null){
//			System.out.println(new String(ar3));			
//		}else{
//			System.out.println("内容不够,无法提取!");
//		}
//		
//		
//		byte[] ar2 = "eerr".getBytes();
//		if(buf.put(ar2)==false){
//			System.out.println("空间不够");			
//		}
//		
//		
//		byte[] ar3a = new byte[4];
//		ar3a = buf.get(ar3a.length);
//		if(ar3a!=null){
//			System.out.println(new String(ar3a));			
//		}else{
//			System.out.println("内容不够,无法提取!");
//		}
//		
//		
//		byte[] araa = "aabbccddee".getBytes();
//		if(buf.put(araa)==false){
//			System.out.println("空间不够");
//			return ;
//		}
//		byte[] ar3aa = new byte[10];
//		ar3aa = buf.get(ar3aa.length);
//		if(ar3aa!=null){
//			System.out.println(new String(ar3aa));			
//		}else{
//			System.out.println("内容不够,无法提取!");
//		}
//		
//	}
	public boolean put(byte[] ar){
		if(end+1==start||(start==end&&start==0&&ar.length>=buf.length)){
			//System.out.println("空间不够");
			return false;//已满||空间不够
		}else if(start<end&&ar.length>=buf.length-end+start){
			//System.out.println("空间不够");
			return false;//空间不够
		}else if(start>end&&ar.length>=start-end){
			//System.out.println("空间不够");
			return false;//空间不够	
		}else if(start<end&&ar.length<buf.length-end+start){
			if(ar.length<buf.length-end){
				System.arraycopy(ar, 0, buf, end, ar.length);
				end = end+ar.length;
			}else{
				System.arraycopy(ar, 0, buf, end, buf.length-end);
				System.arraycopy(ar, ar.length-(buf.length-end), buf, 0, ar.length-(buf.length-end));				
				end = ar.length-(buf.length-end);
			}
		}else if(start>end&&ar.length<start-end){
			System.arraycopy(ar, 0, buf, end, ar.length);
			end = end+ar.length;
		}else if(start==end&&start==0&&ar.length<buf.length){
			start=0;
			System.arraycopy(ar, 0, buf, start, ar.length);
			end=ar.length;
		}
		return true;
	}
	public byte[] get(int len){
		byte[] arr = new byte[len];
		if(start<end){
			if(len<=end-start){
				System.arraycopy(buf, start, arr, 0, len);
				start=start+len;
				if(start==end){
					start=end=0;
				}
				return arr;
			}else{
				//System.out.println("内容不够,无法提取!");
				return null;
			}
		}else if(start>end){
			if(len<=buf.length-start){
				System.arraycopy(buf, start, arr, 0, len);
				start=start+len;
				if(start==end){
					start=end=0;
				}
				return arr;				
			}else if(len<=buf.length-start+end){
				System.arraycopy(buf, start, arr, 0, buf.length-start);
				System.arraycopy(buf, 0, arr, buf.length-start,len-(buf.length-start));
				start=len-(buf.length-start);
				if(start==end){
					start=end=0;
				}
				return arr;				
			}else{
				//System.out.println("内容不够,无法提取!");
				return null;
			}
		}
		//System.out.println("内容不够,无法提取!");
		return null;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值