KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
42
43 import com.quadcap.util.Debug;
44
45 /**
46  * A <code>RandomAccess</code> implementation using a byte array.
47  *
48  * @author Stan Bailes
49  */

50 public class BufferedRandomAccess extends RandomAccess {
51     static final int BUFSIZE = 16384;
52
53     class Buffer {
54         byte[] buf = new byte[BUFSIZE];
55         long adr = -1;
56         boolean dty = false;
57
58         //#ifdef DEBUG
59
public String JavaDoc toString() {
60             return "Buffer[" + adr + "]" + (dty?"*":"");
61         }
62         //#endif
63
}
64     Buffer b1 = new Buffer();
65     Buffer b2 = new Buffer();
66     Buffer b = null;
67
68     RandomAccess ra;
69     long size;
70
71     public BufferedRandomAccess(RandomAccess ra) {
72     this.ra = ra;
73         size = ra.size();
74     }
75
76     /**
77      * Return the size of the managed region.
78      */

79     public long size() {
80     return size;
81     }
82
83     /**
84      * Resize the managed region.
85      */

86     public void resize(long newSize) throws IOException JavaDoc {
87         size = newSize;
88         ra.resize(newSize);
89     }
90
91     /* Write <tt>len</tt> bytes from position <tt>offset</tt> in buffer
92      * <tt>buf</tt> to position <tt>pos</tt> bytes into the managed
93      * area.
94      */

95     public void write(long pos, byte[] buf, int offset, int len)
96         throws IOException JavaDoc
97     {
98         do {
99             long pb = pos & ~(BUFSIZE-1);
100             long pe = pb + BUFSIZE-1;
101             if (pe >= size) pe = size-1;
102             int xlen = (int)(pe - pos + 1);
103             if (xlen > len) xlen = len;
104             if (xlen > 0) {
105                 getBuffer(pb);
106                 System.arraycopy(buf, offset, b.buf, (int)(pos-pb), xlen);
107                 b.dty = true;
108                 offset += xlen;
109                 pos += xlen;
110                 len -= xlen;
111             } else {
112                 len = 0;
113             }
114         } while (len > 0);
115         
116     }
117
118     /**
119      * Read <tt>len</tt> bytes from location <tt>pos</tt> of the region
120      * into the buffer <tt>buf</tt>, starting at <tt>offset</tt>.
121      */

122     public void read(long pos, byte[] buf, int offset, int len)
123         throws IOException JavaDoc
124     {
125         do {
126             long pb = pos & ~(BUFSIZE-1);
127             long pe = pb + BUFSIZE-1;
128             if (pe >= size) pe = size-1;
129             int xlen = (int)(pe - pos + 1);
130             if (xlen > len) xlen = len;
131             if (xlen > 0) {
132                 getBuffer(pb);
133                 System.arraycopy(b.buf, (int)(pos-pb), buf, offset, xlen);
134                 offset += xlen;
135                 pos += xlen;
136                 len -= xlen;
137             } else {
138                 len = 0;
139             }
140         } while (len > 0);
141     }
142
143     final void getBuffer(long addr) throws IOException JavaDoc {
144         if (addr == b1.adr) {
145             b = b1;
146         } else if (addr == b2.adr) {
147             b = b2;
148         } else {
149             b = (b == b1) ? b2 : b1;
150             if (b.dty) flush(b);
151             b.adr = addr;
152             int xlen = (int)(ra.size() - addr);
153             if (xlen > BUFSIZE) xlen = BUFSIZE;
154             ra.read(addr, b.buf, 0, xlen);
155         }
156     }
157     
158     /**
159      * Finalization...
160      */

161     public void close() throws IOException JavaDoc {
162         flush();
163         ra.close();
164     }
165
166     final void flushBuffers() throws IOException JavaDoc {
167         flush(b1);
168         flush(b2);
169     }
170     
171     public void flush() throws IOException JavaDoc {
172         flushBuffers();
173         ra.flush();
174     }
175
176     final void flush(Buffer b) throws IOException JavaDoc {
177         if (b.dty) {
178             int xlen = (int)(ra.size() - b.adr);
179             if (xlen > BUFSIZE) xlen = BUFSIZE;
180             ra.write(b.adr, b.buf, 0, xlen);
181             b.dty = false;
182         }
183     }
184 }
185
Popular Tags