KickJava   Java API By Example, From Geeks To Geeks.

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


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.foundation.*;
26
27 /**
28  * IoAdapter for in-memory operation.
29  * <br><br>Configure db4o to operate with this in-memory IoAdapter with
30  * <code>MemoryIoAdapter memoryIoAdapter = new MemoryIoAdapter();<br>
31  * Db4o.configure().io(memoryIoAdapter);</code><br>
32  * <br><br>Use the normal #openFile() and #openServer() commands to
33  * open ObjectContainers and ObjectServers. The names specified as
34  * file names will be used to identify the
35  * <code>byte[]</code> content of the in-memory files in
36  * the _memoryFiles Hashtable in the adapter. After working with an
37  * in-memory ObjectContainer/ObjectServer the <code>byte[]</code> content
38  * is available in the MemoryIoAdapter by using
39  * {@link #get(String)}. To add old existing database
40  * <code>byte[]</code> content to a MemoryIoAdapter use
41  * {@link #put(String, byte[])}. To reduce memory consumption of memory
42  * file names that will no longer be used call {@link #put(String, byte[])}
43  * and pass an empty byte array.
44  */

45 public class MemoryIoAdapter extends IoAdapter{
46
47     private byte[] _bytes;
48     private int _length;
49     private int _seekPos;
50     
51     private Hashtable4 _memoryFiles;
52     private int _growBy;
53     
54     public MemoryIoAdapter(){
55         _memoryFiles = new Hashtable4();
56         _growBy = 10000;
57     }
58     
59     private MemoryIoAdapter(MemoryIoAdapter adapter, byte[] bytes){
60         _bytes = bytes;
61         _length = bytes.length;
62         _growBy = adapter._growBy;
63     }
64     
65     private MemoryIoAdapter(MemoryIoAdapter adapter, int initialLength){
66         this(adapter, new byte[initialLength]);
67     }
68     
69     /**
70      * creates an in-memory database with the passed content bytes and
71      * adds it to the adapter for the specified name.
72      * @param name the name to be use for #openFile() or #openServer() calls
73      * @param bytes the database content
74      */

75     public void put(String JavaDoc name, byte[] bytes){
76         if(bytes == null){
77             bytes = new byte[0];
78         }
79         _memoryFiles.put(name, new MemoryIoAdapter(this,bytes));
80     }
81     
82     /**
83      * returns the content bytes for a database with the given name.
84      * @param name the name to be use for #openFile() or #openServer() calls
85      * @return the content bytes
86      */

87     public byte[] get(String JavaDoc name){
88         MemoryIoAdapter mia = (MemoryIoAdapter)_memoryFiles.get(name);
89         if(mia == null){
90             return null;
91         }
92         return mia._bytes;
93     }
94     
95     /**
96      * configures the length a memory file should grow, if no more
97      * free slots are found within.
98      * <br><br>Specify a large value (100,000 or more) for best performance.
99      * Specify a small value (100) for the smallest memory consumption. The
100      * default setting is 10,000.
101      * @param length the length in bytes
102      */

103     public void growBy(int length){
104         if(length < 1){
105             length = 1;
106         }
107         _growBy = length;
108     }
109     
110     /**
111      * for internal processing only.
112      */

113     public void close() throws IOException {
114         // do nothing
115
}
116     
117     public void delete(String JavaDoc path) {
118         _memoryFiles.remove(path);
119     }
120     
121     /**
122      * for internal processing only.
123      */

124     public boolean exists(String JavaDoc path){
125         MemoryIoAdapter mia = (MemoryIoAdapter)_memoryFiles.get(path);
126         if(mia == null){
127             return false;
128         }
129         return mia._length > 0;
130     }
131
132
133     /**
134      * for internal processing only.
135      */

136     public long getLength() throws IOException {
137         return _length;
138     }
139
140     /**
141      * for internal processing only.
142      */

143     public IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength) throws IOException {
144         MemoryIoAdapter mia = (MemoryIoAdapter)_memoryFiles.get(path);
145         if(mia == null){
146             mia = new MemoryIoAdapter(this, (int)initialLength);
147             _memoryFiles.put( path, mia);
148         }
149         return mia;
150     }
151
152     /**
153      * for internal processing only.
154      */

155     public int read(byte[] bytes, int length) throws IOException {
156         System.arraycopy(_bytes, _seekPos, bytes, 0, length);
157         _seekPos += length;
158         return length;
159     }
160
161     /**
162      * for internal processing only.
163      */

164     public void seek(long pos) throws IOException {
165         _seekPos = (int)pos;
166     }
167
168     /**
169      * for internal processing only.
170      */

171     public void sync() throws IOException {
172     }
173     
174     /**
175      * for internal processing only.
176      */

177     public void write(byte[] buffer, int length) throws IOException {
178         if(_seekPos + length > _bytes.length){
179             int growBy = _growBy;
180             if(_seekPos + length > growBy){
181                 growBy = _seekPos + length;
182             }
183             byte[] temp = new byte[_bytes.length + growBy];
184             System.arraycopy(_bytes, 0, temp, 0, _length);
185             _bytes = temp;
186         }
187         System.arraycopy(buffer, 0, _bytes, _seekPos, length);
188         _seekPos += length;
189         if(_seekPos > _length){
190             _length = _seekPos;
191         }
192     }
193
194 }
195
Popular Tags