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.util.io.win32;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.io.*;
027:
028: /**
029: * An IoAdapter implementation that uses JNI to talk directly with the WIN32 API.
030: */
031: public class Win32IoAdapter extends IoAdapter {
032:
033: static {
034: System.loadLibrary("Win32IoAdapter");
035: }
036:
037: private long _handle;
038:
039: public Win32IoAdapter(String path, boolean lockFile,
040: long initialLength, boolean readOnly) {
041: try {
042: _handle = openFile(path, lockFile, initialLength);
043: } catch (IOException e) {
044: throw new Db4oIOException(e);
045: }
046: }
047:
048: public Win32IoAdapter() {
049: }
050:
051: public void close() throws Db4oIOException {
052: try {
053: closeFile(getHandle());
054: _handle = 0;
055: } catch (IOException e) {
056: throw new Db4oIOException(e);
057: }
058: }
059:
060: public void delete(String path) {
061: new File(path).delete();
062: }
063:
064: public boolean exists(String path) {
065: File existingFile = new File(path);
066: return existingFile.exists() && existingFile.length() > 0;
067: }
068:
069: public long getLength() throws Db4oIOException {
070: try {
071: return getLength(getHandle());
072: } catch (IOException e) {
073: throw new Db4oIOException(e);
074: }
075: }
076:
077: public IoAdapter open(String path, boolean lockFile,
078: long initialLength, boolean readOnly)
079: throws Db4oIOException {
080: return new Win32IoAdapter(path, lockFile, initialLength,
081: readOnly);
082: }
083:
084: public int read(byte[] bytes, int length) throws Db4oIOException {
085: try {
086: return read(getHandle(), bytes, length);
087: } catch (IOException e) {
088: throw new Db4oIOException(e);
089: }
090: }
091:
092: public void seek(long pos) throws Db4oIOException {
093: try {
094: seek(getHandle(), pos);
095: } catch (IOException e) {
096: throw new Db4oIOException(e);
097: }
098: }
099:
100: public void sync() throws Db4oIOException {
101: try {
102: sync(getHandle());
103: } catch (IOException e) {
104: throw new Db4oIOException(e);
105: }
106: }
107:
108: public void write(byte[] bytes, int length) throws Db4oIOException {
109: try {
110: write(getHandle(), bytes, length);
111: } catch (IOException e) {
112: throw new Db4oIOException(e);
113: }
114: }
115:
116: public void copy(long oldAddress, long newAddress, int length)
117: throws Db4oIOException {
118: try {
119: copy(getHandle(), oldAddress, newAddress, length);
120: } catch (IOException e) {
121: throw new Db4oIOException(e);
122: }
123: }
124:
125: private long getHandle() {
126: if (0 == _handle) {
127: throw new IllegalStateException("File is not open.");
128: }
129: return _handle;
130: }
131:
132: private static native long openFile(String path, boolean lockFile,
133: long initialLength) throws IOException;
134:
135: private static native void closeFile(long handle)
136: throws IOException;
137:
138: private static native long getLength(long handle)
139: throws IOException;
140:
141: private static native int read(long handle, byte[] bytes, int length)
142: throws IOException;
143:
144: private static native void seek(long handle, long pos)
145: throws IOException;
146:
147: private static native void sync(long handle) throws IOException;
148:
149: private static native void write(long handle, byte[] bytes,
150: int length) throws IOException;
151:
152: private static native void copy(long handle, long oldAddress,
153: long newAddress, int length) throws IOException;
154: }
|