KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
27  * @exclude
28  */

29 public final class ObjectHeader {
30     
31     private final YapClass _yapClass;
32     
33     public final MarshallerFamily _marshallerFamily;
34     
35     public final ObjectHeaderAttributes _headerAttributes;
36     
37     public ObjectHeader(YapStream stream, YapReader reader){
38         this(stream,null,reader);
39     }
40
41     public ObjectHeader(YapClass yapClass, YapReader reader){
42         this(null,yapClass,reader);
43     }
44
45     public ObjectHeader(YapWriter writer){
46         this(writer.getStream(), writer);
47     }
48     
49     public ObjectHeader(YapStream stream, YapClass yc, YapReader reader){
50         if (Deploy.debug) {
51             reader.readBegin(YapConst.YAPOBJECT);
52         }
53         int classID = reader.readInt();
54         _marshallerFamily = readMarshallerFamily(reader, classID);
55         
56         classID=normalizeID(classID);
57
58         _yapClass=(yc!=null ? yc : stream.getYapClass(classID));
59
60         if (Deploy.debug) {
61             // This check has been added to cope with defragment in debug mode: SlotDefragment#setIdentity()
62
// will trigger calling this constructor with a source db yap class and a target db stream,
63
// thus _yapClass==null. There may be a better solution, since this call is just meant to
64
// skip the object header.
65
if(_yapClass!=null) {
66                 int ycID = _yapClass.getID();
67                 if (classID != ycID) {
68                     System.out.println("ObjectHeader::init YapClass does not match. Expected ID: " + ycID + " Read ID: " + classID);
69                 }
70             }
71         }
72         _headerAttributes = readAttributes(_marshallerFamily,reader);
73     }
74
75     public static ObjectHeader defrag(ReaderPair readers) {
76         YapReader source = readers.source();
77         YapReader target = readers.target();
78         ObjectHeader header=new ObjectHeader(readers.context().systemTrans().stream(),null,source);
79         int newID =readers.mapping().mappedID(header.yapClass().getID());
80         if (Deploy.debug) {
81             target.readBegin(YapConst.YAPOBJECT);
82         }
83         header._marshallerFamily._object.writeObjectClassID(target,newID);
84         header._marshallerFamily._object.skipMarshallerInfo(target);
85         readAttributes(header._marshallerFamily, target);
86         return header;
87     }
88             
89     public ObjectMarshaller objectMarshaller() {
90         return _marshallerFamily._object;
91     }
92
93     private MarshallerFamily readMarshallerFamily(YapReader reader, int classID) {
94         boolean marshallerAware=marshallerAware(classID);
95         byte marshallerVersion=0;
96         if(marshallerAware) {
97             marshallerVersion = reader.readByte();
98         }
99         MarshallerFamily marshallerFamily=MarshallerFamily.version(marshallerVersion);
100         return marshallerFamily;
101     }
102     
103     private static ObjectHeaderAttributes readAttributes(MarshallerFamily marshallerFamily,YapReader reader) {
104         return marshallerFamily._object.readHeaderAttributes(reader);
105     }
106
107     private boolean marshallerAware(int id) {
108         return id<0;
109     }
110     
111     private int normalizeID(int id) {
112         return (id<0 ? -id : id);
113     }
114
115     public YapClass yapClass() {
116         return _yapClass;
117     }
118 }
119
Popular Tags