KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapMemoryFile


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;
22
23 import java.io.*;
24
25 import com.db4o.config.*;
26 import com.db4o.ext.*;
27 import com.db4o.inside.*;
28
29 /**
30  * @exclude
31  */

32 public class YapMemoryFile extends YapFile {
33     
34     private boolean i_closed = false;
35     final MemoryFile i_memoryFile;
36     private int i_length = 0;
37
38     protected YapMemoryFile(Configuration config,YapStream a_parent, MemoryFile memoryFile) {
39         super(config,a_parent);
40         this.i_memoryFile = memoryFile;
41         try {
42             open();
43         } catch (Exception JavaDoc e) {
44             Exceptions4.throwRuntimeException(22, e);
45         }
46         initialize3();
47     }
48
49     YapMemoryFile(Configuration config,MemoryFile memoryFile) {
50         this(config, null, memoryFile);
51     }
52     
53     public void backup(String JavaDoc path)throws IOException{
54         Exceptions4.throwRuntimeException(60);
55     }
56     
57     public void blockSize(int size){
58         // do nothing, blocksize is always 1
59
}
60
61     void checkDemoHop() {
62         // do nothing
63
}
64
65     protected boolean close2() {
66         if (Deploy.debug) {
67             write(true);
68         } else {
69             try {
70                 write(true);
71             } catch (Throwable JavaDoc t) {
72                 fatalException(t);
73             }
74         }
75         super.close2();
76         if (i_closed == false) {
77             byte[] temp = new byte[i_length];
78             System.arraycopy(i_memoryFile.getBytes(), 0, temp, 0, i_length);
79             i_memoryFile.setBytes(temp);
80         }
81         i_closed = true;
82         return true;
83     }
84
85     public void copy(int oldAddress, int oldAddressOffset, int newAddress, int newAddressOffset, int length) {
86         byte[] bytes = memoryFileBytes(newAddress + newAddressOffset + length);
87         System.arraycopy(bytes, oldAddress + oldAddressOffset, bytes, newAddress + newAddressOffset, length);
88     }
89
90     void emergencyClose() {
91         super.emergencyClose();
92         i_closed = true;
93     }
94
95     public long fileLength() {
96         return i_length;
97     }
98
99     String JavaDoc fileName() {
100         return "Memory File";
101     }
102
103     protected boolean hasShutDownHook() {
104         return false;
105     }
106
107     public final boolean needsLockFileThread() {
108         return false;
109     }
110
111     private void open() throws IOException {
112         byte[] bytes = i_memoryFile.getBytes();
113         if (bytes == null || bytes.length == 0) {
114             i_memoryFile.setBytes(new byte[i_memoryFile.getInitialSize()]);
115             configureNewFile();
116             write(false);
117             writeHeader(false);
118         } else {
119             i_length = bytes.length;
120             readThis();
121         }
122     }
123
124     public void readBytes(byte[] a_bytes, int a_address, int a_length) {
125         try {
126             System.arraycopy(i_memoryFile.getBytes(), a_address, a_bytes, 0, a_length);
127         } catch (Exception JavaDoc e) {
128             Exceptions4.throwRuntimeException(13, e);
129         }
130     }
131     
132     public void readBytes(byte[] bytes, int address, int addressOffset, int length){
133         readBytes(bytes, address + addressOffset, length);
134     }
135
136     public void syncFiles() {
137     }
138
139     public boolean writeAccessTime(int address, int offset, long time) {
140         return true;
141     }
142
143     public void writeBytes(YapReader a_bytes, int address, int addressOffset) {
144         int fullAddress = address + addressOffset;
145         int length = a_bytes.getLength();
146         System.arraycopy(a_bytes._buffer, 0, memoryFileBytes(fullAddress + length), fullAddress , length);
147     }
148
149     private byte[] memoryFileBytes(int a_lastByte) {
150         byte[] bytes = i_memoryFile.getBytes();
151         if (a_lastByte > i_length) {
152             if (a_lastByte > bytes.length) {
153                 int increase = a_lastByte - bytes.length;
154                 if (increase < i_memoryFile.getIncrementSizeBy()) {
155                     increase = i_memoryFile.getIncrementSizeBy();
156                 }
157                 byte[] temp = new byte[bytes.length + increase];
158                 System.arraycopy(bytes, 0, temp, 0, bytes.length);
159                 i_memoryFile.setBytes(temp);
160                 bytes = temp;
161             }
162             i_length = a_lastByte;
163         }
164         return bytes;
165     }
166     
167     public void debugWriteXBytes(int a_address, int a_length) {
168         // do nothing
169
}
170
171 }
172
Popular Tags