Quadcap Embeddable Database

com/quadcap/sql/file/RandomAccess.java

Go to the documentation of this file.
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1997 - 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 /** 00044 * This interface is used to access a region of consecutive bytes in a 00045 * random access manner.<p> 00046 * 00047 * @author Stan Bailes 00048 */ 00049 00050 public abstract class RandomAccess { 00051 byte[] fmtBuf = new byte[8]; 00052 /** 00053 * Return the size of the managed region. 00054 */ 00055 public abstract long size(); 00056 00057 /** 00058 * Resize the managed region. 00059 */ 00060 public abstract void resize(long newSize) throws IOException; 00061 00062 /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer 00063 * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed 00064 * area. 00065 */ 00066 public abstract void write(long pos, byte[] buf, int offset, int len) 00067 throws IOException; 00068 00069 /** 00070 * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region 00071 * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>. 00072 */ 00073 public abstract void read(long pos, byte[] buf, int offset, int len) 00074 throws IOException; 00075 00076 /** 00077 * Finalization... 00078 */ 00079 public abstract void close() throws IOException; 00080 00081 public abstract void flush() throws IOException; 00082 00083 public void writeByte(int pos, int val) throws IOException { 00084 synchronized (fmtBuf) { 00085 fmtBuf[0] = (byte)(val & 0xff); 00086 write(pos, fmtBuf, 0, 1); 00087 } 00088 } 00089 00090 public int readByte(int pos) throws IOException { 00091 synchronized (fmtBuf) { 00092 read(pos, fmtBuf, 0, 1); 00093 return fmtBuf[0] & 0xff; 00094 } 00095 } 00096 00097 /** 00098 * Write a integer value to the specified position in the buffer 00099 */ 00100 public void writeInt(long pos, int val) throws IOException { 00101 synchronized (fmtBuf) { 00102 ByteUtil.putInt(fmtBuf, 0, val); 00103 write(pos, fmtBuf, 0, 4); 00104 } 00105 } 00106 00107 /** 00108 * Write a integer value to the specified position in the buffer 00109 */ 00110 public int readInt(long pos) throws IOException { 00111 synchronized (fmtBuf) { 00112 read(pos, fmtBuf, 0, 4); 00113 return ByteUtil.getInt(fmtBuf, 0); 00114 } 00115 } 00116 00117 /** 00118 * Write a long value to the specified position in the buffer 00119 */ 00120 public void writeLong(long pos, long val) throws IOException { 00121 synchronized (fmtBuf) { 00122 ByteUtil.putLong(fmtBuf, 0, val); 00123 write(pos, fmtBuf, 0, 8); 00124 } 00125 } 00126 00127 /** 00128 * Write a long value to the specified position in the buffer 00129 */ 00130 public long readLong(long pos) throws IOException { 00131 synchronized (fmtBuf) { 00132 read(pos, fmtBuf, 0, 8); 00133 return ByteUtil.getLong(fmtBuf, 0); 00134 } 00135 } 00136 }