KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > header > FileHeader


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

32 public abstract class FileHeader {
33     
34     private static final FileHeader[] AVAILABLE_FILE_HEADERS = new FileHeader[]{
35         new FileHeader0(),
36         new FileHeader1()
37     };
38     
39     private static int readerLength(){
40         int length = AVAILABLE_FILE_HEADERS[0].length();
41         for (int i = 1; i < AVAILABLE_FILE_HEADERS.length; i++) {
42             length = YInt.max(length, AVAILABLE_FILE_HEADERS[i].length());
43         }
44         return length;
45     }
46
47     public static FileHeader readFixedPart(YapFile file) throws IOException{
48         YapReader reader = prepareFileHeaderReader(file);
49         FileHeader header = detectFileHeader(file, reader);
50         if(header == null){
51             Exceptions4.throwRuntimeException(17);
52         } else {
53             header.readFixedPart(file, reader);
54         }
55         return header;
56     }
57
58     private static YapReader prepareFileHeaderReader(YapFile file) {
59         YapReader reader = new YapReader(readerLength());
60         reader.read(file, 0, 0);
61         return reader;
62     }
63
64     private static FileHeader detectFileHeader(YapFile file, YapReader reader) {
65         for (int i = 0; i < AVAILABLE_FILE_HEADERS.length; i++) {
66             reader.seek(0);
67             FileHeader result = AVAILABLE_FILE_HEADERS[i].newOnSignatureMatch(file, reader);
68             if(result != null) {
69                 return result;
70             }
71         }
72         return null;
73     }
74
75     public abstract void close() throws IOException;
76
77     public abstract void initNew(YapFile file) throws IOException;
78
79     public abstract Transaction interruptedTransaction();
80
81     public abstract int length();
82     
83     protected abstract FileHeader newOnSignatureMatch(YapFile file, YapReader reader);
84     
85     protected long timeToWrite(long time, boolean shuttingDown) {
86         return shuttingDown ? 0 : time;
87     }
88
89     protected abstract void readFixedPart(YapFile file, YapReader reader) throws IOException;
90
91     public abstract void readVariablePart(YapFile file);
92     
93     protected boolean signatureMatches(YapReader reader, byte[] signature, byte version){
94         for (int i = 0; i < signature.length; i++) {
95             if(reader.readByte() != signature[i]){
96                 return false;
97             }
98         }
99         return reader.readByte() == version;
100     }
101     
102     // TODO: freespaceID should not be passed here, it should be taken from SystemData
103
public abstract void writeFixedPart(
104         YapFile file, boolean shuttingDown, YapWriter writer, int blockSize, int freespaceID);
105     
106     public abstract void writeTransactionPointer(Transaction systemTransaction, int transactionAddress);
107
108     protected void writeTransactionPointer(Transaction systemTransaction, int transactionAddress, final int address, final int offset) {
109         YapWriter bytes = new YapWriter(systemTransaction, address, YapConst.INT_LENGTH * 2);
110         bytes.moveForward(offset);
111         bytes.writeInt(transactionAddress);
112         bytes.writeInt(transactionAddress);
113         if (Debug.xbytes && Deploy.overwrite) {
114             bytes.setID(YapConst.IGNORE_ID);
115         }
116         bytes.write();
117     }
118     
119     public abstract void writeVariablePart(YapFile file, int part);
120
121     protected void readClassCollectionAndFreeSpace(YapFile file, YapReader reader) {
122         SystemData systemData = file.systemData();
123         systemData.classCollectionID(reader.readInt());
124         systemData.freespaceID(reader.readInt());
125     }
126
127 }
128
Popular Tags