KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapReader


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;
22
23 import com.db4o.foundation.*;
24
25 /**
26  * public for .NET conversion reasons.
27  *
28  * @exclude
29  * @renameto Buffer
30  */

31 public class YapReader implements SlotReader {
32     
33     // for coding convenience, we allow objects to grab into the buffer
34
public byte[] _buffer;
35     public int _offset;
36
37     
38     YapReader(){
39     }
40     
41     public YapReader(int a_length){
42         _buffer = new byte[a_length];
43     }
44     
45     public void seek(int offset) {
46         _offset = offset;
47     }
48     
49     public final void append(byte a_byte) {
50         _buffer[_offset++] = a_byte;
51     }
52
53     public void append(byte[] a_bytes) {
54         System.arraycopy(a_bytes, 0, _buffer, _offset, a_bytes.length);
55         _offset += a_bytes.length;
56     }
57     
58     final boolean containsTheSame(YapReader other) {
59         if(other != null) {
60             byte[] otherBytes = other._buffer;
61             if(_buffer == null) {
62                 return otherBytes == null;
63             }
64             if(otherBytes != null && _buffer.length == otherBytes.length) {
65                 int len = _buffer.length;
66                 for (int i = 0; i < len; i++) {
67                     if(_buffer[i] != otherBytes[i]) {
68                         return false;
69                     }
70                 }
71                 return true;
72             }
73         }
74         return false;
75     }
76     
77     public void copyTo(YapReader to, int fromOffset, int toOffset, int length) {
78         System.arraycopy(_buffer, fromOffset, to._buffer, toOffset, length);
79     }
80
81     public int getLength() {
82         return _buffer.length;
83     }
84     
85     public void incrementOffset(int a_by) {
86         _offset += a_by;
87     }
88     
89     /**
90      * non-encrypted read, used for indexes
91      * @param a_stream
92      * @param a_address
93      */

94     public void read(YapStream a_stream, int a_address, int addressOffset){
95         a_stream.readBytes(_buffer, a_address, addressOffset, getLength());
96     }
97     
98     public final void readBegin(byte a_identifier) {
99         if (Deploy.debug) {
100             if (Deploy.brackets) {
101                 if (readByte() != YapConst.YAPBEGIN) {
102                     throw new RuntimeException JavaDoc("YapBytes.readBegin() YAPBEGIN expected.");
103                 }
104             }
105             if (Deploy.identifiers) {
106                 byte readB = readByte();
107                 if (readB != a_identifier) {
108                     throw new RuntimeException JavaDoc("YapBytes.readBegin() wrong identifier: "+(char)readB);
109                 }
110             }
111         }
112     }
113     
114     public BitMap4 readBitMap(int bitCount){
115         BitMap4 map = new BitMap4(_buffer, _offset, bitCount);
116         _offset += map.marshalledLength();
117         return map;
118     }
119     
120     public byte readByte() {
121         return _buffer[_offset++];
122     }
123     
124     public byte[] readBytes(int a_length){
125         byte[] bytes = new byte[a_length];
126         readBytes(bytes);
127         return bytes;
128     }
129     
130     void readBytes(byte[] bytes) {
131         int length = bytes.length;
132         System.arraycopy(_buffer, _offset, bytes, 0, length);
133         _offset += length;
134     }
135     
136     public final YapReader readEmbeddedObject(Transaction a_trans) {
137         return a_trans.stream().readReaderByAddress(readInt(), readInt());
138     }
139     
140     public void readEncrypt(YapStream a_stream, int a_address) {
141         a_stream.readBytes(_buffer, a_address, getLength());
142         a_stream.i_handlers.decrypt(this);
143     }
144
145     public void readEnd() {
146         if (Deploy.debug && Deploy.brackets) {
147             if (readByte() != YapConst.YAPEND) {
148                 throw new RuntimeException JavaDoc("YapBytes.readEnd() YAPEND expected");
149             }
150         }
151     }
152
153     public final int readInt() {
154         if (Deploy.debug) {
155             return YInt.readInt(this);
156         }
157             
158         // if (YapConst.INTEGER_BYTES == 4) {
159

160         int o = (_offset += 4) - 1;
161         return (_buffer[o] & 255) | (_buffer[--o] & 255)
162             << 8 | (_buffer[--o] & 255)
163             << 16 | _buffer[--o]
164             << 24;
165                 
166 // } else {
167
// int ret = 0;
168
// int ii = _offset + YapConst.INTEGER_BYTES;
169
// while (_offset < ii) {
170
// ret = (ret << 8) + (_buffer[_offset++] & 0xff);
171
// }
172
// return ret;
173
// }
174

175     }
176     
177     public long readLong() {
178         return YLong.readLong(this);
179     }
180     
181     public YapReader readPayloadReader(int offset, int length){
182         YapReader payLoad = new YapReader(length);
183         System.arraycopy(_buffer,offset, payLoad._buffer, 0, length);
184         return payLoad;
185     }
186
187     void replaceWith(byte[] a_bytes) {
188         System.arraycopy(a_bytes, 0, _buffer, 0, getLength());
189     }
190     
191     public String JavaDoc toString() {
192         try {
193             String JavaDoc str = "";
194             for (int i = 0; i < _buffer.length; i++) {
195                 if(i > 0){
196                    str += " , ";
197                 }
198                 str += _buffer[i];
199             }
200             return str;
201         } catch (Exception JavaDoc e) {
202             if(Deploy.debug || Debug.atHome) {
203                 e.printStackTrace();
204             }
205         }
206         return "";
207     }
208     
209     public void writeBegin(byte a_identifier) {
210         if (Deploy.debug) {
211             if (Deploy.brackets) {
212                 append(YapConst.YAPBEGIN);
213             }
214             if (Deploy.identifiers) {
215                 append(a_identifier);
216             }
217         }
218     }
219     
220     public final void writeBitMap(BitMap4 nullBitMap) {
221         nullBitMap.writeTo(_buffer, _offset);
222         _offset += nullBitMap.marshalledLength();
223     }
224     
225     public final void writeEncrypt(YapFile file, int address, int addressOffset) {
226         file.i_handlers.encrypt(this);
227         file.writeBytes(this, address, addressOffset);
228         file.i_handlers.decrypt(this);
229     }
230     
231     public void writeEnd() {
232         if (Deploy.debug && Deploy.brackets) {
233             append(YapConst.YAPEND);
234         }
235     }
236     
237     public final void writeInt(int a_int) {
238         
239         if (Deploy.debug) {
240             YInt.writeInt(a_int, this);
241         } else {
242             
243 // if (YapConst.INTEGER_BYTES == 4) {
244

245                 int o = _offset + 4;
246                 _offset = o;
247                 byte[] b = _buffer;
248                 b[--o] = (byte)a_int;
249                 b[--o] = (byte) (a_int >>= 8);
250                 b[--o] = (byte) (a_int >>= 8);
251                 b[--o] = (byte) (a_int >>= 8);
252                 
253 // } else {
254
// for (; ii >= 0; ii -= 8) {
255
// _buffer[_offset++] = (byte) (a_int >> ii);
256
// }
257
// }
258

259         }
260     }
261     
262     public void writeIDOf(Transaction trans, Object JavaDoc obj) {
263         if(obj == null){
264             writeInt(0);
265             return;
266         }
267         
268         if(obj instanceof YapMeta){
269             writeIDOf(trans, (YapMeta)obj);
270             return;
271         }
272         
273         writeInt(((Integer JavaDoc)obj).intValue());
274     }
275     
276     public void writeIDOf(Transaction trans, YapMeta yapMeta) {
277         if(yapMeta == null){
278             writeInt(0);
279             return;
280         }
281         yapMeta.writeOwnID(trans, this);
282     }
283     
284     public void writeShortString(Transaction trans, String JavaDoc a_string) {
285         trans.stream().i_handlers.i_stringHandler.writeShort(a_string, this);
286     }
287
288     public void writeLong(long l) {
289         YLong.writeLong(l, this);
290     }
291
292     public void incrementIntSize() {
293         incrementOffset(YapConst.INT_LENGTH);
294     }
295
296     public int offset() {
297         return _offset;
298     }
299
300     public void offset(int offset) {
301         _offset=offset;
302     }
303
304     public void copyBytes(byte[] target,int sourceOffset,int targetOffset, int length) {
305         System.arraycopy(_buffer, sourceOffset, target, targetOffset, length);
306     }
307 }
308
Popular Tags