001: package com.quadcap.sql.file;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: /**
044: * This interface is used to access a region of consecutive bytes in a
045: * random access manner.<p>
046: *
047: * @author Stan Bailes
048: */
049:
050: public abstract class RandomAccess {
051: byte[] fmtBuf = new byte[8];
052:
053: /**
054: * Return the size of the managed region.
055: */
056: public abstract long size();
057:
058: /**
059: * Resize the managed region.
060: */
061: public abstract void resize(long newSize) throws IOException;
062:
063: /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer
064: * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed
065: * area.
066: */
067: public abstract void write(long pos, byte[] buf, int offset, int len)
068: throws IOException;
069:
070: /**
071: * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region
072: * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>.
073: */
074: public abstract void read(long pos, byte[] buf, int offset, int len)
075: throws IOException;
076:
077: /**
078: * Finalization...
079: */
080: public abstract void close() throws IOException;
081:
082: public abstract void flush() throws IOException;
083:
084: public void writeByte(int pos, int val) throws IOException {
085: synchronized (fmtBuf) {
086: fmtBuf[0] = (byte) (val & 0xff);
087: write(pos, fmtBuf, 0, 1);
088: }
089: }
090:
091: public int readByte(int pos) throws IOException {
092: synchronized (fmtBuf) {
093: read(pos, fmtBuf, 0, 1);
094: return fmtBuf[0] & 0xff;
095: }
096: }
097:
098: /**
099: * Write a integer value to the specified position in the buffer
100: */
101: public void writeInt(long pos, int val) throws IOException {
102: synchronized (fmtBuf) {
103: ByteUtil.putInt(fmtBuf, 0, val);
104: write(pos, fmtBuf, 0, 4);
105: }
106: }
107:
108: /**
109: * Write a integer value to the specified position in the buffer
110: */
111: public int readInt(long pos) throws IOException {
112: synchronized (fmtBuf) {
113: read(pos, fmtBuf, 0, 4);
114: return ByteUtil.getInt(fmtBuf, 0);
115: }
116: }
117:
118: /**
119: * Write a long value to the specified position in the buffer
120: */
121: public void writeLong(long pos, long val) throws IOException {
122: synchronized (fmtBuf) {
123: ByteUtil.putLong(fmtBuf, 0, val);
124: write(pos, fmtBuf, 0, 8);
125: }
126: }
127:
128: /**
129: * Write a long value to the specified position in the buffer
130: */
131: public long readLong(long pos) throws IOException {
132: synchronized (fmtBuf) {
133: read(pos, fmtBuf, 0, 8);
134: return ByteUtil.getLong(fmtBuf, 0);
135: }
136: }
137: }
|