可以使用ByteBuffer重写循环缓冲区

时间:2016-07-07 13:46:17

标签: java byte bytebuffer

byte[] buffer = new byte[100];
int index = 0;

public void recordByte(Byte b) {
   index = (index + 1) % 100;
   buffer[index] = b; 
}

public void printLastBytes() {
   for(int i = index; i < index + 100; i++) {
       System.out.print(buffer[i % 100]);
   }
}

可以使用ByteBuffer 重写

这段代码(循环缓冲区)重写
private static final int SIZE = 100;
private final byte[] result = new byte[SIZE];

ByteBuffer buffer = ByteBuffer.allocate(SIZE);


@Override
public void record(byte b) {
    buffer.put(b);

}

@Override
public String print() {

}

0 个答案:

没有答案