KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class StringMarshaller1 extends StringMarshaller{
27     
28     private static final int DEFRAGMENT_INCREMENT_OFFSET = YapConst.INT_LENGTH * 2;
29     
30     public boolean inlinedStrings(){
31         return true;
32     }
33     
34     public void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection) {
35         
36         if(topLevel){
37             header.addBaseLength(linkLength());
38             header.prepareIndexedPayLoadEntry(trans);
39         }else{
40             if(withIndirection){
41                 header.addPayLoadLength(linkLength());
42             }
43         }
44         
45         if(obj == null){
46             return;
47         }
48         
49         header.addPayLoadLength(trans.stream().stringIO().length((String JavaDoc)obj));
50     }
51     
52     public Object JavaDoc writeNew(Object JavaDoc obj, boolean topLevel, YapWriter writer, boolean redirect) {
53         
54         YapStream stream = writer.getStream();
55         String JavaDoc str = (String JavaDoc) obj;
56         
57         if(! redirect){
58             if(str != null){
59                 writeShort(stream,str , writer);
60             }
61             // TODO: Really we should return a YapWriter for indexing but
62
// for now it's not needed since this is used for untyped
63
// references only which are not indexed.
64
return str;
65         }
66         
67         if (str == null) {
68             writer.writeEmbeddedNull();
69             return null;
70         }
71         
72         int length = stream.stringIO().length(str);
73         
74         YapWriter bytes = new YapWriter(writer.getTransaction(), length);
75         writeShort(stream, str, bytes);
76         
77         writer.writePayload(bytes, topLevel);
78         return bytes;
79     }
80     
81     public YapReader readIndexEntry(YapWriter parentSlot) throws CorruptionException{
82         int payLoadOffSet = parentSlot.readInt();
83         int length = parentSlot.readInt();
84         if(payLoadOffSet == 0){
85             return null;
86         }
87         return parentSlot.readPayloadWriter(payLoadOffSet, length);
88     }
89     
90     public YapReader readSlotFromParentSlot(YapStream stream, YapReader reader) throws CorruptionException {
91         int payLoadOffSet = reader.readInt();
92         int length = reader.readInt();
93         if(payLoadOffSet == 0){
94             return null;
95         }
96         return reader.readPayloadReader(payLoadOffSet, length);
97     }
98
99     public void defrag(SlotReader reader) {
100         reader.incrementOffset(DEFRAGMENT_INCREMENT_OFFSET);
101     }
102
103 }
104
Popular Tags