1 | /* FileChannelImpl.java --
|
---|
2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package gnu.java.nio;
|
---|
39 |
|
---|
40 | import java.io.EOFException;
|
---|
41 | import java.io.FileInputStream;
|
---|
42 | import java.io.FileOutputStream;
|
---|
43 | import java.io.IOException;
|
---|
44 | import java.io.RandomAccessFile;
|
---|
45 | import java.nio.ByteBuffer;
|
---|
46 | import java.nio.MappedByteBuffer;
|
---|
47 | import java.nio.channels.ClosedChannelException;
|
---|
48 | import java.nio.channels.FileChannel;
|
---|
49 | import java.nio.channels.FileLock;
|
---|
50 | import java.nio.channels.NonReadableChannelException;
|
---|
51 | import java.nio.channels.NonWritableChannelException;
|
---|
52 | import java.nio.channels.ReadableByteChannel;
|
---|
53 | import java.nio.channels.WritableByteChannel;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * This file is not user visible !
|
---|
57 | * But alas, Java does not have a concept of friendly packages
|
---|
58 | * so this class is public.
|
---|
59 | * Instances of this class are created by invoking getChannel
|
---|
60 | * Upon a Input/Output/RandomAccessFile object.
|
---|
61 | */
|
---|
62 |
|
---|
63 | public class FileChannelImpl extends FileChannel
|
---|
64 | {
|
---|
65 | public long address;
|
---|
66 | public int length;
|
---|
67 | public int fd;
|
---|
68 | public MappedByteBuffer buf;
|
---|
69 | public Object file_obj; // just to keep it live...
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * This method came from java.io.RandomAccessFile
|
---|
73 | * It is private there so we will repeat it here.
|
---|
74 | */
|
---|
75 | private native long lengthInternal (int native_fd) throws IOException;
|
---|
76 |
|
---|
77 | public FileChannelImpl (int fd, Object obj)
|
---|
78 | {
|
---|
79 | this.fd = fd;
|
---|
80 | this.file_obj = obj;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public long size () throws IOException
|
---|
84 | {
|
---|
85 | if (!isOpen ())
|
---|
86 | throw new ClosedChannelException ();
|
---|
87 |
|
---|
88 | return lengthInternal (fd);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void implCloseChannel() throws IOException
|
---|
92 | {
|
---|
93 | if (address != 0)
|
---|
94 | {
|
---|
95 | nio_unmmap_file (fd, address, (int) length);
|
---|
96 | address = 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // FIXME
|
---|
100 | fd = 0;
|
---|
101 |
|
---|
102 | if (file_obj instanceof RandomAccessFile)
|
---|
103 | {
|
---|
104 | RandomAccessFile o = (RandomAccessFile) file_obj;
|
---|
105 | o.close();
|
---|
106 | }
|
---|
107 | else if (file_obj instanceof FileInputStream)
|
---|
108 | {
|
---|
109 | FileInputStream o = (FileInputStream) file_obj;
|
---|
110 | o.close();
|
---|
111 | }
|
---|
112 | else if (file_obj instanceof FileOutputStream)
|
---|
113 | {
|
---|
114 | FileOutputStream o = (FileOutputStream) file_obj;
|
---|
115 | o.close();
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public int read (ByteBuffer dst) throws IOException
|
---|
120 | {
|
---|
121 | int s = (int)size();
|
---|
122 |
|
---|
123 | if (buf == null)
|
---|
124 | {
|
---|
125 | throw new EOFException("file not mapped");
|
---|
126 | }
|
---|
127 |
|
---|
128 | for (int i=0; i<s; i++)
|
---|
129 | {
|
---|
130 | dst.put( buf.get() );
|
---|
131 | }
|
---|
132 |
|
---|
133 | return s;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public int read (ByteBuffer dst, long position)
|
---|
137 | throws IOException
|
---|
138 | {
|
---|
139 | if (position < 0)
|
---|
140 | throw new IllegalArgumentException ();
|
---|
141 |
|
---|
142 | if (!isOpen ())
|
---|
143 | throw new ClosedChannelException ();
|
---|
144 |
|
---|
145 | // FIXME: check for NonReadableChannelException
|
---|
146 |
|
---|
147 | throw new Error ("Not implemented");
|
---|
148 | }
|
---|
149 |
|
---|
150 | public long read (ByteBuffer[] dsts, int offset, int length)
|
---|
151 | throws IOException
|
---|
152 | {
|
---|
153 | long result = 0;
|
---|
154 |
|
---|
155 | for (int i = offset; i < offset + length; i++)
|
---|
156 | {
|
---|
157 | result += write (dsts[i]);
|
---|
158 | }
|
---|
159 |
|
---|
160 | return result;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public int write (ByteBuffer src) throws IOException
|
---|
164 | {
|
---|
165 | int w = 0;
|
---|
166 |
|
---|
167 | if (buf == null)
|
---|
168 | {
|
---|
169 | throw new EOFException ("file not mapped");
|
---|
170 | }
|
---|
171 |
|
---|
172 | while (src.hasRemaining ())
|
---|
173 | {
|
---|
174 | buf.put (src.get ());
|
---|
175 | w++;
|
---|
176 | }
|
---|
177 |
|
---|
178 | return w;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public int write (ByteBuffer src, long position)
|
---|
182 | throws IOException
|
---|
183 | {
|
---|
184 | if (position < 0)
|
---|
185 | throw new IllegalArgumentException ();
|
---|
186 |
|
---|
187 | if (!isOpen ())
|
---|
188 | throw new ClosedChannelException ();
|
---|
189 |
|
---|
190 | // FIXME: check for NonWritableChannelException
|
---|
191 |
|
---|
192 | throw new Error ("Not implemented");
|
---|
193 | }
|
---|
194 |
|
---|
195 | public long write(ByteBuffer[] srcs, int offset, int length)
|
---|
196 | throws IOException
|
---|
197 | {
|
---|
198 | long res = 0;
|
---|
199 |
|
---|
200 | for (int i = offset;i < offset + length;i++)
|
---|
201 | {
|
---|
202 | res += write (srcs[i]);
|
---|
203 | }
|
---|
204 |
|
---|
205 | return res;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public MappedByteBuffer map (FileChannel.MapMode mode, long position,
|
---|
209 | long size)
|
---|
210 | throws IOException
|
---|
211 | {
|
---|
212 | if ((mode != MapMode.READ_ONLY
|
---|
213 | && mode != MapMode.READ_WRITE
|
---|
214 | && mode != MapMode.PRIVATE)
|
---|
215 | || position < 0
|
---|
216 | || size < 0
|
---|
217 | || size > Integer.MAX_VALUE)
|
---|
218 | throw new IllegalArgumentException ();
|
---|
219 |
|
---|
220 | // int cmode = mode.m;
|
---|
221 | // address = nio_mmap_file (fd, position, size, cmode);
|
---|
222 | // length = size;
|
---|
223 | // buf = new MappedByteFileBuffer (this);
|
---|
224 | // return buf;
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static MappedByteBuffer create_direct_mapped_buffer (long address,
|
---|
229 | long length)
|
---|
230 | {
|
---|
231 | // FileChannelImpl ch = new FileChannelImpl (-1, null);
|
---|
232 | // ch.address = address;
|
---|
233 | // ch.length = (int) length;
|
---|
234 | // ch.buf = new MappedByteFileBuffer (ch);
|
---|
235 | // return ch.buf;
|
---|
236 | return null;
|
---|
237 | }
|
---|
238 |
|
---|
239 | public long write (ByteBuffer[] srcs)
|
---|
240 | throws IOException
|
---|
241 | {
|
---|
242 | return write (srcs, 0, srcs.length);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * msync with the disk
|
---|
247 | */
|
---|
248 | public void force (boolean metaData) throws IOException
|
---|
249 | {
|
---|
250 | if (!isOpen ())
|
---|
251 | throw new ClosedChannelException ();
|
---|
252 |
|
---|
253 | // FIXME: What to do with metaData ?
|
---|
254 |
|
---|
255 | nio_msync (fd, address, length);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public long transferTo (long position, long count, WritableByteChannel target)
|
---|
259 | throws IOException
|
---|
260 | {
|
---|
261 | if (position < 0
|
---|
262 | || count < 0)
|
---|
263 | throw new IllegalArgumentException ();
|
---|
264 |
|
---|
265 | if (!isOpen ())
|
---|
266 | throw new ClosedChannelException ();
|
---|
267 |
|
---|
268 | // FIXME: check for NonReadableChannelException
|
---|
269 | // FIXME: check for NonWritableChannelException
|
---|
270 |
|
---|
271 | throw new Error ("Not implemented");
|
---|
272 | }
|
---|
273 |
|
---|
274 | public long transferFrom (ReadableByteChannel src, long position, long count)
|
---|
275 | throws IOException
|
---|
276 | {
|
---|
277 | if (position < 0
|
---|
278 | || count < 0)
|
---|
279 | throw new IllegalArgumentException ();
|
---|
280 |
|
---|
281 | if (!isOpen ())
|
---|
282 | throw new ClosedChannelException ();
|
---|
283 |
|
---|
284 | // FIXME: check for NonReadableChannelException
|
---|
285 | // FIXME: check for NonWritableChannelException
|
---|
286 |
|
---|
287 | throw new Error ("Not implemented");
|
---|
288 | }
|
---|
289 |
|
---|
290 | public FileLock lock (long position, long size, boolean shared)
|
---|
291 | throws IOException
|
---|
292 | {
|
---|
293 | if (position < 0
|
---|
294 | || size < 0)
|
---|
295 | throw new IllegalArgumentException ();
|
---|
296 |
|
---|
297 | if (!isOpen ())
|
---|
298 | throw new ClosedChannelException ();
|
---|
299 |
|
---|
300 | // FIXME: check for NonReadableChannelException
|
---|
301 | // FIXME: check for NonWritableChannelException
|
---|
302 |
|
---|
303 | throw new Error ("Not implemented");
|
---|
304 | }
|
---|
305 |
|
---|
306 | public FileLock tryLock (long position, long size, boolean shared)
|
---|
307 | throws IOException
|
---|
308 | {
|
---|
309 | if (position < 0
|
---|
310 | || size < 0)
|
---|
311 | throw new IllegalArgumentException ();
|
---|
312 |
|
---|
313 | if (!isOpen ())
|
---|
314 | throw new ClosedChannelException ();
|
---|
315 |
|
---|
316 | throw new Error ("Not implemented");
|
---|
317 | }
|
---|
318 |
|
---|
319 | public long position ()
|
---|
320 | throws IOException
|
---|
321 | {
|
---|
322 | if (!isOpen ())
|
---|
323 | throw new ClosedChannelException ();
|
---|
324 |
|
---|
325 | throw new Error ("not implemented");
|
---|
326 | }
|
---|
327 |
|
---|
328 | public FileChannel position (long newPosition)
|
---|
329 | throws IOException
|
---|
330 | {
|
---|
331 | if (newPosition < 0)
|
---|
332 | throw new IllegalArgumentException ();
|
---|
333 |
|
---|
334 | if (!isOpen ())
|
---|
335 | throw new ClosedChannelException ();
|
---|
336 |
|
---|
337 | throw new Error ("not implemented");
|
---|
338 | }
|
---|
339 |
|
---|
340 | public FileChannel truncate (long size)
|
---|
341 | throws IOException
|
---|
342 | {
|
---|
343 | if (size < 0)
|
---|
344 | throw new IllegalArgumentException ();
|
---|
345 |
|
---|
346 | if (!isOpen ())
|
---|
347 | throw new ClosedChannelException ();
|
---|
348 |
|
---|
349 | // FIXME: check for NonWritableChannelException
|
---|
350 |
|
---|
351 | throw new Error ("not implemented");
|
---|
352 | }
|
---|
353 |
|
---|
354 | private static native long nio_mmap_file (int fd, long pos, int size, int mode);
|
---|
355 | private static native void nio_unmmap_file (int fd, long address, int size);
|
---|
356 | private static native void nio_msync (int fd, long address, int length);
|
---|
357 | }
|
---|