FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / java / nio / FileChannelImpl.java
blob31779bbc64e78ba3939be0cd7ed1becf116f0c7c
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)
9 any later version.
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
19 02111-1307 USA.
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.
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. */
38 package gnu.java.nio;
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;
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.
63 public class FileChannelImpl extends FileChannel
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...
71 /**
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)
79 this.fd = fd;
80 this.file_obj = obj;
83 public long size () throws IOException
85 if (!isOpen ())
86 throw new ClosedChannelException ();
88 return lengthInternal (fd);
91 protected void implCloseChannel() throws IOException
93 if (address != 0)
95 nio_unmmap_file (fd, address, (int) length);
96 address = 0;
99 // FIXME
100 fd = 0;
102 if (file_obj instanceof RandomAccessFile)
104 RandomAccessFile o = (RandomAccessFile) file_obj;
105 o.close();
107 else if (file_obj instanceof FileInputStream)
109 FileInputStream o = (FileInputStream) file_obj;
110 o.close();
112 else if (file_obj instanceof FileOutputStream)
114 FileOutputStream o = (FileOutputStream) file_obj;
115 o.close();
119 public int read (ByteBuffer dst) throws IOException
121 int s = (int)size();
123 if (buf == null)
125 throw new EOFException("file not mapped");
128 for (int i=0; i<s; i++)
130 dst.put( buf.get() );
133 return s;
136 public int read (ByteBuffer dst, long position)
137 throws IOException
139 if (position < 0)
140 throw new IllegalArgumentException ();
142 if (!isOpen ())
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)
151 throws IOException
153 long result = 0;
155 for (int i = offset; i < offset + length; i++)
157 result += write (dsts[i]);
160 return result;
163 public int write (ByteBuffer src) throws IOException
165 int w = 0;
167 if (buf == null)
169 throw new EOFException ("file not mapped");
172 while (src.hasRemaining ())
174 buf.put (src.get ());
175 w++;
178 return w;
181 public int write (ByteBuffer src, long position)
182 throws IOException
184 if (position < 0)
185 throw new IllegalArgumentException ();
187 if (!isOpen ())
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)
196 throws IOException
198 long res = 0;
200 for (int i = offset;i < offset + length;i++)
202 res += write (srcs[i]);
205 return res;
208 public MappedByteBuffer map (FileChannel.MapMode mode, long position,
209 long size)
210 throws IOException
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 ();
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;
228 static MappedByteBuffer create_direct_mapped_buffer (long address,
229 long length)
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;
239 public long write (ByteBuffer[] srcs)
240 throws IOException
242 return write (srcs, 0, srcs.length);
246 * msync with the disk
248 public void force (boolean metaData) throws IOException
250 if (!isOpen ())
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)
259 throws IOException
261 if (position < 0
262 || count < 0)
263 throw new IllegalArgumentException ();
265 if (!isOpen ())
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)
275 throws IOException
277 if (position < 0
278 || count < 0)
279 throw new IllegalArgumentException ();
281 if (!isOpen ())
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)
291 throws IOException
293 if (position < 0
294 || size < 0)
295 throw new IllegalArgumentException ();
297 if (!isOpen ())
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)
307 throws IOException
309 if (position < 0
310 || size < 0)
311 throw new IllegalArgumentException ();
313 if (!isOpen ())
314 throw new ClosedChannelException ();
316 throw new Error ("Not implemented");
319 public long position ()
320 throws IOException
322 if (!isOpen ())
323 throw new ClosedChannelException ();
325 throw new Error ("not implemented");
328 public FileChannel position (long newPosition)
329 throws IOException
331 if (newPosition < 0)
332 throw new IllegalArgumentException ();
334 if (!isOpen ())
335 throw new ClosedChannelException ();
337 throw new Error ("not implemented");
340 public FileChannel truncate (long size)
341 throws IOException
343 if (size < 0)
344 throw new IllegalArgumentException ();
346 if (!isOpen ())
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);