37 |
|
|
38 |
package gnu.java.nio; |
package gnu.java.nio; |
39 |
|
|
40 |
import java.io.*; |
import java.io.EOFException; |
41 |
import java.nio.*; |
import java.io.FileInputStream; |
42 |
import java.nio.channels.*; |
import java.io.FileOutputStream; |
43 |
|
import java.io.IOException; |
44 |
/** This file is not user visible ! |
import java.io.RandomAccessFile; |
45 |
* But alas, Java does not have a concept of frieldly packages |
import java.nio.ByteBuffer; |
46 |
|
import java.nio.MappedByteBuffer; |
47 |
|
import java.nio.channels.FileChannel; |
48 |
|
|
49 |
|
/** |
50 |
|
* This file is not user visible ! |
51 |
|
* But alas, Java does not have a concept of friendly packages |
52 |
* so this class is public. |
* so this class is public. |
53 |
* Instances of this class are created by invoking getChannel |
* Instances of this class are created by invoking getChannel |
54 |
* Upon a Input/Output/RandomAccessFile object. |
* Upon a Input/Output/RandomAccessFile object. |
56 |
|
|
57 |
public class FileChannelImpl extends FileChannel |
public class FileChannelImpl extends FileChannel |
58 |
{ |
{ |
59 |
public long address; |
public long address; |
60 |
public int length; |
public int length; |
61 |
public int fd; |
public int fd; |
62 |
public MappedByteBuffer buf; |
public MappedByteBuffer buf; |
63 |
public Object file_obj; // just to keep it live... |
public Object file_obj; // just to keep it live... |
64 |
|
|
65 |
/** |
/** |
66 |
* This method came from java.io.RandomAccessFile |
* This method came from java.io.RandomAccessFile |
67 |
* It is private there so we will repeat it here. |
* It is private there so we will repeat it here. |
68 |
*/ |
*/ |
69 |
private native long lengthInternal(int native_fd) throws IOException; |
private native long lengthInternal (int native_fd) throws IOException; |
|
|
|
|
public FileChannelImpl(int fd, |
|
|
Object obj) |
|
|
{ |
|
|
this.fd = fd; |
|
|
this.file_obj = obj; |
|
|
|
|
|
// System.out.println("file channel: " + fd); |
|
|
} |
|
70 |
|
|
71 |
public long size() throws IOException |
public FileChannelImpl (int fd, Object obj) |
72 |
{ |
{ |
73 |
return lengthInternal(fd); |
this.fd = fd; |
74 |
|
this.file_obj = obj; |
75 |
|
} |
76 |
|
|
77 |
} |
public long size () throws IOException |
78 |
|
{ |
79 |
|
return lengthInternal (fd); |
80 |
|
} |
81 |
|
|
82 |
protected void implCloseChannel() throws IOException |
protected void implCloseChannel() throws IOException |
83 |
{ |
{ |
84 |
//System.out.println("length in Java ="+length); |
if (address != 0) |
|
|
|
|
if (address != 0) |
|
85 |
{ |
{ |
86 |
nio_unmmap_file(fd, |
nio_unmmap_file (fd, address, (int) length); |
|
address, |
|
|
(int)length); |
|
87 |
} |
} |
88 |
|
|
89 |
// FIXME |
// FIXME |
90 |
fd = 0; |
fd = 0; |
91 |
|
|
92 |
if (file_obj instanceof RandomAccessFile) |
if (file_obj instanceof RandomAccessFile) |
93 |
{ |
{ |
94 |
RandomAccessFile o = (RandomAccessFile) file_obj; |
RandomAccessFile o = (RandomAccessFile) file_obj; |
95 |
o.close(); |
o.close(); |
|
//System.out.println("closing stream too"); |
|
96 |
} |
} |
97 |
else if (file_obj instanceof FileInputStream) |
else if (file_obj instanceof FileInputStream) |
98 |
{ |
{ |
99 |
FileInputStream o = (FileInputStream) file_obj; |
FileInputStream o = (FileInputStream) file_obj; |
100 |
o.close(); |
o.close(); |
101 |
} |
} |
102 |
else if (file_obj instanceof FileOutputStream) |
else if (file_obj instanceof FileOutputStream) |
103 |
{ |
{ |
104 |
FileOutputStream o = (FileOutputStream) file_obj; |
FileOutputStream o = (FileOutputStream) file_obj; |
105 |
o.close(); |
o.close(); |
106 |
} |
} |
107 |
} |
} |
108 |
|
|
109 |
public int read(java.nio.ByteBuffer dst) throws IOException |
public int read (ByteBuffer dst) throws IOException |
110 |
{ |
{ |
111 |
//System.out.println("unimplemented: in here-1"); |
int w = 0; |
112 |
|
int s = (int)size(); |
|
int w = 0; |
|
|
|
|
|
int s = (int)size(); |
|
113 |
|
|
114 |
if (buf == null) |
if (buf == null) |
115 |
{ |
{ |
116 |
throw new EOFException("file not mapped"); |
throw new EOFException("file not mapped"); |
117 |
} |
} |
118 |
|
|
119 |
for (int i=0; i<s; i++) |
for (int i=0; i<s; i++) |
120 |
{ |
{ |
121 |
dst.put( buf.get() ); |
dst.put( buf.get() ); |
122 |
} |
} |
123 |
|
|
124 |
return s; |
return s; |
125 |
} |
} |
126 |
|
|
127 |
public int write(java.nio.ByteBuffer src) throws IOException |
public long read (ByteBuffer[] dsts) throws IOException |
128 |
{ |
{ |
129 |
int w = 0; |
return read (dsts, 0, dsts.length); |
130 |
|
} |
131 |
|
|
132 |
if (buf == null) |
public long read (ByteBuffer[] dsts, int offset, int length) |
133 |
|
throws IOException |
134 |
|
{ |
135 |
|
long result = 0; |
136 |
|
|
137 |
|
for (int i = offset; i < offset + length; i++) |
138 |
{ |
{ |
139 |
throw new EOFException("file not mapped"); |
result += write (dsts[i]); |
140 |
} |
} |
141 |
|
|
142 |
while (src.hasRemaining()) |
return result; |
143 |
|
} |
144 |
|
|
145 |
|
public int write (ByteBuffer src) throws IOException |
146 |
|
{ |
147 |
|
int w = 0; |
148 |
|
|
149 |
|
if (buf == null) |
150 |
{ |
{ |
151 |
buf.put(src.get()); |
throw new EOFException ("file not mapped"); |
|
w++; |
|
152 |
} |
} |
153 |
return w; |
|
154 |
} |
while (src.hasRemaining ()) |
155 |
|
{ |
156 |
|
buf.put (src.get ()); |
157 |
|
w++; |
158 |
|
} |
159 |
|
|
160 |
|
return w; |
161 |
|
} |
162 |
|
|
163 |
public long write(java.nio.ByteBuffer[] srcs, |
public long write(ByteBuffer[] srcs, int offset, int length) |
164 |
int offset, |
throws IOException |
165 |
int length) throws IOException |
{ |
166 |
{ |
long res = 0; |
|
long res = 0; |
|
167 |
|
|
168 |
for (int i=offset;i<offset+length;i++) |
for (int i = offset;i < offset + length;i++) |
169 |
{ |
{ |
170 |
res += write(srcs[i]); |
res += write (srcs[i]); |
171 |
} |
} |
172 |
return res; |
return res; |
173 |
} |
} |
184 |
return null; |
return null; |
185 |
} |
} |
186 |
|
|
187 |
static MappedByteBuffer create_direct_mapped_buffer(long address, |
static MappedByteBuffer create_direct_mapped_buffer (long address, |
188 |
long length) |
long length) |
189 |
{ |
{ |
190 |
FileChannelImpl ch = new FileChannelImpl(-1, null); |
FileChannelImpl ch = new FileChannelImpl (-1, null); |
191 |
|
ch.address = address; |
192 |
ch.address = address; |
ch.length = (int) length; |
193 |
ch.length = (int)length; |
ch.buf = new MappedByteFileBuffer (ch); |
194 |
|
return ch.buf; |
195 |
|
} |
196 |
|
|
197 |
ch.buf = new MappedByteFileBuffer(ch); |
public long write (ByteBuffer[] srcs) |
198 |
return ch.buf; |
throws IOException |
199 |
} |
{ |
200 |
|
return write (srcs, 0, srcs.length); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
|
* msync with the disk |
205 |
|
*/ |
206 |
|
public void force (boolean metaData) |
207 |
|
{ |
208 |
|
nio_msync (fd, address, length); |
209 |
|
} |
210 |
|
|
211 |
/* msync with the disk */ |
static native long nio_mmap_file (int fd, long pos, int size, int mode); |
|
public void force(boolean metaData) |
|
|
{ |
|
|
nio_msync(fd, address, length); |
|
|
} |
|
212 |
|
|
213 |
static native long nio_mmap_file(int fd, |
static native void nio_unmmap_file (int fd, long address, int size); |
|
long pos, |
|
|
int size, |
|
|
int mode); |
|
|
|
|
|
static native void nio_unmmap_file(int fd, |
|
|
long address, |
|
|
int size); |
|
|
static native void nio_msync(int fd, |
|
|
long address, |
|
|
int length); |
|
214 |
|
|
215 |
public long write(ByteBuffer[] srcs) throws IOException { |
static native void nio_msync (int fd, long address, int length); |
|
throw new Error("not implemented"); |
|
|
} |
|
|
public long read(ByteBuffer[] srcs, int offset, int length) throws IOException { |
|
|
throw new Error("not implemented"); |
|
|
} |
|
|
public long read(ByteBuffer[] srcs) throws IOException { |
|
|
throw new Error("not implemented"); |
|
|
} |
|
216 |
} |
} |