我需要一些帮助/提示,以将基于内存的循环缓冲区转换为基于磁盘的...通过对Java文件进行读/写操作来存储一系列图像。磁盘上的循环缓冲区实现
研究在线我发现我需要:
* 不需要固定大小的帧,但与包括实际尺寸,以及时间戳和标记的报头前缀的每个帧。然后你可以简单地记住第一个有效帧头部的偏移量和最后一个有效帧尾部的偏移量,你就可以实现一个FIFO循环缓冲区。 * 对标题结构使用8的包装并将缓冲区填充为8的倍数,以便您可以读取/写入文件而不会出现对齐错误。
下面是我现在使用的代码:
class CircularBuffer {
private ImageIcon buffer[];
private int lastIdx;
private int firstIdx;
private int size;
private boolean empty;
/** default size */
static final int DEFAULT_SIZE = 50;
public CircularBuffer(int size) {
this.size = size;
this.clear();
}
public CircularBuffer() {
this(CircularBuffer.DEFAULT_SIZE);
}
public synchronized void push(ImageIcon data) {
buffer[this.lastIdx] = data;
if(this.lastIdx == this.firstIdx && !this.empty) {
this.firstIdx++;
this.firstIdx %= this.size;
}
if (this.empty){
this.empty = false;
}
this.lastIdx++;
this.lastIdx %= this.size;
}
public synchronized int getLength() {
if (this.empty)
return 0;
int len = this.lastIdx - this.firstIdx;
if (len < 0)
len = this.size + len;
return len == 0 ? this.size-1 : len;
}
public synchronized ImageIcon pop() {
if (isEmpty()) {
throw new IndexOutOfBoundsException("Empty buffer");
}
ImageIcon res = buffer[this.firstIdx];
buffer[this.firstIdx] = null;
this.firstIdx++;
this.firstIdx %= this.size;
if (this.firstIdx == this.lastIdx)
this.empty = true;
return res;
}
public synchronized boolean isEmpty() {
return this.empty;
}
public void clear() {
this.firstIdx = 0;
this.lastIdx = 0;
this.empty = true;
this.buffer = new ImageIcon[size];
}
public int getSize() {
return this.size;
}
}
感谢您的帮助!
你的问题是什么? – Nick 2013-02-26 13:44:36
问题:如何修改上面的代码在磁盘而不是内存上执行操作... – Tekmanoid 2013-02-26 13:48:35
你试过了什么? – Nick 2013-02-26 13:50:29