KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > persist > ScaledRAFileHybrid


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.persist;
33
34 import java.io.IOException JavaDoc;
35
36 import org.hsqldb.Database;
37
38 /**
39  * Mixe NIO / non-NIO version of ScaledRAFile.
40  * This class is used only for storing a CACHED
41  * TABLE .data file and cannot be used for TEXT TABLE source files.
42  *
43  * Due to various issues with java.nio classes, this class will use a mapped
44  * channel of fixed size. After reaching this size, the file and channel are
45  * closed and a new one opened, up to the maximum size.
46  *
47  * @author fredt@users
48  * @version 1.8.0.5
49  * @since 1.7.2
50  */

51 public class ScaledRAFileHybrid implements ScaledRAInterface {
52
53     final Database database;
54     final String JavaDoc fileName;
55     final boolean isReadOnly;
56     final boolean wasNio;
57     long maxLength;
58     ScaledRAInterface store;
59
60     public ScaledRAFileHybrid(Database database, String JavaDoc name,
61                               boolean readOnly) throws IOException JavaDoc {
62
63         this.database = database;
64         this.fileName = name;
65         this.isReadOnly = readOnly;
66
67         newStore(0);
68
69         this.wasNio = store.wasNio();
70     }
71
72     public long length() throws IOException JavaDoc {
73         return store.length();
74     }
75
76     public void seek(long position) throws IOException JavaDoc {
77         checkSeek(position);
78         store.seek(position);
79     }
80
81     public long getFilePointer() throws IOException JavaDoc {
82         return store.getFilePointer();
83     }
84
85     public int read() throws IOException JavaDoc {
86
87         checkLength(1);
88
89         return store.read();
90     }
91
92     public void read(byte[] b, int offset, int length) throws IOException JavaDoc {
93         checkLength(length);
94         store.read(b, offset, length);
95     }
96
97     public void write(byte[] b, int offset, int length) throws IOException JavaDoc {
98         checkLength(length);
99         store.write(b, offset, length);
100     }
101
102     public int readInt() throws IOException JavaDoc {
103
104         checkLength(4);
105
106         return store.readInt();
107     }
108
109     public void writeInt(int i) throws IOException JavaDoc {
110         checkLength(4);
111         store.writeInt(i);
112     }
113
114     public long readLong() throws IOException JavaDoc {
115
116         checkLength(8);
117
118         return store.readLong();
119     }
120
121     public void writeLong(long i) throws IOException JavaDoc {
122         checkLength(8);
123         store.writeLong(i);
124     }
125
126     public void close() throws IOException JavaDoc {
127         store.close();
128     }
129
130     public boolean isReadOnly() {
131         return store.isReadOnly();
132     }
133
134     public boolean wasNio() {
135         return wasNio;
136     }
137
138     public boolean canAccess(int length) {
139         return true;
140     }
141
142     public boolean canSeek(long position) {
143         return true;
144     }
145
146     public Database getDatabase() {
147         return null;
148     }
149
150     private void checkLength(int length) throws IOException JavaDoc {
151
152         if (store.canAccess(length)) {
153             return;
154         }
155
156         newStore(store.getFilePointer() + length);
157     }
158
159     private void checkSeek(long position) throws IOException JavaDoc {
160
161         if (store.canSeek(position)) {
162             return;
163         }
164
165         newStore(position);
166     }
167
168     void newStore(long requiredPosition) throws IOException JavaDoc {
169
170         long currentPosition = 0;
171
172         if (store != null) {
173             currentPosition = store.getFilePointer();
174
175             store.close();
176         }
177
178         if (requiredPosition <= ScaledRAFile.MAX_NIO_LENGTH) {
179             try {
180                 store = new ScaledRAFileNIO(database, fileName, isReadOnly,
181                                             (int) requiredPosition);
182
183                 store.seek(currentPosition);
184
185                 return;
186             } catch (Throwable JavaDoc e) {}
187         }
188
189         store = new ScaledRAFile(database, fileName, isReadOnly);
190
191         store.seek(currentPosition);
192     }
193 }
194
Popular Tags