1 21 package com.db4o.inside.marshall; 22 23 import com.db4o.*; 24 25 26 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 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 |