Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added a new file reader cache that may serve as full-file-copy of b…
…lob database files. This is not yet used - removed class FileWriter and replaced all usage of that class with CachedFileWriter git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6309 6c8d7289-2bf4-0310-a012-ef5d649a1542
- Loading branch information
orbiter
committed
Sep 9, 2009
1 parent
fd6b9cb
commit 27d0028
Showing
4 changed files
with
109 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// CachedFileReader.java | ||
// --------------------- | ||
// (C) 2009 by Michael Peter Christen; mc@yacy.net | ||
// first published 09.09.2009 on http://yacy.net | ||
// | ||
// 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 | ||
|
||
|
||
package de.anomic.kelondro.io.random; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
|
||
import de.anomic.kelondro.util.MemoryControl; | ||
|
||
public class CachedFileReader extends AbstractReader implements Reader { | ||
|
||
private RandomAccessFile RAFile; | ||
private byte[] cache; | ||
private int cachelen; | ||
|
||
public CachedFileReader(final File file) throws IOException, FileNotFoundException { | ||
this.name = file.getName(); | ||
this.file = file; | ||
this.RAFile = new RandomAccessFile(this.file, "r"); | ||
if (MemoryControl.available() / 10L > this.RAFile.length() && this.RAFile.length() < (long) Integer.MAX_VALUE) { | ||
this.cache = new byte[(int) this.RAFile.length()]; | ||
this.RAFile.seek(0); | ||
this.RAFile.readFully(this.cache); | ||
} else { | ||
this.cache = null; | ||
} | ||
this.cachelen = 0; | ||
} | ||
|
||
public synchronized long available() throws IOException { | ||
return this.length() - RAFile.getFilePointer(); | ||
} | ||
|
||
public synchronized long length() throws IOException { | ||
return this.RAFile.length(); | ||
} | ||
|
||
public synchronized final void readFully(final byte[] b, final int off, int len) throws IOException { | ||
long seek = RAFile.getFilePointer(); | ||
if (cache != null && cachelen - seek >= len) { | ||
// read from cache | ||
System.arraycopy(cache, (int) (seek), b, off, len); | ||
RAFile.seek(seek + len); | ||
return; | ||
} | ||
// cannot use the cache | ||
RAFile.readFully(b, off, len); | ||
return; | ||
} | ||
|
||
public synchronized void seek(final long pos) throws IOException { | ||
RAFile.seek(pos); | ||
} | ||
|
||
public synchronized void close() { | ||
if (RAFile != null) try { | ||
try{RAFile.getChannel().close();} catch (IOException e) {} | ||
RAFile.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
this.cache = null; | ||
this.RAFile = null; | ||
} | ||
|
||
protected void finalize() throws Throwable { | ||
this.close(); | ||
super.finalize(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters