001: // kelondroAbstractRA.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: //
008: // $LastChangedDate: 2008-01-29 10:12:48 +0000 (Di, 29 Jan 2008) $
009: // $LastChangedRevision: 4414 $
010: // $LastChangedBy: orbiter $
011: //
012: // This program is free software; you can redistribute it and/or modify
013: // it under the terms of the GNU General Public License as published by
014: // the Free Software Foundation; either version 2 of the License, or
015: // (at your option) any later version.
016: //
017: // This program is distributed in the hope that it will be useful,
018: // but WITHOUT ANY WARRANTY; without even the implied warranty of
019: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: // GNU General Public License for more details.
021: //
022: // You should have received a copy of the GNU General Public License
023: // along with this program; if not, write to the Free Software
024: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: // Using this software in any meaning (reading, learning, copying, compiling,
027: // running) means that you agree that the Author(s) is (are) not responsible
028: // for cost, loss of data or any harm that may be caused directly or indirectly
029: // by usage of this softare or this documentation. The usage of this software
030: // is on your own risk. The installation and usage (starting/running) of this
031: // software may allow other people or application to access your computer and
032: // any attached devices and is highly dependent on the configuration of the
033: // software which must be done by the user of the software; the author(s) is
034: // (are) also not responsible for proper configuration and usage of the
035: // software, even if provoked by documentation provided together with
036: // the software.
037: //
038: // Any changes to this file according to the GPL as documented in the file
039: // gpl.txt aside this file in the shipment you received can be done to the
040: // lines that follows this copyright notice here, but changes must not be
041: // done inside the copyright notive above. A re-distribution must contain
042: // the intact and unchanged copyright notice.
043: // Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.kelondro;
046:
047: import java.io.BufferedReader;
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.IOException;
051: import java.io.InputStreamReader;
052: import java.util.HashMap;
053: import java.util.Iterator;
054: import java.util.Map;
055:
056: import de.anomic.server.serverByteBuffer;
057:
058: abstract class kelondroAbstractRA implements kelondroRA {
059:
060: // logging support
061: protected String name = null;
062:
063: public String name() {
064: return name;
065: }
066:
067: // pseudo-native methods:
068: abstract public long length() throws IOException;
069:
070: abstract public long available() throws IOException;
071:
072: abstract public int read() throws IOException;
073:
074: abstract public void write(int b) throws IOException;
075:
076: abstract public int read(byte[] b, int off, int len)
077: throws IOException;
078:
079: abstract public void write(byte[] b, int off, int len)
080: throws IOException;
081:
082: abstract public void seek(long pos) throws IOException;
083:
084: abstract public void close() throws IOException;
085:
086: // derived methods:
087: public void readFully(byte[] b, int off, int len)
088: throws IOException {
089: if (len < 0)
090: throw new IndexOutOfBoundsException("length is negative:"
091: + len);
092: if (b.length < off + len)
093: throw new IndexOutOfBoundsException(
094: "bounds do not fit: b.length=" + b.length
095: + ", off=" + off + ", len=" + len);
096: while (len > 0) {
097: int r = read(b, off, len);
098: if (r < 0)
099: throw new IOException("EOF"); // read exceeded EOF
100: off += r;
101: len -= r;
102: }
103: }
104:
105: public byte[] readFully() throws IOException {
106: ByteArrayOutputStream dest = new ByteArrayOutputStream();
107: byte[] buffer = new byte[1024];
108:
109: int c, total = 0;
110: while ((c = read(buffer, 0, 1024)) > 0) {
111: dest.write(buffer, 0, c);
112: total += c;
113: }
114: dest.flush();
115: dest.close();
116: return dest.toByteArray();
117: }
118:
119: public byte readByte() throws IOException {
120: final int ch = this .read();
121: if (ch < 0)
122: throw new IOException();
123: return (byte) (ch);
124: }
125:
126: public void writeByte(final int v) throws IOException {
127: this .write(v);
128: }
129:
130: public short readShort() throws IOException {
131: final int ch1 = this .read();
132: final int ch2 = this .read();
133: if ((ch1 | ch2) < 0)
134: throw new IOException();
135: return (short) ((ch1 << 8) | (ch2 << 0));
136: }
137:
138: public void writeShort(final int v) throws IOException {
139: this .write((v >>> 8) & 0xFF);
140: this .write((v >>> 0) & 0xFF);
141: }
142:
143: public int readInt() throws IOException {
144: final int ch1 = this .read();
145: final int ch2 = this .read();
146: final int ch3 = this .read();
147: final int ch4 = this .read();
148: if ((ch1 | ch2 | ch3 | ch4) < 0)
149: throw new IOException(
150: "kelondroAbstractRA.readInt: wrong values; ch1="
151: + ch1 + ", ch2=" + ch2 + ", ch3=" + ch3
152: + ", ch4=" + ch4);
153: return ((ch1 << 24) | (ch2 << 16) | (ch3 << 8) | ch4);
154: }
155:
156: public void writeInt(final int v) throws IOException {
157: this .write((v >>> 24) & 0xFF);
158: this .write((v >>> 16) & 0xFF);
159: this .write((v >>> 8) & 0xFF);
160: this .write((v >>> 0) & 0xFF);
161: }
162:
163: public long readLong() throws IOException {
164: return ((long) (readInt()) << 32) | (readInt() & 0xFFFFFFFFL);
165: }
166:
167: public void writeLong(final long v) throws IOException {
168: this .write((int) (v >>> 56) & 0xFF);
169: this .write((int) (v >>> 48) & 0xFF);
170: this .write((int) (v >>> 40) & 0xFF);
171: this .write((int) (v >>> 32) & 0xFF);
172: this .write((int) (v >>> 24) & 0xFF);
173: this .write((int) (v >>> 16) & 0xFF);
174: this .write((int) (v >>> 8) & 0xFF);
175: this .write((int) (v >>> 0) & 0xFF);
176: }
177:
178: public void write(final byte[] b) throws IOException {
179: this .write(b, 0, b.length);
180: }
181:
182: private static final byte cr = 13;
183: private static final byte lf = 10;
184:
185: public void writeLine(final String line) throws IOException {
186: byte[] b = new byte[line.length() + 2];
187: System.arraycopy(line.getBytes(), 0, b, 0, line.length());
188: b[b.length - 2] = cr;
189: b[b.length - 1] = lf;
190: this .write(b);
191: }
192:
193: public void writeLine(final byte[] line) throws IOException {
194: byte[] b = new byte[line.length + 2];
195: System.arraycopy(line, 0, b, 0, line.length);
196: b[b.length - 2] = cr;
197: b[b.length - 1] = lf;
198: this .write(b);
199: }
200:
201: public String readLine() throws IOException {
202: // with these functions, we consider a line as always terminated by CRLF
203: byte[] bb = new byte[80];
204: int bbsize = 0;
205: int c;
206: while (true) {
207: c = read();
208: if (c < 0) {
209: if (bbsize == 0)
210: return null;
211: return new String(bb, 0, bbsize);
212: }
213: if (c == cr)
214: continue;
215: if (c == lf)
216: return new String(bb, 0, bbsize, "UTF-8");
217:
218: // append to bb
219: if (bbsize == bb.length) {
220: // extend bb size
221: byte[] newbb = new byte[bb.length * 2];
222: System.arraycopy(bb, 0, newbb, 0, bb.length);
223: bb = newbb;
224: newbb = null;
225: }
226: bb[bbsize++] = (byte) c;
227: }
228: }
229:
230: public void writeMap(final Map<String, String> map,
231: final String comment) throws IOException {
232: this .seek(0);
233: final Iterator<Map.Entry<String, String>> iter = map.entrySet()
234: .iterator();
235: Map.Entry<String, String> entry;
236: final serverByteBuffer bb = new serverByteBuffer(
237: map.size() * 40);
238: bb.append("# ").append(comment).append("\r\n");
239: while (iter.hasNext()) {
240: entry = iter.next();
241: bb.append(entry.getKey()).append('=');
242: if (entry.getValue() != null) {
243: bb.append(entry.getValue());
244: }
245: bb.append("\r\n");
246: }
247: bb.append("# EOF\r\n");
248: write(bb.getBytes());
249: }
250:
251: public HashMap<String, String> readMap() throws IOException {
252: this .seek(0);
253: byte[] b = readFully();
254: BufferedReader br = new BufferedReader(new InputStreamReader(
255: new ByteArrayInputStream(b)));
256: final HashMap<String, String> map = new HashMap<String, String>();
257: String line;
258: int pos;
259: while ((line = br.readLine()) != null) { // very slow readLine????
260: line = line.trim();
261: if (line.equals("# EOF"))
262: return map;
263: if ((line.length() == 0) || (line.charAt(0) == '#'))
264: continue;
265: pos = line.indexOf("=");
266: if (pos < 0)
267: continue;
268: map.put(line.substring(0, pos), line.substring(pos + 1));
269: }
270: return map;
271: }
272:
273: /**
274: * this does not write the content to the see position
275: * but to the very beginning of the record
276: * some additional bytes will ensure that we know the correct content size later on
277: */
278: public void writeArray(final byte[] b) throws IOException {
279: seek(0);
280: writeInt(b.length);
281: write(b);
282: }
283:
284: public byte[] readArray() throws IOException {
285: seek(0);
286: final int l = readInt();
287: final byte[] b = new byte[l];
288: read(b, 0, l);
289: return b;
290: }
291:
292: }
|