KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > util > io > win32 > Win32IoAdapter


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 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.util.io.win32;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import com.db4o.io.IoAdapter;
27
28 /**
29  * An IoAdapter implementation that uses JNI to talk directly with the WIN32 API.
30  */

31 public class Win32IoAdapter extends IoAdapter {
32     
33     static {
34         System.loadLibrary("Win32IoAdapter");
35     }
36     
37     private long _handle;
38
39     public Win32IoAdapter(String JavaDoc path, boolean lockFile, long initialLength) {
40         _handle = openFile(path, lockFile, initialLength);
41     }
42
43     public Win32IoAdapter() {
44     }
45
46     public void close() throws IOException JavaDoc {
47         closeFile(getHandle());
48         _handle = 0;
49     }
50
51     public void delete(String JavaDoc path) {
52         new File JavaDoc(path).delete();
53     }
54
55     public boolean exists(String JavaDoc path){
56         File JavaDoc existingFile = new File JavaDoc(path);
57         return existingFile.exists() && existingFile.length() > 0;
58     }
59
60     public long getLength() throws IOException JavaDoc {
61         return getLength(getHandle());
62     }
63
64     public IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength)
65             throws IOException JavaDoc {
66         return new Win32IoAdapter(path, lockFile, initialLength);
67     }
68
69     public int read(byte[] bytes, int length) throws IOException JavaDoc {
70         return read(getHandle(), bytes, length);
71     }
72
73     public void seek(long pos) throws IOException JavaDoc {
74         seek(getHandle(), pos);
75     }
76     
77     public void sync() throws IOException JavaDoc {
78         sync(getHandle());
79     }
80     
81     public void write(byte[] bytes, int length) throws IOException JavaDoc {
82         write(getHandle(), bytes, length);
83     }
84     
85     public void copy(long oldAddress, long newAddress, int length)
86             throws IOException JavaDoc {
87         copy(getHandle(), oldAddress, newAddress, length);
88     }
89
90     private long getHandle() {
91         if (0 == _handle) {
92             throw new IllegalStateException JavaDoc("File is not open.");
93         }
94         return _handle;
95     }
96     
97     private static native long openFile(String JavaDoc path, boolean lockFile, long initialLength);
98     
99     private static native void closeFile(long handle);
100     
101     private static native long getLength(long handle);
102     
103     private static native int read(long handle, byte[] bytes, int length);
104     
105     private static native void seek(long handle, long pos);
106     
107     private static native void sync(long handle);
108     
109     private static native void write(long handle, byte[] bytes, int lenght);
110     
111     private static native void copy(long handle, long oldAddress, long newAddress, int length);
112 }
113
Popular Tags