source: trunk/gcc/libjava/java/nio/channels/FileChannel.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.8 KB
Line 
1/* FileChannel.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.nio.channels;
39
40import java.io.IOException;
41import java.nio.ByteBuffer;
42import java.nio.MappedByteBuffer;
43import java.nio.channels.spi.AbstractInterruptibleChannel;
44
45/**
46 * @author Michael Koch
47 * @since 1.4
48 */
49public abstract class FileChannel extends AbstractInterruptibleChannel
50 implements ByteChannel, GatheringByteChannel, ScatteringByteChannel
51{
52 public static class MapMode
53 {
54 public int m;
55
56 public static MapMode READ_ONLY = new MapMode(0);
57 public static MapMode READ_WRITE = new MapMode(1);
58 public static MapMode PRIVATE = new MapMode(2);
59
60 /**
61 * Initializes the MapMode.
62 */
63 MapMode(int a)
64 {
65 m = a;
66 }
67
68 /**
69 * Returns a string representation of the <code>MapMode</code> object.
70 */
71 public String toString()
72 {
73 if (this == READ_ONLY)
74 return "READ_ONLY";
75 else if (this == READ_WRITE)
76 return "READ_WRITE";
77
78 return "PRIVATE";
79 }
80 }
81
82 /**
83 * Initializes the channel.
84 */
85 protected FileChannel ()
86 {
87 }
88
89 /**
90 * Maps the file into the memory.
91 *
92 * @exception IllegalArgumentException If the preconditions on the parameters
93 * do not hold.
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.
99 */
100 public abstract MappedByteBuffer map(MapMode mode, long position, long size)
101 throws IOException;
102
103 /**
104 * Return the size of the file thus far
105 *
106 * @exception ClosedChannelException If this channel is closed.
107 */
108 public abstract long size() throws IOException;
109
110 /**
111 * Writes data to the channel.
112 *
113 * @exception IOException If an I/O error occurs.
114 */
115 public long write (ByteBuffer[] srcs) throws IOException
116 {
117 long result = 0;
118
119 for (int i = 0; i < srcs.length; i++)
120 {
121 result += write (srcs[i]);
122 }
123
124 return result;
125 }
126
127 /**
128 * Writes data to the channel.
129 *
130 * @exception IOException If an I/O error occurs.
131 */
132 public abstract int write (ByteBuffer src) throws IOException;
133
134 /**
135 * Writes data to the channel.
136 *
137 * @exception AsynchronousCloseException If another thread closes this channel
138 * while the transfer is in progress.
139 * @exception ClosedByInterruptException If another thread interrupts the
140 * current thread while the transfer is in progress, thereby closing both
141 * channels and setting the current thread's interrupt status.
142 * @exception ClosedChannelException If this channel is closed.
143 * @exception IllegalArgumentException If position is negative.
144 * @exception IOException If an I/O error occurs.
145 * @exception NonWritableChannelException If this channel was not opened for
146 * writing.
147 */
148 public abstract int write (ByteBuffer srcs, long position) throws IOException;
149
150 /**
151 * Writes data to the channel.
152 *
153 * @exception IOException If an I/O error occurs.
154 */
155 public abstract long write(ByteBuffer[] srcs, int offset, int length)
156 throws IOException;
157
158 /**
159 * Reads data from the channel.
160 *
161 * @exception IOException If an I/O error occurs.
162 */
163 public abstract long read (ByteBuffer[] dsts, int offset, int length)
164 throws IOException;
165
166 /**
167 * Reads data from the channel.
168 *
169 * @exception IOException If an I/O error occurs.
170 */
171 public final long read (ByteBuffer[] dsts) throws IOException
172 {
173 long result = 0;
174
175 for (int i = 0; i < dsts.length; i++)
176 {
177 read (dsts [i]);
178 }
179
180 return result;
181 }
182
183 /**
184 * Reads data from the channel.
185 *
186 * @exception IOException If an I/O error occurs.
187 */
188 public abstract int read(ByteBuffer dst) throws IOException;
189
190 /**
191 * Reads data from the channel.
192 *
193 * @exception AsynchronousCloseException If another thread closes this channel
194 * while the transfer is in progress.
195 * @exception ClosedByInterruptException If another thread interrupts the
196 * current thread while the transfer is in progress, thereby closing both
197 * channels and setting the current thread's interrupt status.
198 * @exception ClosedChannelException If this channel is closed.
199 * @exception IllegalArgumentException If position is negative.
200 * @exception IOException If an I/O error occurs.
201 * @exception NonReadableChannelException If this channel was not opened for
202 * reading.
203 */
204 public abstract int read(ByteBuffer dst, long position) throws IOException;
205
206 /**
207 * Closes the channel.
208 *
209 * This is called from @see close.
210 *
211 * @exception IOException If an I/O error occurs.
212 */
213 protected abstract void implCloseChannel() throws IOException;
214
215 /**
216 * msync with the disk
217 *
218 * @exception ClosedChannelException If this channel is closed.
219 * @exception IOException If an I/O error occurs.
220 */
221 public abstract void force(boolean metaData) throws IOException;
222
223 /**
224 * Creates a file lock for the whole assoziated file.
225 *
226 * @exception AsynchronousCloseException If another thread closes this channel
227 * while the transfer is in progress.
228 * @exception ClosedChannelException If this channel is closed.
229 * @exception FileLockInterruptionException If the invoking thread is
230 * interrupted while blocked in this method.
231 * @exception IOException If an I/O error occurs.
232 * @exception NonReadableChannelException If shared is true and this channel
233 * was not opened for reading.
234 * @exception NonWritableChannelException If shared is false and this channel
235 * was not opened for writing.
236 * @exception OverlappingFileLockException If a lock that overlaps the
237 * requested region is already held by this Java virtual machine, or if
238 * another thread is already blocked in this method and is attempting to lock
239 * an overlapping region.
240 */
241 public final FileLock lock () throws IOException
242 {
243 return lock (0, Long.MAX_VALUE, false);
244 }
245
246 /**
247 * Creates a file lock for a region of the assoziated file.
248 *
249 * @exception AsynchronousCloseException If another thread closes this channel
250 * while the transfer is in progress.
251 * @exception ClosedChannelException If this channel is closed.
252 * @exception FileLockInterruptionException If the invoking thread is
253 * interrupted while blocked in this method.
254 * @exception IllegalArgumentException If the preconditions on the parameters
255 * do not hold.
256 * @exception IOException If an I/O error occurs.
257 * @exception OverlappingFileLockException If a lock that overlaps the
258 * requested region is already held by this Java virtual machine, or if
259 * another thread is already blocked in this method and is attempting to lock
260 * an overlapping region.
261 * @exception NonReadableChannelException If shared is true and this channel
262 * was not opened for reading.
263 * @exception NonWritableChannelException If shared is false and this channel
264 * was not opened for writing.
265 */
266 public abstract FileLock lock (long position, long size, boolean shared)
267 throws IOException;
268
269 /**
270 * Tries to aqquire alock on the whole assoziated file.
271 *
272 * @exception ClosedChannelException If this channel is closed.
273 * @exception IOException If an I/O error occurs.
274 * @exception OverlappingFileLockException If a lock that overlaps the
275 * requested region is already held by this Java virtual machine, or if
276 * another thread is already blocked in this method and is attempting to lock
277 * an overlapping region.
278 */
279 public final FileLock tryLock () throws IOException
280 {
281 return tryLock (0, Long.MAX_VALUE, false);
282 }
283
284 /**
285 * Tries to aqquire a lock on a region of the assoziated file.
286 *
287 * @exception ClosedChannelException If this channel is closed.
288 * @exception IllegalArgumentException If the preconditions on the parameters
289 * do not hold.
290 * @exception IOException If an I/O error occurs.
291 * @exception OverlappingFileLockException If a lock that overlaps the
292 * requested region is already held by this Java virtual machine, or if
293 * another thread is already blocked in this method and is attempting to lock
294 * an overlapping region.
295 */
296 public abstract FileLock tryLock (long position, long size, boolean shared)
297 throws IOException;
298
299 /**
300 * Returns the current position on the file.
301 *
302 * @exception ClosedChannelException If this channel is closed.
303 * @exception IOException If an I/O error occurs.
304 */
305 public abstract long position () throws IOException;
306
307 /**
308 * Sets the position of the channel on the assoziated file.
309 *
310 * @exception ClosedChannelException If this channel is closed.
311 * @exception IllegalArgumentException If newPosition is negative.
312 * @exception IOException If an I/O error occurs.
313 */
314 public abstract FileChannel position (long newPosition) throws IOException;
315
316 /**
317 * Transfers bytes from this channel's file to the given writable byte
318 * channel.
319 *
320 * @exception AsynchronousCloseException If another thread closes this channel
321 * while the transfer is in progress.
322 * @exception ClosedByInterruptException If another thread interrupts the
323 * current thread while the transfer is in progress, thereby closing both
324 * channels and setting the current thread's interrupt status.
325 * @exception ClosedChannelException If this channel is closed.
326 * @exception IllegalArgumentException If the preconditions on the parameters
327 * do not hold.
328 * @exception IOException If an I/O error occurs.
329 * @exception NonReadableChannelException If this channel was not opened for
330 * reading.
331 * @exception NonWritableChannelException If the target channel was not
332 * opened for writing.
333 */
334 public abstract long transferTo (long position, long count,
335 WritableByteChannel target)
336 throws IOException;
337
338 /**
339 * Transfers bytes from the given readable channel into this channel.
340 *
341 * @exception AsynchronousCloseException If another thread closes this channel
342 * while the transfer is in progress.
343 * @exception ClosedByInterruptException If another thread interrupts the
344 * current thread while the transfer is in progress, thereby closing both
345 * channels and setting the current thread's interrupt status.
346 * @exception ClosedChannelException If this channel is closed.
347 * @exception IllegalArgumentException If the preconditions on the parameters
348 * do not hold.
349 * @exception IOException If an I/O error occurs.
350 * @exception NonReadableChannelException If the source channel was not
351 * opened for reading.
352 * @exception NonWritableChannelException If this channel was not opened for
353 * writing.
354 */
355 public abstract long transferFrom (ReadableByteChannel src, long position,
356 long count) throws IOException;
357
358 /**
359 * Truncates the channel's file at <code>size</code>.
360 *
361 * @exception ClosedChannelException If this channel is closed.
362 * @exception IllegalArgumentException If size is negative.
363 * @exception IOException If an I/O error occurs.
364 * @exception NonWritableChannelException If this channel was not opened for
365 * writing.
366 */
367 public abstract FileChannel truncate (long size) throws IOException;
368}
Note: See TracBrowser for help on using the repository browser.