Quadcap Embeddable Database

com/quadcap/sql/file/ByteArrayRandomAccess.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 com.quadcap.util.Debug; 00042 import com.quadcap.util.Util; 00043 00044 /** 00045 * A <code>RandomAccess</code> implementation using a byte array. 00046 * 00047 * @author Stan Bailes 00048 */ 00049 public class ByteArrayRandomAccess extends RandomAccess { 00050 byte[] buf; 00051 int size; 00052 00053 public ByteArrayRandomAccess() { 00054 this.size = 0; 00055 setCapacity(1024); 00056 } 00057 00058 public ByteArrayRandomAccess(int initialCap) { 00059 this.size = 0; 00060 setCapacity(initialCap); 00061 } 00062 00063 public ByteArrayRandomAccess(byte[] buf) { 00064 this.buf = buf; 00065 this.size = buf.length; 00066 } 00067 00068 public void reset(byte[] buf, int size) { 00069 this.buf = buf; 00070 this.size = size; 00071 } 00072 00073 public byte[] getBytes() { 00074 return buf; 00075 } 00076 00077 final void setCapacity(long cap) { 00078 if (buf == null || cap > buf.length) { 00079 byte[] nbuf = new byte[(int)cap]; 00080 if (size > 0) { 00081 System.arraycopy(buf, 0, nbuf, 0, size); 00082 } 00083 this.buf = nbuf; 00084 } 00085 } 00086 00087 public byte[] toByteArray() { 00088 byte[] ret = new byte[size]; 00089 read(0, ret, 0, size); 00090 return ret; 00091 } 00092 00093 /** 00094 * Return the size of the managed region. 00095 */ 00096 public long size() { 00097 return size; 00098 } 00099 00100 /** 00101 * Resize the managed region. 00102 */ 00103 public void resize(long newSize) { 00104 if (newSize > buf.length) { 00105 setCapacity(newSize + (newSize >> 1)); 00106 } 00107 this.size = (int)newSize; 00108 } 00109 00110 /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer 00111 * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed 00112 * area. 00113 */ 00114 public void write(long pos, byte[] b, int offset, int len) { 00115 //#ifdef DEBUG 00116 if (trace) { 00117 Debug.println("BAR:write(" + pos + ", " + 00118 Util.hexBytes(b,offset,len) + ")"); 00119 } 00120 //#endif 00121 System.arraycopy(b, offset, buf, (int)pos, len); 00122 } 00123 00124 /** 00125 * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region 00126 * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>. 00127 */ 00128 public void read(long pos, byte[] b, int offset, int len) { 00129 //#ifdef DEBUG 00130 if (trace) { 00131 Debug.println("BAR:read(" + pos + ", " + 00132 Util.hexBytes(b,offset,len) + ")"); 00133 } 00134 //#endif 00135 System.arraycopy(buf, (int)pos, b, offset, len); 00136 } 00137 00138 //#ifdef DEBUG 00139 static final boolean trace = false; 00140 //#endif 00141 00142 public final void writeByte(int pos, int val) { 00143 buf[pos] = (byte)(val & 0xff); 00144 } 00145 00146 public final int readByte(int pos) { 00147 return buf[pos] & 0xff; 00148 } 00149 00150 /** 00151 * Finalization... 00152 */ 00153 public void close() { 00154 } 00155 00156 public void flush() { 00157 } 00158 }