01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.test.acid;
22:
23: import java.io.*;
24:
25: import com.db4o.*;
26: import com.db4o.io.*;
27:
28: public class LoggingIoAdapter extends VanillaIoAdapter {
29: private final static int READ = 1;
30: private final static int WRITE = 2;
31: private final static int SYNC = 4;
32:
33: private PrintStream _out;
34: private int _config;
35: private long _curpos;
36:
37: public LoggingIoAdapter(IoAdapter delegateAdapter, PrintStream out) {
38: this (delegateAdapter, out, WRITE);
39: }
40:
41: public LoggingIoAdapter(IoAdapter delegateAdapter, PrintStream out,
42: int config) {
43: super (delegateAdapter);
44: _out = out;
45: _config = config;
46: }
47:
48: private LoggingIoAdapter(IoAdapter delegateAdapter, String path,
49: boolean lockFile, long initialLength, PrintStream out,
50: int config) throws Db4oIOException {
51: super (delegateAdapter
52: .open(path, lockFile, initialLength, false));
53: _out = out;
54: _config = config;
55: _curpos = 0;
56: }
57:
58: public IoAdapter open(String path, boolean lockFile,
59: long initialLength, boolean readOnly)
60: throws Db4oIOException {
61: return new LoggingIoAdapter(_delegate, path, lockFile,
62: initialLength, _out, _config);
63: }
64:
65: public int read(byte[] bytes, int length) throws Db4oIOException {
66: if (config(READ)) {
67: _out.println("READ " + _curpos + "," + length);
68: }
69: return _delegate.read(bytes, length);
70: }
71:
72: public void seek(long pos) throws Db4oIOException {
73: _curpos = pos;
74: _delegate.seek(pos);
75: }
76:
77: public void sync() throws Db4oIOException {
78: if (config(SYNC)) {
79: _out.println("SYNC");
80: }
81: _delegate.sync();
82: }
83:
84: public void write(byte[] buffer, int length) throws Db4oIOException {
85: if (config(WRITE)) {
86: _out.println("WRITE " + _curpos + "," + length);
87: }
88: _delegate.write(buffer, length);
89: }
90:
91: private boolean config(int mask) {
92: return (_config & mask) != 0;
93: }
94: }
|