JAvaリング配列バッファ


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=buf.length-end+start){
			//System.out.println("    ");
			return false;//    
		}else if(start>end&&ar.length>=start-end){
			//System.out.println("    ");
			return false;//    	
		}else if(startend&&ar.lengthend){
			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;
	}

}