KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > ByteArrayRandomAccess


1 package com.quadcap.sql.file;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import com.quadcap.util.Debug;
42 import com.quadcap.util.Util;
43
44 /**
45  * A <code>RandomAccess</code> implementation using a byte array.
46  *
47  * @author Stan Bailes
48  */

49 public class ByteArrayRandomAccess extends RandomAccess {
50     byte[] buf;
51     int size;
52
53     public ByteArrayRandomAccess() {
54     this.size = 0;
55     setCapacity(1024);
56     }
57
58     public ByteArrayRandomAccess(int initialCap) {
59     this.size = 0;
60     setCapacity(initialCap);
61     }
62
63     public ByteArrayRandomAccess(byte[] buf) {
64     this.buf = buf;
65     this.size = buf.length;
66     }
67
68     public void reset(byte[] buf, int size) {
69         this.buf = buf;
70         this.size = size;
71     }
72
73     public byte[] getBytes() {
74         return buf;
75     }
76
77     final void setCapacity(long cap) {
78     if (buf == null || cap > buf.length) {
79         byte[] nbuf = new byte[(int)cap];
80         if (size > 0) {
81         System.arraycopy(buf, 0, nbuf, 0, size);
82         }
83         this.buf = nbuf;
84     }
85     }
86
87     public byte[] toByteArray() {
88     byte[] ret = new byte[size];
89     read(0, ret, 0, size);
90     return ret;
91     }
92
93     /**
94      * Return the size of the managed region.
95      */

96     public long size() {
97     return size;
98     }
99
100     /**
101      * Resize the managed region.
102      */

103     public void resize(long newSize) {
104     if (newSize > buf.length) {
105         setCapacity(newSize + (newSize >> 1));
106     }
107     this.size = (int)newSize;
108     }
109
110     /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer
111      * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed
112      * area.
113      */

114     public void write(long pos, byte[] b, int offset, int len) {
115         //#ifdef DEBUG
116
if (trace) {
117             Debug.println("BAR:write(" + pos + ", " +
118                           Util.hexBytes(b,offset,len) + ")");
119         }
120         //#endif
121
System.arraycopy(b, offset, buf, (int)pos, len);
122     }
123
124     /**
125      * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region
126      * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>.
127      */

128     public void read(long pos, byte[] b, int offset, int len) {
129         //#ifdef DEBUG
130
if (trace) {
131             Debug.println("BAR:read(" + pos + ", " +
132                           Util.hexBytes(b,offset,len) + ")");
133         }
134         //#endif
135
System.arraycopy(buf, (int)pos, b, offset, len);
136     }
137
138     //#ifdef DEBUG
139
static final boolean trace = false;
140     //#endif
141

142     public final void writeByte(int pos, int val) {
143         buf[pos] = (byte)(val & 0xff);
144     }
145
146     public final int readByte(int pos) {
147         return buf[pos] & 0xff;
148     }
149
150     /**
151      * Finalization...
152      */

153     public void close() {
154     }
155
156     public void flush() {
157     }
158 }
159
Popular Tags