일련의 이미지를 저장하기 위해 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
질문 : 위의 코드를 수정하여 메모리가 아닌 디스크에서 작업하는 방법 ... – Tekmanoid
그리고 무엇을 시도 했습니까? – Nick