KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > marshall > ClassMarshaller


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.inside.marshall;
22
23 import com.db4o.*;
24
25 /**
26  * @exclude
27  */

28 public abstract class ClassMarshaller {
29     
30     public MarshallerFamily _family;
31     
32     public RawClassSpec readSpec(Transaction trans,YapReader reader) {
33         byte[] nameBytes=readName(trans, reader);
34         String JavaDoc className=trans.stream().stringIO().read(nameBytes);
35         readMetaClassID(reader); // skip
36
int ancestorID=reader.readInt();
37         reader.incrementOffset(YapConst.INT_LENGTH); // index ID
38
int numFields=reader.readInt();
39         return new RawClassSpec(className,ancestorID,numFields);
40     }
41
42     public void write(Transaction trans, YapClass clazz, YapReader writer) {
43         
44         writer.writeShortString(trans, clazz.nameToWrite());
45         
46         int intFormerlyKnownAsMetaClassID = 0;
47         writer.writeInt(intFormerlyKnownAsMetaClassID);
48         
49         writer.writeIDOf(trans, clazz.i_ancestor);
50         
51         writeIndex(trans, clazz, writer);
52         
53         YapField[] fields = clazz.i_fields;
54         
55         if (fields == null) {
56             writer.writeInt(0);
57             return;
58         }
59         writer.writeInt(fields.length);
60         for (int i = 0; i < fields.length; i++) {
61             _family._field.write(trans, clazz, fields[i], writer);
62         }
63     }
64
65     protected void writeIndex(Transaction trans, YapClass clazz, YapReader writer) {
66         int indexID = clazz.index().write(trans);
67         writer.writeInt(indexIDForWriting(indexID));
68     }
69     
70     protected abstract int indexIDForWriting(int indexID);
71
72     public byte[] readName(Transaction trans, YapReader reader) {
73         byte[] name = readName(trans.stream().stringIO(), reader);
74         return name;
75     }
76     
77     public int readMetaClassID(YapReader reader) {
78         return reader.readInt();
79     }
80     
81     private byte[] readName(YapStringIO sio, YapReader reader) {
82         if (Deploy.debug) {
83             reader.readBegin(YapConst.YAPCLASS);
84         }
85         int len = reader.readInt();
86         len = len * sio.bytesPerChar();
87         byte[] nameBytes = new byte[len];
88         System.arraycopy(reader._buffer, reader._offset, nameBytes, 0, len);
89         if(Deploy.csharp){
90             nameBytes = Platform4.updateClassName(nameBytes);
91         }
92         reader.incrementOffset(len);
93         return nameBytes;
94     }
95
96     public void read(YapStream stream, YapClass clazz, YapReader reader) {
97         clazz.i_ancestor = stream.getYapClass(reader.readInt());
98         
99         if(clazz.i_dontCallConstructors){
100             // The logic further down checks the ancestor YapClass, whether
101
// or not it is allowed, not to call constructors. The ancestor
102
// YapClass may possibly have not been loaded yet.
103
clazz.createConstructor(stream, clazz.classReflector(), clazz.getName(), true);
104         }
105         
106         clazz.checkDb4oType();
107         
108         readIndex(stream, clazz, reader);
109         
110         clazz.i_fields = createFields(clazz, reader.readInt());
111         readFields(stream, reader, clazz.i_fields);
112     }
113
114     protected abstract void readIndex(YapStream stream, YapClass clazz, YapReader reader) ;
115
116     private YapField[] createFields(YapClass clazz, final int fieldCount) {
117         final YapField[] fields = new YapField[fieldCount];
118         for (int i = 0; i < fields.length; i++) {
119             fields[i] = new YapField(clazz);
120             fields[i].setArrayPosition(i);
121         }
122         return fields;
123     }
124
125     private void readFields(YapStream stream, YapReader reader, final YapField[] fields) {
126         for (int i = 0; i < fields.length; i++) {
127             fields[i] = _family._field.read(stream, fields[i], reader);
128         }
129     }
130
131     public int marshalledLength(YapStream stream, YapClass clazz) {
132         int len = stream.stringIO().shortLength(clazz.nameToWrite())
133                 + YapConst.OBJECT_LENGTH
134                 + (YapConst.INT_LENGTH * 2)
135                 + (YapConst.ID_LENGTH);
136
137         len += clazz.index().ownLength();
138         
139         if (clazz.i_fields != null) {
140             for (int i = 0; i < clazz.i_fields.length; i++) {
141                 len += _family._field.marshalledLength(stream, clazz.i_fields[i]);
142             }
143         }
144         return len;
145     }
146
147     public void defrag(YapClass yapClass,YapStringIO sio,ReaderPair readers, int classIndexID) throws CorruptionException {
148         readName(sio, readers.source());
149         readName(sio, readers.target());
150         
151         int metaClassID=0;
152         readers.writeInt(metaClassID);
153
154         // ancestor ID
155
readers.copyID();
156
157         readers.writeInt(indexIDForWriting(classIndexID));
158         
159         // field length
160
readers.incrementIntSize();
161         
162         YapField[] fields=yapClass.i_fields;
163         for(int fieldIdx=0;fieldIdx<fields.length;fieldIdx++) {
164             _family._field.defrag(yapClass,fields[fieldIdx],sio,readers);
165         }
166     }
167 }
168
Popular Tags