001: // kelondroAbstractIOChunks.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, 2005
007: // created: 11.12.2005
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.IOException;
045:
046: public abstract class kelondroAbstractIOChunks {
047:
048: // logging support
049: protected String name = null;
050:
051: public String name() {
052: return name;
053: }
054:
055: // profiling support
056: protected kelondroProfile profile = new kelondroProfile();
057:
058: public kelondroProfile profile() {
059: return profile;
060: }
061:
062: // pseudo-native methods:
063: abstract public long length() throws IOException;
064:
065: abstract public int read(long pos, byte[] b, int off, int len)
066: throws IOException;
067:
068: abstract public void write(long pos, byte[] b, int off, int len)
069: throws IOException;
070:
071: abstract public void close() throws IOException;
072:
073: // derived methods:
074: public synchronized void readFully(long pos, byte[] b, int off,
075: int len) throws IOException {
076: long handle = profile.startRead();
077: if (len < 0)
078: throw new IndexOutOfBoundsException("length is negative:"
079: + len);
080: if (b.length < off + len)
081: throw new IndexOutOfBoundsException(
082: "bounds do not fit: b.length=" + b.length
083: + ", off=" + off + ", len=" + len);
084: while (len > 0) {
085: int r = read(pos, b, off, len); // blocks until at least one byte is available
086: if (r < 0)
087: throw new IOException("EOF"); // read exceeded EOF
088: if (r == 0)
089: throw new IOException(
090: "readFully cannot read remaining " + len
091: + " bytes"); // security exception to prevent endless loops
092: pos += r;
093: off += r;
094: len -= r;
095: }
096: profile.stopRead(handle);
097: }
098:
099: public synchronized byte readByte(long pos) throws IOException {
100: byte[] b = new byte[1];
101: this .readFully(pos, b, 0, 1);
102: return b[0];
103: }
104:
105: public synchronized void writeByte(long pos, final int v)
106: throws IOException {
107: long handle = profile.startWrite();
108: this .write(pos, new byte[] { (byte) (v & 0xFF) });
109: profile.stopWrite(handle);
110: }
111:
112: public synchronized short readShort(long pos) throws IOException {
113: byte[] b = new byte[2];
114: this .readFully(pos, b, 0, 2);
115: return (short) ((((int) b[0] & 0xFF) << 8) | (((int) b[1] & 0xFF) << 0));
116: }
117:
118: public synchronized void writeShort(long pos, final int v)
119: throws IOException {
120: long handle = profile.startWrite();
121: this .write(pos, new byte[] { (byte) ((v >>> 8) & 0xFF),
122: (byte) ((v >>> 0) & 0xFF) });
123: profile.stopWrite(handle);
124: }
125:
126: public synchronized int readInt(long pos) throws IOException {
127: byte[] b = new byte[4];
128: this .readFully(pos, b, 0, 4);
129: return (((int) b[0] & 0xFF) << 24)
130: | (((int) b[1] & 0xFF) << 16)
131: | (((int) b[2] & 0xFF) << 8) | ((int) b[3] & 0xFF);
132: }
133:
134: public synchronized void writeInt(long pos, final int v)
135: throws IOException {
136: long handle = profile.startWrite();
137: this .write(pos, new byte[] { (byte) ((v >>> 24) & 0xFF),
138: (byte) ((v >>> 16) & 0xFF), (byte) ((v >>> 8) & 0xFF),
139: (byte) ((v >>> 0) & 0xFF) });
140: profile.stopWrite(handle);
141: }
142:
143: public synchronized long readLong(long pos) throws IOException {
144: byte[] b = new byte[8];
145: this .readFully(pos, b, 0, 8);
146: return (((long) b[0] & 0xFF) << 56)
147: | (((long) b[1] & 0xFF) << 48) | (((long) b[2]) << 40)
148: | (((long) b[3] & 0xFF) << 32)
149: | (((long) b[4] & 0xFF) << 24)
150: | (((long) b[5] & 0xFF) << 16)
151: | (((long) b[6] & 0xFF) << 8) | ((long) b[7] & 0xFF);
152: }
153:
154: public synchronized void writeLong(long pos, final long v)
155: throws IOException {
156: long handle = profile.startWrite();
157: this .write(pos, new byte[] { (byte) ((v >>> 56) & 0xFF),
158: (byte) ((v >>> 48) & 0xFF), (byte) ((v >>> 40) & 0xFF),
159: (byte) ((v >>> 32) & 0xFF), (byte) ((v >>> 24) & 0xFF),
160: (byte) ((v >>> 16) & 0xFF), (byte) ((v >>> 8) & 0xFF),
161: (byte) ((v >>> 0) & 0xFF) });
162: profile.stopWrite(handle);
163: }
164:
165: public synchronized void write(long pos, final byte[] b)
166: throws IOException {
167: long handle = profile.startWrite();
168: this .write(pos, b, 0, b.length);
169: profile.stopWrite(handle);
170: }
171:
172: }
|