001: // kelondroFileRA.java
002: // -----------------------
003: // part of The Kelondro Database
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2004
007: // last major change: 05.02.2004
008: //
009: // This program is free software; you can redistribute it and/or modify
010: // it under the terms of the GNU General Public License as published by
011: // the Free Software Foundation; either version 2 of the License, or
012: // (at your option) any later version.
013: //
014: // This program is distributed in the hope that it will be useful,
015: // but WITHOUT ANY WARRANTY; without even the implied warranty of
016: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: // GNU General Public License for more details.
018: //
019: // You should have received a copy of the GNU General Public License
020: // along with this program; if not, write to the Free Software
021: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: //
023: // Using this software in any meaning (reading, learning, copying, compiling,
024: // running) means that you agree that the Author(s) is (are) not responsible
025: // for cost, loss of data or any harm that may be caused directly or indirectly
026: // by usage of this softare or this documentation. The usage of this software
027: // is on your own risk. The installation and usage (starting/running) of this
028: // software may allow other people or application to access your computer and
029: // any attached devices and is highly dependent on the configuration of the
030: // software which must be done by the user of the software; the author(s) is
031: // (are) also not responsible for proper configuration and usage of the
032: // software, even if provoked by documentation provided together with
033: // the software.
034: //
035: // Any changes to this file according to the GPL as documented in the file
036: // gpl.txt aside this file in the shipment you received can be done to the
037: // lines that follows this copyright notice here, but changes must not be
038: // done inside the copyright notive above. A re-distribution must contain
039: // the intact and unchanged copyright notice.
040: // Contributions and changes to the program code must be marked as such.
041:
042: package de.anomic.kelondro;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.IOException;
047: import java.io.RandomAccessFile;
048: import java.util.Map;
049:
050: public final class kelondroFileRA extends kelondroAbstractRA implements
051: kelondroRA {
052:
053: protected RandomAccessFile RAFile;
054:
055: public kelondroFileRA(String file) throws IOException,
056: FileNotFoundException {
057: this (new File(file));
058: }
059:
060: public kelondroFileRA(File file) throws IOException,
061: FileNotFoundException {
062: this .name = file.getName();
063: RAFile = new RandomAccessFile(file, "rw");
064: }
065:
066: public long length() throws IOException {
067: return RAFile.length();
068: }
069:
070: public long available() throws IOException {
071: return RAFile.length() - RAFile.getFilePointer();
072: }
073:
074: // pseudo-native method read
075: public int read() throws IOException {
076: return RAFile.read();
077: }
078:
079: // pseudo-native method write
080: public void write(int b) throws IOException {
081: RAFile.write(b);
082: }
083:
084: public int read(byte[] b, int off, int len) throws IOException {
085: return RAFile.read(b, off, len);
086: }
087:
088: public void write(byte[] b, int off, int len) throws IOException {
089: // write to file
090: RAFile.write(b, off, len);
091: }
092:
093: public void seek(long pos) throws IOException {
094: RAFile.seek(pos);
095: }
096:
097: public void close() throws IOException {
098: if (RAFile != null)
099: RAFile.close();
100: RAFile = null;
101: }
102:
103: protected void finalize() throws Throwable {
104: if (RAFile != null) {
105: this .close();
106: }
107: super .finalize();
108: }
109:
110: // some static tools
111: public static void writeMap(File f, Map<String, String> map,
112: String comment) throws IOException {
113: File fp = f.getParentFile();
114: if (fp != null)
115: fp.mkdirs();
116: kelondroRA kra = null;
117: try {
118: kra = new kelondroFileRA(f);
119: kra.writeMap(map, comment);
120: kra.close();
121: } finally {
122: if (kra != null)
123: try {
124: kra.close();
125: } catch (Exception e) {
126: }
127: }
128: }
129:
130: public static Map<String, String> readMap(File f)
131: throws IOException {
132: kelondroRA kra = null;
133: try {
134: kra = new kelondroFileRA(f);
135: Map<String, String> map = kra.readMap();
136: kra.close();
137: return map;
138: } finally {
139: if (kra != null)
140: try {
141: kra.close();
142: } catch (Exception e) {
143: }
144: }
145: }
146:
147: }
|