001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.handlers.*;
026: import com.db4o.internal.slots.*;
027: import com.db4o.marshall.*;
028:
029: /**
030: *
031: * @exclude
032: */
033: public class Buffer implements ReadBuffer, SlotBuffer, WriteBuffer {
034:
035: // for coding convenience, we allow objects to grab into the buffer
036: public byte[] _buffer;
037: public int _offset;
038:
039: Buffer() {
040: }
041:
042: public Buffer(int a_length) {
043: _buffer = new byte[a_length];
044: }
045:
046: public void seek(int offset) {
047: _offset = offset;
048: }
049:
050: public void writeBytes(byte[] bytes) {
051: System.arraycopy(bytes, 0, _buffer, _offset, bytes.length);
052: _offset += bytes.length;
053: }
054:
055: // TODO: Change all callers to call writeBytes directly.
056: public void append(byte[] bytes) {
057: writeBytes(bytes);
058: }
059:
060: public void append(Pointer4 pointer, final Buffer buffer) {
061: writeInt(buffer.length());
062: writeInt(pointer.id());
063: writeInt(pointer.address());
064: append(buffer._buffer);
065: }
066:
067: public final boolean containsTheSame(Buffer other) {
068: if (other != null) {
069: return Arrays4.areEqual(_buffer, other._buffer);
070: }
071: return false;
072: }
073:
074: public void copyTo(Buffer to, int fromOffset, int toOffset,
075: int length) {
076: System.arraycopy(_buffer, fromOffset, to._buffer, toOffset,
077: length);
078: }
079:
080: public int length() {
081: return _buffer.length;
082: }
083:
084: public void incrementOffset(int a_by) {
085: _offset += a_by;
086: }
087:
088: /**
089: * non-encrypted read, used for indexes
090: * @param a_stream
091: * @param a_address
092: */
093: public void read(ObjectContainerBase stream, int address,
094: int addressOffset) {
095: stream.readBytes(_buffer, address, addressOffset, length());
096: }
097:
098: public final void readBegin(byte identifier) {
099: if (Deploy.debug) {
100: Debug.readBegin(this , identifier);
101: }
102: }
103:
104: public BitMap4 readBitMap(int bitCount) {
105: BitMap4 map = new BitMap4(_buffer, _offset, bitCount);
106: _offset += map.marshalledLength();
107: return map;
108: }
109:
110: public byte readByte() {
111: return _buffer[_offset++];
112: }
113:
114: public byte[] readBytes(int a_length) {
115: byte[] bytes = new byte[a_length];
116: readBytes(bytes);
117: return bytes;
118: }
119:
120: public void readBytes(byte[] bytes) {
121: int length = bytes.length;
122: System.arraycopy(_buffer, _offset, bytes, 0, length);
123: _offset += length;
124: }
125:
126: public final Buffer readEmbeddedObject(Transaction trans)
127: throws Db4oIOException {
128: int address = readInt();
129: int length = readInt();
130: if (address == 0) {
131: return null;
132: }
133: return trans.container().bufferByAddress(address, length);
134: }
135:
136: public void readEncrypt(ObjectContainerBase stream, int address)
137: throws Db4oIOException {
138: stream.readBytes(_buffer, address, length());
139: stream._handlers.decrypt(this );
140: }
141:
142: public void readEnd() {
143: if (Deploy.debug) {
144: Debug.readEnd(this );
145: }
146: }
147:
148: public final int readInt() {
149: if (Deploy.debug) {
150: int ret = 0;
151: readBegin(Const4.YAPINTEGER);
152: if (Deploy.debugLong) {
153: ret = Integer.valueOf(
154: new LatinStringIO().read(this ,
155: Const4.INTEGER_BYTES).trim())
156: .intValue();
157: } else {
158: for (int i = 0; i < Const4.INTEGER_BYTES; i++) {
159: ret = (ret << 8) + (_buffer[_offset++] & 0xff);
160: }
161: }
162: readEnd();
163: return ret;
164: }
165:
166: int o = (_offset += 4) - 1;
167:
168: return (_buffer[o] & 255) | (_buffer[--o] & 255) << 8
169: | (_buffer[--o] & 255) << 16 | _buffer[--o] << 24;
170:
171: }
172:
173: public long readLong() {
174: return LongHandler.readLong(this );
175: }
176:
177: public Buffer readPayloadReader(int offset, int length) {
178: Buffer payLoad = new Buffer(length);
179: System.arraycopy(_buffer, offset, payLoad._buffer, 0, length);
180: return payLoad;
181: }
182:
183: public Slot readSlot() {
184: return new Slot(readInt(), readInt());
185: }
186:
187: void replaceWith(byte[] a_bytes) {
188: System.arraycopy(a_bytes, 0, _buffer, 0, length());
189: }
190:
191: public String toString() {
192: String str = "";
193: for (int i = 0; i < _buffer.length; i++) {
194: if (i > 0) {
195: str += " , ";
196: }
197: str += _buffer[i];
198: }
199: return str;
200: }
201:
202: public void writeBegin(byte a_identifier) {
203: if (Deploy.debug) {
204: if (Deploy.brackets) {
205: writeByte(Const4.YAPBEGIN);
206: }
207: if (Deploy.identifiers) {
208: writeByte(a_identifier);
209: }
210: }
211: }
212:
213: public final void writeBitMap(BitMap4 nullBitMap) {
214: nullBitMap.writeTo(_buffer, _offset);
215: _offset += nullBitMap.marshalledLength();
216: }
217:
218: public final void writeByte(byte a_byte) {
219: _buffer[_offset++] = a_byte;
220: }
221:
222: public void writeEnd() {
223: if (Deploy.debug && Deploy.brackets) {
224: writeByte(Const4.YAPEND);
225: }
226: }
227:
228: public final void writeInt(int a_int) {
229: if (Deploy.debug) {
230: IntHandler.writeInt(a_int, this );
231: } else {
232: int o = _offset + 4;
233: _offset = o;
234: byte[] b = _buffer;
235: b[--o] = (byte) a_int;
236: b[--o] = (byte) (a_int >>= 8);
237: b[--o] = (byte) (a_int >>= 8);
238: b[--o] = (byte) (a_int >> 8);
239: }
240: }
241:
242: public void writeIDOf(Transaction trans, Object obj) {
243: if (obj == null) {
244: writeInt(0);
245: return;
246: }
247:
248: if (obj instanceof PersistentBase) {
249: writeIDOf(trans, (PersistentBase) obj);
250: return;
251: }
252:
253: writeInt(((Integer) obj).intValue());
254: }
255:
256: public void writeIDOf(Transaction trans, PersistentBase persistent) {
257: if (persistent == null) {
258: writeInt(0);
259: return;
260: }
261: if (canWritePersistentBase()) {
262: persistent.writeOwnID(trans, this );
263: } else {
264: writeInt(persistent.getID());
265: }
266: }
267:
268: public final void writeSlot(Slot slot) {
269: writeInt(slot.address());
270: writeInt(slot.length());
271: }
272:
273: protected boolean canWritePersistentBase() {
274: return true;
275: }
276:
277: public void writeShortString(Transaction trans, String a_string) {
278: trans.container()._handlers._stringHandler.writeShort(trans,
279: a_string, this );
280: }
281:
282: public void writeLong(long l) {
283: LongHandler.writeLong(this , l);
284: }
285:
286: public void incrementIntSize() {
287: incrementOffset(Const4.INT_LENGTH);
288: }
289:
290: public int offset() {
291: return _offset;
292: }
293:
294: public void offset(int offset) {
295: _offset = offset;
296: }
297:
298: public void copyBytes(byte[] target, int sourceOffset,
299: int targetOffset, int length) {
300: System.arraycopy(_buffer, sourceOffset, target, targetOffset,
301: length);
302: }
303: }
|