1 /* FileChannelImpl.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
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
;
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.
63 public class FileChannelImpl
extends FileChannel
68 public MappedByteBuffer buf
;
69 public Object file_obj
; // just to keep it live...
72 * This method came from java.io.RandomAccessFile
73 * It is private there so we will repeat it here.
75 private native long lengthInternal (int native_fd
) throws IOException
;
77 public FileChannelImpl (int fd
, Object obj
)
83 public long size () throws IOException
86 throw new ClosedChannelException ();
88 return lengthInternal (fd
);
91 protected void implCloseChannel() throws IOException
95 nio_unmmap_file (fd
, address
, (int) length
);
102 if (file_obj
instanceof RandomAccessFile
)
104 RandomAccessFile o
= (RandomAccessFile
) file_obj
;
107 else if (file_obj
instanceof FileInputStream
)
109 FileInputStream o
= (FileInputStream
) file_obj
;
112 else if (file_obj
instanceof FileOutputStream
)
114 FileOutputStream o
= (FileOutputStream
) file_obj
;
119 public int read (ByteBuffer dst
) throws IOException
125 throw new EOFException("file not mapped");
128 for (int i
=0; i
<s
; i
++)
130 dst
.put( buf
.get() );
136 public int read (ByteBuffer dst
, long position
)
140 throw new IllegalArgumentException ();
143 throw new ClosedChannelException ();
145 // FIXME: check for NonReadableChannelException
147 throw new Error ("Not implemented");
150 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
155 for (int i
= offset
; i
< offset
+ length
; i
++)
157 result
+= write (dsts
[i
]);
163 public int write (ByteBuffer src
) throws IOException
169 throw new EOFException ("file not mapped");
172 while (src
.hasRemaining ())
174 buf
.put (src
.get ());
181 public int write (ByteBuffer src
, long position
)
185 throw new IllegalArgumentException ();
188 throw new ClosedChannelException ();
190 // FIXME: check for NonWritableChannelException
192 throw new Error ("Not implemented");
195 public long write(ByteBuffer
[] srcs
, int offset
, int length
)
200 for (int i
= offset
;i
< offset
+ length
;i
++)
202 res
+= write (srcs
[i
]);
208 public MappedByteBuffer
map (FileChannel
.MapMode mode
, long position
,
212 if ((mode
!= MapMode
.READ_ONLY
213 && mode
!= MapMode
.READ_WRITE
214 && mode
!= MapMode
.PRIVATE
)
217 || size
> Integer
.MAX_VALUE
)
218 throw new IllegalArgumentException ();
220 // int cmode = mode.m;
221 // address = nio_mmap_file (fd, position, size, cmode);
223 // buf = new MappedByteFileBuffer (this);
228 static MappedByteBuffer
create_direct_mapped_buffer (long address
,
231 // FileChannelImpl ch = new FileChannelImpl (-1, null);
232 // ch.address = address;
233 // ch.length = (int) length;
234 // ch.buf = new MappedByteFileBuffer (ch);
239 public long write (ByteBuffer
[] srcs
)
242 return write (srcs
, 0, srcs
.length
);
246 * msync with the disk
248 public void force (boolean metaData
) throws IOException
251 throw new ClosedChannelException ();
253 // FIXME: What to do with metaData ?
255 nio_msync (fd
, address
, length
);
258 public long transferTo (long position
, long count
, WritableByteChannel target
)
263 throw new IllegalArgumentException ();
266 throw new ClosedChannelException ();
268 // FIXME: check for NonReadableChannelException
269 // FIXME: check for NonWritableChannelException
271 throw new Error ("Not implemented");
274 public long transferFrom (ReadableByteChannel src
, long position
, long count
)
279 throw new IllegalArgumentException ();
282 throw new ClosedChannelException ();
284 // FIXME: check for NonReadableChannelException
285 // FIXME: check for NonWritableChannelException
287 throw new Error ("Not implemented");
290 public FileLock
lock (long position
, long size
, boolean shared
)
295 throw new IllegalArgumentException ();
298 throw new ClosedChannelException ();
300 // FIXME: check for NonReadableChannelException
301 // FIXME: check for NonWritableChannelException
303 throw new Error ("Not implemented");
306 public FileLock
tryLock (long position
, long size
, boolean shared
)
311 throw new IllegalArgumentException ();
314 throw new ClosedChannelException ();
316 throw new Error ("Not implemented");
319 public long position ()
323 throw new ClosedChannelException ();
325 throw new Error ("not implemented");
328 public FileChannel
position (long newPosition
)
332 throw new IllegalArgumentException ();
335 throw new ClosedChannelException ();
337 throw new Error ("not implemented");
340 public FileChannel
truncate (long size
)
344 throw new IllegalArgumentException ();
347 throw new ClosedChannelException ();
349 // FIXME: check for NonWritableChannelException
351 throw new Error ("not implemented");
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
);