java byte缓存_Java实现环形字节缓冲区,可以添加和弹出字节数组

package com.mildom.decodedemo;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

* author : tanliang

* date : 2020/12/17 16:00

* description :

*/

public class RingByteBuffer {

int capacity;

volatile  int readSequence;//最后读到的一位

volatile  int writeSequence;//最后写到的一位

volatile  int remainBytes;

volatile  int usedBytes;

volatile byte[]data;

Locklock =new ReentrantLock();

public RingByteBuffer(int capacity) {

this.capacity = capacity;

this.data =new byte[capacity];

this.readSequence = -1;

this.writeSequence = -1;

this.remainBytes = capacity;

this.usedBytes =0;

}

public synchronized boolean putBytes(byte[] bytes) {

lock.lock();

if(bytes ==null || bytes.length ==0 ||remainBytes < bytes.length) {

lock.unlock();

return false;

}

if(readSequence <=writeSequence) {

int rightRemain =capacity -writeSequence-1;

if (bytes.length <= rightRemain) {

System.arraycopy(bytes,0,data,writeSequence+1,bytes.length);

writeSequence =writeSequence + bytes.length;

}else {

System.arraycopy(bytes,0,data,writeSequence+1,rightRemain);

System.arraycopy(bytes,rightRemain-1+1,data,0,bytes.length -rightRemain);

writeSequence = bytes.length -rightRemain -1;

}

}else {

System.arraycopy(bytes,0,data,writeSequence+1,bytes.length);

writeSequence =writeSequence + bytes.length;

}

//加入成功则剩余的减少

remainBytes =remainBytes - bytes.length;

usedBytes =usedBytes + bytes.length;

lock.unlock();

return true;

}

public synchronized byte[]pollBytes(int length) {

lock.lock();

if(usedBytes < length) {

lock.unlock();

return null;

}

byte[] temp =new byte[length];

if(readSequence

System.arraycopy(data,readSequence+1,temp,0,length);

readSequence =readSequence + length;

}else {

int rightWrited =capacity -readSequence -1;

if (length <= rightWrited) {

System.arraycopy(data,readSequence+1,temp,0,length);

readSequence =readSequence + length;

}else {

System.arraycopy(data,readSequence+1,temp,0,rightWrited);

System.arraycopy(data,0,temp,rightWrited-1+1,length - rightWrited);

readSequence = length - rightWrited -1;

}

}

//弹出成功则减少长度

usedBytes =usedBytes - length;

remainBytes =remainBytes + length;

lock.unlock();

return temp;

}

public void clear(){

usedBytes =0;

remainBytes =capacity;

readSequence = -1;

writeSequence = -1;

}

public synchronized int getUsedBytes() {

return usedBytes;

}

public synchronized int getRemainBytes() {

return remainBytes;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值