KickJava   Java API By Example, From Geeks To Geeks.

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


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 abstract class StringMarshaller {
27     
28     
29     public abstract boolean inlinedStrings();
30     
31     public abstract void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection);
32     
33     protected final int linkLength(){
34         return YapConst.INT_LENGTH + YapConst.ID_LENGTH;
35     }
36     
37     public abstract Object JavaDoc writeNew(Object JavaDoc a_object, boolean topLevel, YapWriter a_bytes, boolean redirect);
38     
39     public final String JavaDoc read(YapStream stream, YapReader reader) throws CorruptionException {
40         if (reader == null) {
41             return null;
42         }
43         if (Deploy.debug) {
44             reader.readBegin(YapConst.YAPSTRING);
45         }
46         String JavaDoc ret = readShort(stream, reader);
47         if (Deploy.debug) {
48             reader.readEnd();
49         }
50         return ret;
51     }
52     
53     public String JavaDoc readFromOwnSlot(YapStream stream, YapReader reader){
54         try {
55             return read(stream, reader);
56         } catch (Exception JavaDoc e) {
57             if(Deploy.debug || Debug.atHome) {
58                 e.printStackTrace();
59             }
60         }
61         return "";
62     }
63     
64     public String JavaDoc readFromParentSlot(YapStream stream, YapReader reader, boolean redirect) throws CorruptionException {
65         if(! redirect){
66             return read(stream, reader);
67         }
68         return read(stream, readSlotFromParentSlot(stream, reader));
69     }
70     
71     public abstract YapReader readIndexEntry(YapWriter parentSlot) throws CorruptionException;
72     
73     public static String JavaDoc readShort(YapStream stream, YapReader bytes) throws CorruptionException {
74         return readShort(stream.stringIO(),stream.configImpl().internStrings(),bytes);
75     }
76
77     public static String JavaDoc readShort(YapStringIO io, boolean internStrings, YapReader bytes) throws CorruptionException {
78         int length = bytes.readInt();
79         if (length > YapConst.MAXIMUM_BLOCK_SIZE) {
80             throw new CorruptionException();
81         }
82         if (length > 0) {
83             String JavaDoc str = io.read(bytes, length);
84             if(! Deploy.csharp){
85                 if(internStrings){
86                     str = str.intern();
87                 }
88             }
89             return str;
90         }
91         return "";
92     }
93
94     // TODO: Instead of working with YapReader objects to transport
95
// string buffers, we should consider to have a specific string
96
// buffer class, that allows comparisons and carries it's encoding.
97
public abstract YapReader readSlotFromParentSlot(YapStream stream, YapReader reader) throws CorruptionException;
98
99     public static YapReader writeShort(YapStream stream, String JavaDoc str){
100         YapReader reader = new YapReader(stream.stringIO().length(str));
101         writeShort(stream, str, reader);
102         return reader;
103     }
104     
105     public static void writeShort(YapStream stream, String JavaDoc str, YapReader reader){
106         int length = str.length();
107         if (Deploy.debug) {
108             reader.writeBegin(YapConst.YAPSTRING);
109         }
110         reader.writeInt(length);
111         stream.stringIO().write(reader, str);
112         if (Deploy.debug) {
113             reader.writeEnd();
114         }
115     }
116
117     public abstract void defrag(SlotReader reader);
118 }
119
Popular Tags