You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
6.5 KiB
167 lines
6.5 KiB
15 years ago
|
// CachedRandomAccessFileWriter.java
|
||
|
// ---------------------------------
|
||
16 years ago
|
// part of The Kelondro Database
|
||
|
// (C) by Michael Peter Christen; mc@yacy.net
|
||
|
// first published on http://yacy.net
|
||
|
// Frankfurt, Germany, 2004-2008
|
||
|
// last major change: 09.12.2008
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
15 years ago
|
package net.yacy.kelondro.io;
|
||
16 years ago
|
|
||
|
import java.io.File;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.IOException;
|
||
|
import java.io.RandomAccessFile;
|
||
|
|
||
16 years ago
|
|
||
15 years ago
|
|
||
|
public final class CachedFileWriter extends AbstractWriter implements Writer {
|
||
16 years ago
|
|
||
|
private RandomAccessFile RAFile;
|
||
|
private byte[] cache;
|
||
|
private long cachestart;
|
||
|
private int cachelen;
|
||
|
|
||
15 years ago
|
public CachedFileWriter(final File file) throws IOException, FileNotFoundException {
|
||
16 years ago
|
this.name = file.getName();
|
||
|
this.file = file;
|
||
15 years ago
|
this.RAFile = new RandomAccessFile(this.file, "rw");
|
||
|
this.cache = new byte[8192];
|
||
|
this.cachestart = 0;
|
||
|
this.cachelen = 0;
|
||
16 years ago
|
}
|
||
|
|
||
|
public synchronized long length() throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
return this.RAFile.length();
|
||
|
}
|
||
|
|
||
|
public synchronized void setLength(long length) throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
cachelen = 0;
|
||
|
RAFile.setLength(length);
|
||
|
}
|
||
|
|
||
|
public synchronized long available() throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
return this.length() - RAFile.getFilePointer();
|
||
|
}
|
||
|
|
||
|
public synchronized final void readFully(final byte[] b, final int off, int len) throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
long seek = RAFile.getFilePointer();
|
||
|
if (cache != null && cachestart <= seek && cachelen - seek + cachestart >= len) {
|
||
|
// read from cache
|
||
|
//System.out.println("*** DEBUG FileRA " + this.file.getName() + ": CACHE HIT at " + seek);
|
||
|
System.arraycopy(cache, (int) (seek - cachestart), b, off, len);
|
||
|
RAFile.seek(seek + len);
|
||
|
return;
|
||
|
}
|
||
|
if (cache == null || cache.length < len) {
|
||
|
// cannot fill cache here
|
||
|
RAFile.readFully(b, off, len);
|
||
|
return;
|
||
|
}
|
||
|
// we fill the cache here
|
||
16 years ago
|
long available = this.RAFile.length() - seek;
|
||
|
if (available < (long) len) throw new IOException("EOF, available = " + available + ", requested = " + len);
|
||
16 years ago
|
if (cachestart + cachelen == seek && cache.length - cachelen >= len) {
|
||
|
RAFile.readFully(cache, cachelen, len);
|
||
|
//System.out.println("*** DEBUG FileRA " + this.file.getName() + ": append fill " + len + " bytes");
|
||
|
System.arraycopy(cache, cachelen, b, off, len);
|
||
|
cachelen += len;
|
||
|
} else {
|
||
|
// fill the cache as much as possible
|
||
16 years ago
|
int m = (int) Math.min(available, (long) cache.length);
|
||
16 years ago
|
RAFile.readFully(cache, 0, m);
|
||
|
cachestart = seek;
|
||
|
cachelen = m;
|
||
|
if (m != len) RAFile.seek(seek + len);
|
||
|
//System.out.println("*** DEBUG FileRA " + this.file.getName() + ": replace fill " + len + " bytes");
|
||
|
System.arraycopy(cache, 0, b, off, len);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
//assert len > 0;
|
||
|
// write to file
|
||
16 years ago
|
if (this.cache.length > 512) {
|
||
|
// the large cache is only useful during an initialization phase
|
||
|
byte[] newcache = new byte[512];
|
||
|
System.arraycopy(this.cache, 0, newcache, 0, newcache.length);
|
||
|
this.cache = newcache;
|
||
|
if (this.cachelen > this.cache.length) this.cachelen = this.cache.length;
|
||
|
}
|
||
16 years ago
|
long seekpos = this.RAFile.getFilePointer();
|
||
|
if (this.cachelen + len <= this.cache.length && this.cachestart + this.cachelen == seekpos) {
|
||
|
// append to cache
|
||
|
System.arraycopy(b, off, this.cache, this.cachelen, len);
|
||
|
//System.out.println("*** DEBUG FileRA " + this.file.getName() + ": write append " + len + " bytes");
|
||
|
this.cachelen += len;
|
||
|
} else if (len <= this.cache.length) {
|
||
|
// copy to cache
|
||
|
System.arraycopy(b, off, this.cache, 0, len);
|
||
|
//System.out.println("*** DEBUG FileRA " + this.file.getName() + ": write copy " + len + " bytes");
|
||
|
this.cachelen = len;
|
||
|
this.cachestart = seekpos;
|
||
|
} else {
|
||
|
// delete cache
|
||
|
this.cachelen = 0;
|
||
|
}
|
||
|
RAFile.write(b, off, len);
|
||
|
}
|
||
|
|
||
|
public synchronized void seek(final long pos) throws IOException {
|
||
15 years ago
|
checkReopen();
|
||
16 years ago
|
RAFile.seek(pos);
|
||
|
}
|
||
|
|
||
16 years ago
|
public synchronized void close() {
|
||
|
if (RAFile != null) try {
|
||
16 years ago
|
try{RAFile.getChannel().close();} catch (IOException e) {}
|
||
16 years ago
|
//System.out.println("***DEBUG*** closed file " + this.file + ", FD is " + ((RAFile.getFD().valid()) ? "VALID" : "VOID") + ", channel is " + ((RAFile.getChannel().isOpen()) ? "OPEN" : "CLOSE"));
|
||
16 years ago
|
RAFile.close();
|
||
16 years ago
|
//System.out.println("***DEBUG*** closed file " + this.file + ", FD is " + ((RAFile.getFD().valid()) ? "VALID" : "VOID") + ", channel is " + ((RAFile.getChannel().isOpen()) ? "OPEN" : "CLOSE"));
|
||
16 years ago
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
16 years ago
|
this.cache = null;
|
||
|
this.RAFile = null;
|
||
|
}
|
||
15 years ago
|
|
||
|
private void checkReopen() {
|
||
|
if (this.RAFile != null) return;
|
||
|
// re-open the file
|
||
|
try {
|
||
|
this.RAFile = new RandomAccessFile(this.file, "rw");
|
||
|
} catch (FileNotFoundException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
this.cache = new byte[8192];
|
||
|
this.cachestart = 0;
|
||
|
this.cachelen = 0;
|
||
|
}
|
||
16 years ago
|
|
||
|
protected void finalize() throws Throwable {
|
||
16 years ago
|
this.close();
|
||
16 years ago
|
super.finalize();
|
||
|
}
|
||
|
|
||
|
}
|