KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > ext > MemoryFile


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.ext;
22
23 /**
24  * carries in-memory data for db4o in-memory operation.
25  * <br><br>In-memory ObjectContainers are useful for maximum performance
26  * on small databases, for swapping objects or for storing db4o format data
27  * to other media or other databases.<br><br>Be aware of the danger of running
28  * into OutOfMemory problems or complete loss of all data, in case of hardware
29  * or JVM failures.
30  * <br><br>
31  * @see ExtDb4o#openMemoryFile
32  */

33 public class MemoryFile {
34
35     private byte[] i_bytes;
36     private final int INITIAL_SIZE_AND_INC = 10000;
37     private int i_initialSize = INITIAL_SIZE_AND_INC;
38     private int i_incrementSizeBy = INITIAL_SIZE_AND_INC;
39     
40
41     /**
42      * constructs a new MemoryFile without any data.
43      * @see ExtDb4o#openMemoryFile
44      */

45     public MemoryFile() {
46     }
47
48     /**
49      * constructs a MemoryFile to use the byte data from a previous
50      * MemoryFile.
51      * @param bytes the raw byte data.
52      * @see ExtDb4o#openMemoryFile
53      */

54     public MemoryFile(byte[] bytes) {
55         i_bytes = bytes;
56     }
57     
58     /**
59      * returns the raw byte data.
60      * <br><br>Use this method to get the byte data from the MemoryFile
61      * to store it to other media or databases, for backup purposes or
62      * to create other MemoryFile sessions.
63      * <br><br>The byte data from a MemoryFile should only be used
64      * after it is closed.<br><br>
65      * @return bytes the raw byte data.
66      */

67     public byte[] getBytes(){
68         if(i_bytes == null){
69             return new byte[0];
70         }
71         return i_bytes;
72     }
73     
74     /**
75      * returns the size the MemoryFile is to be enlarged, if it grows beyond
76      * the current size.
77      * @return size in bytes
78      */

79     public int getIncrementSizeBy(){
80         return i_incrementSizeBy;
81     }
82     
83     /**
84      * returns the initial size of the MemoryFile.
85      * @return size in bytes
86      */

87     public int getInitialSize(){
88         return i_initialSize;
89     }
90     
91     /**
92      * sets the raw byte data.
93      * <br><br><b>Caution!</b><br>Calling this method during a running
94      * Memory File session may produce unpreditable results.
95      * @param bytes the raw byte data.
96      */

97     public void setBytes(byte[] bytes){
98         i_bytes = bytes;
99     }
100
101     /**
102      * configures the size the MemoryFile is to be enlarged by, if it grows
103      * beyond the current size.
104      * <br><br>Call this method before passing the MemoryFile to
105      * {@link com.db4o.ext.ExtDb4o#openMemoryFile ExtDb4o#openMemoryFile(MemoryFile)}.
106      * <br><br>
107      * This parameter can be modified to tune the maximum performance of
108      * a MemoryFile for a specific usecase. To produce the best results,
109      * test the speed of your application with real data.<br><br>
110      * @param byteCount the desired size in bytes
111      */

112     public void setIncrementSizeBy(int byteCount){
113         i_incrementSizeBy = byteCount;
114     }
115     
116     /**
117      * configures the initial size of the MemoryFile.
118      * <br><br>Call this method before passing the MemoryFile to
119      * {@link com.db4o.ext.ExtDb4o#openMemoryFile ExtDb4o#openMemoryFile(MemoryFile)}.
120      * <br><br>
121      * This parameter can be modified to tune the maximum performance of
122      * a MemoryFile for a specific usecase. To produce the best results,
123      * test speed and memory consumption of your application with
124      * real data.<br><br>
125      * @param byteCount the desired size in bytes
126      */

127     public void setInitialSize(int byteCount){
128         i_initialSize = byteCount;
129     }
130 }
Popular Tags