KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > io > IoAdapter


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.io;
22
23 import java.io.*;
24
25 import com.db4o.*;
26
27 /**
28  * Base class for database file adapters, both for file and memory
29  * databases.
30  */

31 public abstract class IoAdapter {
32     
33     private static final int COPY_SIZE = 4096;
34     
35     private int _blockSize;
36     
37     /**
38      * converts address and address offset to an absolute address
39      */

40     protected final long regularAddress(int blockAddress, int blockAddressOffset){
41         if (0 == _blockSize) {
42             throw new IllegalStateException JavaDoc();
43         }
44         return (long)blockAddress * _blockSize + blockAddressOffset;
45     }
46     
47     /**
48      * copies a block within a file in block mode
49      */

50     public void blockCopy(int oldAddress, int oldAddressOffset, int newAddress, int newAddressOffset, int length) throws IOException{
51         copy(regularAddress(oldAddress, oldAddressOffset), regularAddress(newAddress, newAddressOffset), length);
52     }
53     
54     /**
55      * sets the read/write pointer in the file using block mode
56      */

57     public void blockSeek(int address) throws IOException {
58         blockSeek(address,0);
59     }
60
61     /**
62      * sets the read/write pointer in the file using block mode
63      */

64     public void blockSeek(int address, int offset)
65             throws IOException {
66         seek(regularAddress(address,offset));
67     }
68
69     /**
70      * outside call to set the block size of this adapter
71      */

72     public void blockSize(int blockSize) {
73         if (blockSize < 1) {
74             throw new IllegalArgumentException JavaDoc();
75         }
76         _blockSize=blockSize;
77     }
78
79     /**
80      * implement to close the adapter
81      */

82     public abstract void close() throws IOException;
83
84     /**
85      * copies a block within a file in absolute mode
86      */

87     public void copy(long oldAddress, long newAddress, int length) throws IOException{
88         
89         if(DTrace.enabled){
90             DTrace.IO_COPY.logLength(newAddress, length);
91         }
92         
93         if(length > COPY_SIZE){
94             byte[] buffer = new byte[COPY_SIZE];
95             int pos = 0;
96             while(pos + COPY_SIZE < length){
97                 copy(buffer, oldAddress + pos, newAddress + pos);
98                 pos+= COPY_SIZE;
99             }
100             oldAddress += pos;
101             newAddress += pos;
102             length -= pos;
103         }
104         
105         copy(new byte[length], oldAddress, newAddress);
106     }
107     
108     
109     private void copy(byte[] buffer, long oldAddress, long newAddress) throws IOException {
110         seek(oldAddress);
111         read(buffer);
112         seek(newAddress);
113         write(buffer);
114     }
115     
116     /**
117      * deletes the given path from whatever 'file system' is addressed
118      */

119     public abstract void delete(String JavaDoc path);
120     
121     /**
122      * checks whether a file exists
123      */

124     public abstract boolean exists(String JavaDoc path);
125     
126     /**
127      * implement to return the absolute length of the file
128      */

129     public abstract long getLength() throws IOException;
130
131     /**
132      * implement to open the file
133      */

134     public abstract IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength) throws IOException;
135
136     /**
137      * reads a buffer at the seeked address
138      *
139      * @return the number of bytes read and returned
140      */

141     public int read(byte[] buffer) throws IOException {
142         return read(buffer,buffer.length);
143     }
144
145     /**
146      * implement to read a buffer at the seeked address
147      */

148     public abstract int read(byte[] bytes, int length) throws IOException;
149
150     /**
151      * implement to set the read/write pointer in the file, absolute mode
152      */

153     public abstract void seek(long pos) throws IOException;
154
155     /**
156      * implement to flush the file contents to storage
157      */

158     public abstract void sync() throws IOException;
159
160     /**
161      * writes a buffer to the seeked address
162      */

163     public void write(byte[] bytes) throws IOException {
164         write(bytes,bytes.length);
165     }
166
167     /**
168      * implement to write a buffer at the seeked address
169      */

170     public abstract void write(byte[] buffer, int length) throws IOException;
171     
172     /**
173      * returns the block size currently used
174      */

175     public int blockSize() {
176         return _blockSize;
177     }
178
179
180 }
Popular Tags