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.io;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.internal.*;
027:
028: /**
029: * IO adapter for random access files.
030: */
031: public class RandomAccessFileAdapter extends IoAdapter {
032:
033: private String _path;
034:
035: private RandomAccessFile _delegate;
036:
037: public RandomAccessFileAdapter() {
038: }
039:
040: protected RandomAccessFileAdapter(String path, boolean lockFile,
041: long initialLength, boolean readOnly)
042: throws Db4oIOException {
043: boolean ok = false;
044: try {
045: _path = new File(path).getCanonicalPath();
046: _delegate = new RandomAccessFile(_path, readOnly ? "r"
047: : "rw");
048: if (initialLength > 0) {
049: _delegate.seek(initialLength - 1);
050: _delegate.write(new byte[] { 0 });
051: }
052: if (lockFile) {
053: Platform4.lockFile(_path, _delegate);
054: }
055: ok = true;
056: } catch (IOException e) {
057: throw new Db4oIOException(e);
058: } finally {
059: if (!ok) {
060: close();
061: }
062: }
063: }
064:
065: public void close() throws Db4oIOException {
066: Platform4.unlockFile(_path, _delegate);
067: try {
068: if (_delegate != null) {
069: _delegate.close();
070: }
071: } catch (IOException e) {
072: throw new Db4oIOException(e);
073: }
074: }
075:
076: public void delete(String path) {
077: new File(path).delete();
078: }
079:
080: public boolean exists(String path) {
081: File existingFile = new File(path);
082: return existingFile.exists() && existingFile.length() > 0;
083: }
084:
085: public long getLength() throws Db4oIOException {
086: try {
087: return _delegate.length();
088: } catch (IOException e) {
089: throw new Db4oIOException(e);
090: }
091: }
092:
093: public IoAdapter open(String path, boolean lockFile,
094: long initialLength, boolean readOnly)
095: throws Db4oIOException {
096: return new RandomAccessFileAdapter(path, lockFile,
097: initialLength, readOnly);
098: }
099:
100: public int read(byte[] bytes, int length) throws Db4oIOException {
101: try {
102: return _delegate.read(bytes, 0, length);
103: } catch (IOException e) {
104: throw new Db4oIOException(e);
105: }
106: }
107:
108: public void seek(long pos) throws Db4oIOException {
109:
110: if (DTrace.enabled) {
111: DTrace.REGULAR_SEEK.log(pos);
112: }
113: try {
114: _delegate.seek(pos);
115: } catch (IOException e) {
116: throw new Db4oIOException(e);
117: }
118:
119: }
120:
121: public void sync() throws Db4oIOException {
122: try {
123: _delegate.getFD().sync();
124: } catch (IOException e) {
125: throw new Db4oIOException(e);
126: }
127: }
128:
129: public void write(byte[] buffer, int length) throws Db4oIOException {
130: try {
131: _delegate.write(buffer, 0, length);
132: } catch (IOException e) {
133: throw new Db4oIOException(e);
134: }
135: }
136: }
|