KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > messages > MsgObject


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.cs.messages;
22
23 import com.db4o.*;
24
25
26 public class MsgObject extends MsgD {
27     
28     private static final int LENGTH_FOR_ALL = YapConst.ID_LENGTH + (YapConst.INT_LENGTH * 3);
29     private static final int LENGTH_FOR_FIRST = LENGTH_FOR_ALL;
30     
31     private int _id;
32     private int _address;
33     
34     MsgD getWriter(YapWriter bytes, int[] prependInts) {
35         int lengthNeeded = bytes.getLength() + LENGTH_FOR_FIRST;
36         if(prependInts != null){
37             lengthNeeded += (prependInts.length * YapConst.INT_LENGTH);
38         }
39         int embeddedCount = bytes.embeddedCount();
40         if(embeddedCount > 0){
41             lengthNeeded += (LENGTH_FOR_ALL * embeddedCount) + bytes.embeddedLength();
42         }
43         MsgD message = getWriterForLength(bytes.getTransaction(), lengthNeeded);
44         if(prependInts != null){
45             for (int i = 0; i < prependInts.length; i++) {
46                 message._payLoad.writeInt(prependInts[i]);
47             }
48         }
49         message._payLoad.writeInt(embeddedCount);
50         bytes.appendTo(message._payLoad, -1);
51         return message;
52     }
53
54     public MsgD getWriter(YapWriter bytes) {
55         return getWriter(bytes, null);
56     }
57     
58     public MsgD getWriter(YapClass a_yapClass, YapWriter bytes) {
59         if(a_yapClass == null){
60             return getWriter(bytes, new int[]{0});
61         }
62         return getWriter(bytes, new int[]{ a_yapClass.getID()});
63     }
64     
65     public MsgD getWriter(YapClass a_yapClass, int a_param, YapWriter bytes) {
66         return getWriter(bytes, new int[]{ a_yapClass.getID(), a_param});
67     }
68     
69     public final YapWriter unmarshall() {
70         return unmarshall(0);
71     }
72
73     public final YapWriter unmarshall(int addLengthBeforeFirst) {
74         _payLoad.setTransaction(transaction());
75         int embeddedCount = _payLoad.readInt();
76         int length = _payLoad.readInt();
77         if (length == 0) {
78             return null; // does this happen ?
79
}
80         _id = _payLoad.readInt();
81         _address = _payLoad.readInt();
82         if(embeddedCount == 0){
83             _payLoad.removeFirstBytes(LENGTH_FOR_FIRST + addLengthBeforeFirst);
84         }else{
85             _payLoad._offset += length;
86             YapWriter[] embedded = new YapWriter[embeddedCount + 1];
87             embedded[0] = _payLoad;
88             new YapWriter(_payLoad, embedded, 1); // this line cascades and adds all embedded YapBytes
89
_payLoad.trim4(LENGTH_FOR_FIRST + addLengthBeforeFirst, length);
90         }
91         _payLoad.useSlot(_id, _address, length);
92         return _payLoad;
93     }
94
95     public void payLoad(YapWriter writer) {
96         _payLoad = writer;
97     }
98
99     public int getId() {
100         return _id;
101     }
102 }
103
Popular Tags