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.db4ounit.common.handlers;
022:
023: import com.db4o.*;
024: import com.db4o.internal.*;
025: import com.db4o.marshall.*;
026:
027: public abstract class MockMarshallingContext {
028:
029: private final ObjectContainer _objectContainer;
030:
031: final Buffer _header;
032:
033: final Buffer _payLoad;
034:
035: protected Buffer _current;
036:
037: public MockMarshallingContext(ObjectContainer objectContainer) {
038: _objectContainer = objectContainer;
039: _header = new Buffer(1000);
040: _payLoad = new Buffer(1000);
041: _current = _header;
042: }
043:
044: public WriteBuffer newBuffer(int length) {
045: return new Buffer(length);
046: }
047:
048: public ObjectContainer objectContainer() {
049: return _objectContainer;
050: }
051:
052: public void useVariableLength() {
053: _current = _payLoad;
054: }
055:
056: public byte readByte() {
057: return _current.readByte();
058: }
059:
060: public void readBytes(byte[] bytes) {
061: _current.readBytes(bytes);
062: }
063:
064: public int readInt() {
065: return _current.readInt();
066: }
067:
068: public long readLong() {
069: return _current.readLong();
070: }
071:
072: public void writeByte(byte b) {
073: _current.writeByte(b);
074: }
075:
076: public void writeInt(int i) {
077: _current.writeInt(i);
078: }
079:
080: public void writeLong(long l) {
081: _current.writeLong(l);
082: }
083:
084: public void writeBytes(byte[] bytes) {
085: _current.writeBytes(bytes);
086: }
087:
088: public Object readObject() {
089: int id = readInt();
090: Object obj = container().getByID(transaction(), id);
091: objectContainer().activate(obj, Integer.MAX_VALUE);
092: return obj;
093: }
094:
095: public void writeObject(Object obj) {
096: int id = container().setInternal(transaction(), obj, false);
097: writeInt(id);
098: }
099:
100: public Transaction transaction() {
101: return container().transaction();
102: }
103:
104: protected ObjectContainerBase container() {
105: return ((InternalObjectContainer) _objectContainer).container();
106: }
107:
108: }
|