001: package com.quadcap.sql.file;
002:
003: /* Copyright 1999 - 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: import java.io.RandomAccessFile;
043:
044: /**
045: *
046: *
047: * @author Stan Bailes
048: */
049: public class FileRandomAccess extends RandomAccess {
050: long pos;
051: long maxSize;
052: RandomAccessFile ra;
053:
054: public FileRandomAccess(RandomAccessFile ra, long maxSize) {
055: this .maxSize = maxSize;
056: this .ra = ra;
057: }
058:
059: /**
060: * Return the size of the managed region.
061: */
062: public long size() {
063: try {
064: return ra.length();
065: } catch (IOException ex) {
066: return -1;
067: }
068: }
069:
070: /**
071: * Resize the managed region.
072: */
073: public void resize(long newSize) throws IOException {
074: ra.setLength(newSize);
075: }
076:
077: /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer
078: * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed
079: * area.
080: */
081: public void write(long p, byte[] b, int offset, int len)
082: throws IOException {
083: checkCapacity(p + len);
084: if (p != pos) {
085: ra.seek(p);
086: pos = p;
087: }
088: ra.write(b, offset, len);
089: pos += len;
090: }
091:
092: /**
093: * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region
094: * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>.
095: */
096: public void read(long p, byte[] b, int offset, int len)
097: throws IOException {
098: if (p != pos) {
099: ra.seek(p);
100: pos = p;
101: }
102: ra.read(b, offset, len);
103: pos += len;
104: }
105:
106: /**
107: * Finalization...
108: */
109: public void close() throws IOException {
110: ra.close();
111: }
112:
113: public void flush() throws IOException {
114: try {
115: ra.getFD().sync();
116: } catch (Throwable t) {
117: }
118: }
119:
120: final void checkCapacity(long t) throws IOException {
121: if (t > ra.length()) {
122: if (t > maxSize) {
123: throw new IOException("full");
124: }
125: try {
126: ra.setLength(t);
127: } catch (IOException e) {
128: if (e.toString().indexOf("extended attributes") > 0) {
129: return;
130: }
131: throw e;
132: }
133: }
134: }
135: }
|