KickJava   Java API By Example, From Geeks To Geeks.

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


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 class FieldMarshaller0 implements FieldMarshaller {
29
30     public int marshalledLength(YapStream stream, YapField field) {
31         int len = stream.stringIO().shortLength(field.getName());
32         if(field.needsArrayAndPrimitiveInfo()){
33             len += 1;
34         }
35         if(field.needsHandlerId()){
36             len += YapConst.ID_LENGTH;
37         }
38         return len;
39     }
40     
41     public RawFieldSpec readSpec(YapStream stream, YapReader reader) {
42         
43         String JavaDoc name = null;
44         
45         try {
46             name = StringMarshaller.readShort(stream, reader);
47         } catch (CorruptionException ce) {
48             return null;
49         }
50         
51         if (name.indexOf(YapConst.VIRTUAL_FIELD_PREFIX) == 0) {
52             YapFieldVirtual[] virtuals = stream.i_handlers.i_virtualFields;
53             for (int i = 0; i < virtuals.length; i++) {
54                 if (name.equals(virtuals[i].getName())) {
55                     return new RawFieldSpec(name);
56                 }
57             }
58         }
59         int handlerID = reader.readInt();
60         byte attribs=reader.readByte();
61         
62         return new RawFieldSpec(name,handlerID,attribs);
63     }
64
65     
66     public final YapField read(YapStream stream, YapField field, YapReader reader) {
67         RawFieldSpec spec=readSpec(stream, reader);
68         return fromSpec(spec, stream, field);
69     }
70     
71     protected YapField fromSpec(RawFieldSpec spec,YapStream stream, YapField field) {
72         if(spec==null) {
73             return field;
74         }
75         String JavaDoc name=spec.name();
76         if (spec.isVirtual()) {
77             YapFieldVirtual[] virtuals = stream.i_handlers.i_virtualFields;
78             for (int i = 0; i < virtuals.length; i++) {
79                 if (name.equals(virtuals[i].getName())) {
80                     return virtuals[i];
81                 }
82             }
83         }
84         
85         field.init(field.getParentYapClass(), name);
86         field.init(spec.handlerID(), spec.isPrimitive(), spec.isArray(), spec.isNArray());
87         field.loadHandler(stream);
88         field.alive();
89         
90         return field;
91     }
92
93
94     public void write(Transaction trans, YapClass clazz, YapField field, YapReader writer) {
95         
96         field.alive();
97         
98         writer.writeShortString(trans, field.getName());
99         if(field.isVirtual()){
100             return;
101         }
102         
103         TypeHandler4 handler = field.getHandler();
104         
105         if (handler instanceof YapClass) {
106             
107             // TODO: ensure there is a test case, to make this happen
108
if (handler.getID() == 0) {
109                 trans.stream().needsUpdate(clazz);
110             }
111         }
112         int handlerID = 0;
113         try {
114             // The handler can be null and it can fail to
115
// deliver the ID.
116

117             // In this case the field is dead.
118

119             handlerID = handler.getID();
120         } catch (Exception JavaDoc e) {
121             if (Debug.atHome) {
122                 e.printStackTrace();
123             }
124         }
125         if (handlerID == 0) {
126             handlerID = field.getHandlerID();
127         }
128         writer.writeInt(handlerID);
129         YapBit yb = new YapBit(0);
130         yb.set(handler instanceof YapArrayN); // keep the order
131
yb.set(handler instanceof YapArray);
132         yb.set(field.isPrimitive());
133         writer.append(yb.getByte());
134     }
135
136
137     public void defrag(YapClass yapClass, YapField yapField, YapStringIO sio,ReaderPair readers) throws CorruptionException {
138         readers.readShortString(sio);
139         if (yapField.isVirtual()) {
140             return;
141         }
142         // handler ID
143
readers.copyID();
144         // skip primitive/array/narray attributes
145
readers.incrementOffset(1);
146     }
147 }
148
Popular Tags