Quadcap Embeddable Database

com/quadcap/sql/file/FileRandomAccess.java

Go to the documentation of this file.
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 import java.io.RandomAccessFile; 00043 00044 /** 00045 * 00046 * 00047 * @author Stan Bailes 00048 */ 00049 public class FileRandomAccess extends RandomAccess { 00050 long pos; 00051 long maxSize; 00052 RandomAccessFile ra; 00053 00054 public FileRandomAccess(RandomAccessFile ra, long maxSize) { 00055 this.maxSize = maxSize; 00056 this.ra = ra; 00057 } 00058 00059 /** 00060 * Return the size of the managed region. 00061 */ 00062 public long size() { 00063 try { 00064 return ra.length(); 00065 } catch (IOException ex) { 00066 return -1; 00067 } 00068 } 00069 00070 /** 00071 * Resize the managed region. 00072 */ 00073 public void resize(long newSize) throws IOException { 00074 ra.setLength(newSize); 00075 } 00076 00077 /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer 00078 * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed 00079 * area. 00080 */ 00081 public void write(long p, byte[] b, int offset, int len) 00082 throws IOException 00083 { 00084 checkCapacity(p+len); 00085 if (p != pos) { 00086 ra.seek(p); 00087 pos = p; 00088 } 00089 ra.write(b, offset, len); 00090 pos += len; 00091 } 00092 00093 /** 00094 * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region 00095 * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>. 00096 */ 00097 public void read(long p, byte[] b, int offset, int len) 00098 throws IOException 00099 { 00100 if (p != pos) { 00101 ra.seek(p); 00102 pos = p; 00103 } 00104 ra.read(b, offset, len); 00105 pos += len; 00106 } 00107 00108 /** 00109 * Finalization... 00110 */ 00111 public void close() throws IOException { 00112 ra.close(); 00113 } 00114 00115 public void flush() throws IOException { 00116 try { 00117 ra.getFD().sync(); 00118 } catch (Throwable t) { 00119 } 00120 } 00121 00122 final void checkCapacity(long t) throws IOException { 00123 if (t > ra.length()) { 00124 if (t > maxSize) { 00125 throw new IOException("full"); 00126 } 00127 try { 00128 ra.setLength(t); 00129 } catch (IOException e) { 00130 if (e.toString().indexOf("extended attributes") > 0) { 00131 return; 00132 } 00133 throw e; 00134 } 00135 } 00136 } 00137 }