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., 51 Franklin Street, Fifth Floor, 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. */
38 package java
.nio
.channels
;
40 import java
.io
.IOException
;
41 import java
.nio
.ByteBuffer
;
42 import java
.nio
.MappedByteBuffer
;
43 import java
.nio
.channels
.spi
.AbstractInterruptibleChannel
;
47 * @author Michael Koch
50 public abstract class FileChannel
extends AbstractInterruptibleChannel
51 implements ByteChannel
, GatheringByteChannel
, ScatteringByteChannel
53 public static class MapMode
56 public static final MapMode READ_ONLY
= new MapMode(0);
57 public static final MapMode READ_WRITE
= new MapMode(1);
58 public static final MapMode PRIVATE
= new MapMode(2);
61 * Initializes the MapMode.
69 * Returns a string representation of the <code>MapMode</code> object.
71 public String
toString()
73 if (this == READ_ONLY
)
75 else if (this == READ_WRITE
)
83 * Initializes the channel.
85 protected FileChannel()
90 * Maps the file into the memory.
92 * @exception IllegalArgumentException If the preconditions on the parameters
94 * @exception IOException If an I/O error occurs.
95 * @exception NonReadableChannelException If mode is READ_ONLY but this channel was
96 * not opened for reading.
97 * @exception NonWritableChannelException If mode is READ_WRITE or PRIVATE but this
98 * channel was not opened for writing.
100 public abstract MappedByteBuffer
map(MapMode mode
, long position
, long size
)
104 * Return the size of the file thus far
106 * @exception ClosedChannelException If this channel is closed.
108 public abstract long size() throws IOException
;
111 * Writes data to the channel.
113 * @exception IOException If an I/O error occurs.
115 public final long write(ByteBuffer
[] srcs
) throws IOException
119 for (int i
= 0; i
< srcs
.length
; i
++)
120 result
+= write(srcs
[i
]);
126 * Writes data to the channel.
128 * @exception IOException If an I/O error occurs.
130 public abstract int write(ByteBuffer src
) throws IOException
;
133 * Writes data to the channel.
135 * @exception AsynchronousCloseException If another thread closes this channel
136 * while the transfer is in progress.
137 * @exception ClosedByInterruptException If another thread interrupts the
138 * current thread while the transfer is in progress, thereby closing both
139 * channels and setting the current thread's interrupt status.
140 * @exception ClosedChannelException If this channel is closed.
141 * @exception IllegalArgumentException If position is negative.
142 * @exception IOException If an I/O error occurs.
143 * @exception NonWritableChannelException If this channel was not opened for
146 public abstract int write(ByteBuffer srcs
, long position
)
150 * Writes data to the channel.
152 * @exception IOException If an I/O error occurs.
154 public abstract long write(ByteBuffer
[] srcs
, int offset
, int length
)
158 * Reads data from the channel.
160 * @exception IOException If an I/O error occurs.
162 public abstract long read(ByteBuffer
[] dsts
, int offset
, int length
)
166 * Reads data from the channel.
168 * @exception IOException If an I/O error occurs.
170 public final long read(ByteBuffer
[] dsts
) throws IOException
174 for (int i
= 0; i
< dsts
.length
; i
++)
181 * Reads data from the channel.
183 * @exception IOException If an I/O error occurs.
185 public abstract int read(ByteBuffer dst
) throws IOException
;
188 * Reads data from the channel.
190 * @exception AsynchronousCloseException If another thread closes this channel
191 * while the transfer is in progress.
192 * @exception ClosedByInterruptException If another thread interrupts the
193 * current thread while the transfer is in progress, thereby closing both
194 * channels and setting the current thread's interrupt status.
195 * @exception ClosedChannelException If this channel is closed.
196 * @exception IllegalArgumentException If position is negative.
197 * @exception IOException If an I/O error occurs.
198 * @exception NonReadableChannelException If this channel was not opened for
201 public abstract int read(ByteBuffer dst
, long position
)
205 * Closes the channel.
207 * This is called from @see close.
209 * @exception IOException If an I/O error occurs.
211 protected abstract void implCloseChannel() throws IOException
;
214 * msync with the disk
216 * @exception ClosedChannelException If this channel is closed.
217 * @exception IOException If an I/O error occurs.
219 public abstract void force(boolean metaData
) throws IOException
;
222 * Creates a file lock for the whole assoziated file.
224 * @exception AsynchronousCloseException If another thread closes this channel
225 * while the transfer is in progress.
226 * @exception ClosedChannelException If this channel is closed.
227 * @exception FileLockInterruptionException If the invoking thread is
228 * interrupted while blocked in this method.
229 * @exception IOException If an I/O error occurs.
230 * @exception NonReadableChannelException If shared is true and this channel
231 * was not opened for reading.
232 * @exception NonWritableChannelException If shared is false and this channel
233 * was not opened for writing.
234 * @exception OverlappingFileLockException If a lock that overlaps the
235 * requested region is already held by this Java virtual machine, or if
236 * another thread is already blocked in this method and is attempting to lock
237 * an overlapping region.
239 public final FileLock
lock() throws IOException
241 return lock(0, Long
.MAX_VALUE
, false);
245 * Creates a file lock for a region of the assoziated file.
247 * @exception AsynchronousCloseException If another thread closes this channel
248 * while the transfer is in progress.
249 * @exception ClosedChannelException If this channel is closed.
250 * @exception FileLockInterruptionException If the invoking thread is
251 * interrupted while blocked in this method.
252 * @exception IllegalArgumentException If the preconditions on the parameters
254 * @exception IOException If an I/O error occurs.
255 * @exception OverlappingFileLockException If a lock that overlaps the
256 * requested region is already held by this Java virtual machine, or if
257 * another thread is already blocked in this method and is attempting to lock
258 * an overlapping region.
259 * @exception NonReadableChannelException If shared is true and this channel
260 * was not opened for reading.
261 * @exception NonWritableChannelException If shared is false and this channel
262 * was not opened for writing.
264 public abstract FileLock
lock(long position
, long size
, boolean shared
)
268 * Tries to aqquire alock on the whole assoziated file.
270 * @exception ClosedChannelException If this channel is closed.
271 * @exception IOException If an I/O error occurs.
272 * @exception OverlappingFileLockException If a lock that overlaps the
273 * requested region is already held by this Java virtual machine, or if
274 * another thread is already blocked in this method and is attempting to lock
275 * an overlapping region.
277 public final FileLock
tryLock() throws IOException
279 return tryLock(0, Long
.MAX_VALUE
, false);
283 * Tries to aqquire a lock on a region of the assoziated file.
285 * @exception ClosedChannelException If this channel is closed.
286 * @exception IllegalArgumentException If the preconditions on the parameters
288 * @exception IOException If an I/O error occurs.
289 * @exception OverlappingFileLockException If a lock that overlaps the
290 * requested region is already held by this Java virtual machine, or if
291 * another thread is already blocked in this method and is attempting to lock
292 * an overlapping region.
294 public abstract FileLock
tryLock(long position
, long size
, boolean shared
)
298 * Returns the current position on the file.
300 * @exception ClosedChannelException If this channel is closed.
301 * @exception IOException If an I/O error occurs.
303 public abstract long position() throws IOException
;
306 * Sets the position of the channel on the assoziated file.
308 * @exception ClosedChannelException If this channel is closed.
309 * @exception IllegalArgumentException If newPosition is negative.
310 * @exception IOException If an I/O error occurs.
312 public abstract FileChannel
position(long newPosition
)
316 * Transfers bytes from this channel's file to the given writable byte
319 * @exception AsynchronousCloseException If another thread closes this channel
320 * while the transfer is in progress.
321 * @exception ClosedByInterruptException If another thread interrupts the
322 * current thread while the transfer is in progress, thereby closing both
323 * channels and setting the current thread's interrupt status.
324 * @exception ClosedChannelException If this channel is closed.
325 * @exception IllegalArgumentException If the preconditions on the parameters
327 * @exception IOException If an I/O error occurs.
328 * @exception NonReadableChannelException If this channel was not opened for
330 * @exception NonWritableChannelException If the target channel was not
331 * opened for writing.
333 public abstract long transferTo(long position
, long count
,
334 WritableByteChannel target
)
338 * Transfers bytes from the given readable channel into this channel.
340 * @exception AsynchronousCloseException If another thread closes this channel
341 * while the transfer is in progress.
342 * @exception ClosedByInterruptException If another thread interrupts the
343 * current thread while the transfer is in progress, thereby closing both
344 * channels and setting the current thread's interrupt status.
345 * @exception ClosedChannelException If this channel is closed.
346 * @exception IllegalArgumentException If the preconditions on the parameters
348 * @exception IOException If an I/O error occurs.
349 * @exception NonReadableChannelException If the source channel was not
350 * opened for reading.
351 * @exception NonWritableChannelException If this channel was not opened for
354 public abstract long transferFrom(ReadableByteChannel src
, long position
,
355 long count
) throws IOException
;
358 * Truncates the channel's file at <code>size</code>.
360 * @exception ClosedChannelException If this channel is closed.
361 * @exception IllegalArgumentException If size is negative.
362 * @exception IOException If an I/O error occurs.
363 * @exception NonWritableChannelException If this channel was not opened for
366 public abstract FileChannel
truncate(long size
) throws IOException
;