Quadcap Embeddable Database

com/quadcap/sql/file/BufferedRandomAccess.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 00043 import com.quadcap.util.Debug; 00044 00045 /** 00046 * A <code>RandomAccess</code> implementation using a byte array. 00047 * 00048 * @author Stan Bailes 00049 */ 00050 public class BufferedRandomAccess extends RandomAccess { 00051 static final int BUFSIZE = 16384; 00052 00053 class Buffer { 00054 byte[] buf = new byte[BUFSIZE]; 00055 long adr = -1; 00056 boolean dty = false; 00057 00058 //#ifdef DEBUG 00059 public String toString() { 00060 return "Buffer[" + adr + "]" + (dty?"*":""); 00061 } 00062 //#endif 00063 } 00064 Buffer b1 = new Buffer(); 00065 Buffer b2 = new Buffer(); 00066 Buffer b = null; 00067 00068 RandomAccess ra; 00069 long size; 00070 00071 public BufferedRandomAccess(RandomAccess ra) { 00072 this.ra = ra; 00073 size = ra.size(); 00074 } 00075 00076 /** 00077 * Return the size of the managed region. 00078 */ 00079 public long size() { 00080 return size; 00081 } 00082 00083 /** 00084 * Resize the managed region. 00085 */ 00086 public void resize(long newSize) throws IOException { 00087 size = newSize; 00088 ra.resize(newSize); 00089 } 00090 00091 /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer 00092 * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed 00093 * area. 00094 */ 00095 public void write(long pos, byte[] buf, int offset, int len) 00096 throws IOException 00097 { 00098 do { 00099 long pb = pos & ~(BUFSIZE-1); 00100 long pe = pb + BUFSIZE-1; 00101 if (pe >= size) pe = size-1; 00102 int xlen = (int)(pe - pos + 1); 00103 if (xlen > len) xlen = len; 00104 if (xlen > 0) { 00105 getBuffer(pb); 00106 System.arraycopy(buf, offset, b.buf, (int)(pos-pb), xlen); 00107 b.dty = true; 00108 offset += xlen; 00109 pos += xlen; 00110 len -= xlen; 00111 } else { 00112 len = 0; 00113 } 00114 } while (len > 0); 00115 00116 } 00117 00118 /** 00119 * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region 00120 * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>. 00121 */ 00122 public void read(long pos, byte[] buf, int offset, int len) 00123 throws IOException 00124 { 00125 do { 00126 long pb = pos & ~(BUFSIZE-1); 00127 long pe = pb + BUFSIZE-1; 00128 if (pe >= size) pe = size-1; 00129 int xlen = (int)(pe - pos + 1); 00130 if (xlen > len) xlen = len; 00131 if (xlen > 0) { 00132 getBuffer(pb); 00133 System.arraycopy(b.buf, (int)(pos-pb), buf, offset, xlen); 00134 offset += xlen; 00135 pos += xlen; 00136 len -= xlen; 00137 } else { 00138 len = 0; 00139 } 00140 } while (len > 0); 00141 } 00142 00143 final void getBuffer(long addr) throws IOException { 00144 if (addr == b1.adr) { 00145 b = b1; 00146 } else if (addr == b2.adr) { 00147 b = b2; 00148 } else { 00149 b = (b == b1) ? b2 : b1; 00150 if (b.dty) flush(b); 00151 b.adr = addr; 00152 int xlen = (int)(ra.size() - addr); 00153 if (xlen > BUFSIZE) xlen = BUFSIZE; 00154 ra.read(addr, b.buf, 0, xlen); 00155 } 00156 } 00157 00158 /** 00159 * Finalization... 00160 */ 00161 public void close() throws IOException { 00162 flush(); 00163 ra.close(); 00164 } 00165 00166 final void flushBuffers() throws IOException { 00167 flush(b1); 00168 flush(b2); 00169 } 00170 00171 public void flush() throws IOException { 00172 flushBuffers(); 00173 ra.flush(); 00174 } 00175 00176 final void flush(Buffer b) throws IOException { 00177 if (b.dty) { 00178 int xlen = (int)(ra.size() - b.adr); 00179 if (xlen > BUFSIZE) xlen = BUFSIZE; 00180 ra.write(b.adr, b.buf, 0, xlen); 00181 b.dty = false; 00182 } 00183 } 00184 }