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.io;
22:
23: /**
24: * Bounded handle into an IoAdapter: Can only access a restricted area.
25: */
26: public class IoAdapterWindow {
27:
28: private IoAdapter _io;
29: private int _blockOff;
30: private int _len;
31: private boolean _disabled;
32:
33: /**
34: * @param io The delegate I/O adapter
35: * @param blockOff The block offset address into the I/O adapter that maps to the start index (0) of this window
36: * @param len The size of this window in bytes
37: */
38: public IoAdapterWindow(IoAdapter io, int blockOff, int len) {
39: _io = io;
40: _blockOff = blockOff;
41: _len = len;
42: _disabled = false;
43: }
44:
45: /**
46: * @return Size of this I/O adapter window in bytes.
47: */
48: public int length() {
49: return _len;
50: }
51:
52: /**
53: * @param off Offset in bytes relative to the window start
54: * @param data Data to write into the window starting from the given offset
55: */
56: public void write(int off, byte[] data)
57: throws IllegalArgumentException, IllegalStateException {
58: checkBounds(off, data);
59: _io.blockSeek(_blockOff + off);
60: _io.write(data);
61: }
62:
63: /**
64: * @param off Offset in bytes relative to the window start
65: * @param data Data buffer to read from the window starting from the given offset
66: */
67: public int read(int off, byte[] data)
68: throws IllegalArgumentException, IllegalStateException {
69: checkBounds(off, data);
70: _io.blockSeek(_blockOff + off);
71: return _io.read(data);
72: }
73:
74: /**
75: * Disable IO Adapter Window
76: */
77: public void disable() {
78: _disabled = true;
79: }
80:
81: /**
82: * Flush IO Adapter Window
83: */
84: public void flush() {
85: if (!_disabled) {
86: _io.sync();
87: }
88: }
89:
90: private void checkBounds(int off, byte[] data) {
91: if (_disabled) {
92: throw new IllegalStateException();
93: }
94: if (data == null || off < 0 || off + data.length > _len) {
95: throw new IllegalArgumentException();
96: }
97: }
98: }
|