java 环形字节缓冲区

package cn.anigod.gdwater.turingTool;

import cn.anigod.gdwater.ByteInfo;
import cn.anigod.gdwater.tools.ByteTools;
import cn.anigod.gdwater.tools.FileTools;

import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class RingBuffer {
    int capacity;

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

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


    volatile byte[] data;

    Lock lock = new ReentrantLock();

    public RingBuffer(int capacity) {

        this.capacity = capacity;

        this.data = new byte[capacity];

        this.readInd = 0;

        this.writeInd = 0;

    }

    public  int getCanWriteLen(){
        if(readInd == writeInd){
            return capacity;
        }

        if(writeInd > readInd){
            return capacity - (writeInd - readInd);
        }

        if(readInd > writeInd){
            return  readInd - writeInd;
        }

        return 0;
    }

    public int getCanReadLen(){
        if(writeInd > readInd){
            return writeInd - readInd;
        }
        if(readInd > writeInd){
            return capacity - (readInd - writeInd);
        }

        return 0;
    }

    public void clear(){

        readInd = 0;

        writeInd = 0;
    }

    public synchronized boolean put(byte[] bs){

        lock.lock();
        try {
            if (bs == null || bs.length == 0) {
                lock.unlock();
                return false;
            }

            int writeLen = getCanWriteLen();
            while (writeLen >= bs.length) {
                break;
            }


            if (writeInd == readInd) {

                if (writeInd == capacity) {
                    System.arraycopy(bs, 0, data, 0, bs.length);
                    writeInd = bs.length;
                    lock.unlock();
                    return true;
                }
                int outBorLen = bs.length + writeInd - capacity;
                if (outBorLen > 0) {
                    int inBorLen = capacity - writeInd;
                    System.arraycopy(bs, 0, data, writeInd, inBorLen);
                    System.arraycopy(bs, inBorLen, data, 0, outBorLen);
                    writeInd = outBorLen;
                    lock.unlock();
                    return true;
                } else {
                    System.arraycopy(bs, 0, data, writeInd, bs.length);
                    writeInd = writeInd + bs.length;
                    lock.unlock();
                    return true;
                }
            }
            // 写大于读
            if (writeInd > readInd) {
                int outBorLen = bs.length + writeInd - capacity;
                if (outBorLen > 0) {
                    int inBorLen = capacity - writeInd;
                    System.arraycopy(bs, 0, data, writeInd, inBorLen);
                    System.arraycopy(bs, inBorLen, data, 0, outBorLen);
                    writeInd = outBorLen;
                    lock.unlock();
                    return true;
                } else {
                    System.arraycopy(bs, 0, data, writeInd, bs.length);
                    writeInd = writeInd + bs.length;
                    lock.unlock();
                    return true;
                }
            }

            //读大于写
            if (readInd > writeInd) {
                System.arraycopy(bs, 0, data, writeInd, bs.length);
                writeInd = writeInd + bs.length;
                lock.unlock();
                return true;
            }

        }catch (Exception e){
            System.err.println("writeInd:"+writeInd+", readInd"+readInd+", bsLen:"+bs.length);
        }
            lock.unlock();
        return false;
    }

    public synchronized byte[] poll(int len){

        lock.lock();

        if (getCanReadLen() < len) {
            
            lock.unlock();

            return null;

        }

        byte[] temp = new byte[len];

        if(writeInd > readInd){
            if((writeInd - readInd) < len){
                return null;
            }
            System.arraycopy(data, readInd, temp, 0, len);
            readInd = readInd + len;

            lock.unlock();
            return temp;
        }
        if(readInd > writeInd){
            int outBorLen = len + readInd - capacity;
            if(outBorLen > 0){
                int inBorLen = capacity - readInd;
                System.arraycopy(data, readInd, temp, 0, inBorLen);
                System.arraycopy(data, 0, temp, inBorLen, outBorLen);
                readInd = outBorLen;


                lock.unlock();
                return temp;
            }
            outBorLen = len + readInd - capacity;
            if(outBorLen <= 0 ){
            int inBorLen = capacity - writeInd;
                System.arraycopy(data, readInd, temp, 0, len);
                readInd = readInd + len;


                lock.unlock();
                return temp;
            }
        }
        
        lock.unlock();
        return null;
    }

  

}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中使用数组实现环形缓冲区的完整源代码: ```java public class CircularBuffer { private int head = 0; // 缓冲区头部 private int tail = 0; // 缓冲区尾部 private int size = 0; // 缓冲区已使用大小 private int capacity; // 缓冲区容量 private int[] buffer; // 缓冲区数组 public CircularBuffer(int capacity) { this.capacity = capacity; this.buffer = new int[capacity]; } // 向缓冲区写入数据 public synchronized void write(int data) throws InterruptedException { while (isFull()) { wait(); } buffer[tail] = data; tail = (tail + 1) % capacity; size++; notifyAll(); } // 从缓冲区读取数据 public synchronized int read() throws InterruptedException { while (isEmpty()) { wait(); } int data = buffer[head]; head = (head + 1) % capacity; size--; notifyAll(); return data; } // 判断缓冲区是否已满 public boolean isFull() { return size == capacity; } // 判断缓冲区是否为空 public boolean isEmpty() { return size == 0; } } ``` 使用一个数组来保存缓冲区数据,head和tail分别表示缓冲区头部和尾部的索引,size表示缓冲区已使用的大小,capacity表示缓冲区容量。使用循环数组的方式来实现环形缓冲区,当tail到达数组末尾时,将其置为0,从而继续往数组头部写入数据。当head到达数组末尾时,将其置为0,从而继续从数组头部读取数据。使用synchronized关键字来保证线程安全,当缓冲区已满或为空时,调用wait()方法使线程等待,直到有空间写入或有数据可读取时,使用notifyAll()方法通知其他等待中的线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值