📄 filechannel.java
字号:
* eventually be propagated to the file; they may or may not be made * visible to other programs that have mapped the same file. ({@link * MapMode#READ_WRITE MapMode.READ_WRITE}) </p></li> * * <li><p> <i>Private:</i> Changes made to the resulting buffer will not * be propagated to the file and will not be visible to other programs * that have mapped the same file; instead, they will cause private * copies of the modified portions of the buffer to be created. ({@link * MapMode#PRIVATE MapMode.PRIVATE}) </p></li> * * </ul> * * <p> For a read-only mapping, this channel must have been opened for * reading; for a read/write or private mapping, this channel must have * been opened for both reading and writing. * * <p> The {@link MappedByteBuffer </code>mapped byte buffer<code>} * returned by this method will have a position of zero and a limit and * capacity of <tt>size</tt>; its mark will be undefined. The buffer and * the mapping that it represents will remain valid until the buffer itself * is garbage-collected. * * <p> A mapping, once established, is not dependent upon the file channel * that was used to create it. Closing the channel, in particular, has no * effect upon the validity of the mapping. * * <p> Many of the details of memory-mapped files are inherently dependent * upon the underlying operating system and are therefore unspecified. The * behavior of this method when the requested region is not completely * contained within this channel's file is unspecified. Whether changes * made to the content or size of the underlying file, by this program or * another, are propagated to the buffer is unspecified. The rate at which * changes to the buffer are propagated to the file is unspecified. * * <p> For most operating systems, mapping a file into memory is more * expensive than reading or writing a few tens of kilobytes of data via * the usual {@link #read read} and {@link #write write} methods. From the * standpoint of performance it is generally only worth mapping relatively * large files into memory. </p> * * @param mode * One of the constants {@link MapMode#READ_ONLY READ_ONLY}, {@link * MapMode#READ_WRITE READ_WRITE}, or {@link MapMode#PRIVATE * PRIVATE} defined in the {@link MapMode} class, according to * whether the file is to be mapped read-only, read/write, or * privately (copy-on-write), respectively * * @param position * The position within the file at which the mapped region * is to start; must be non-negative * * @param size * The size of the region to be mapped; must be non-negative and * no greater than {@link java.lang.Integer#MAX_VALUE} * * @throws NonReadableChannelException * If the <tt>mode</tt> is {@link MapMode#READ_ONLY READ_ONLY} but * this channel was not opened for reading * * @throws NonWritableChannelException * If the <tt>mode</tt> is {@link MapMode#READ_WRITE READ_WRITE} or * {@link MapMode#PRIVATE PRIVATE} but this channel was not opened * for both reading and writing * * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * * @throws IOException * If some other I/O error occurs * * @see java.nio.channels.FileChannel.MapMode * @see java.nio.MappedByteBuffer */ public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException; // -- Locks -- /** * Acquires a lock on the given region of this channel's file. * * <p> An invocation of this method will block until the region can be * locked, this channel is closed, or the invoking thread is interrupted, * whichever comes first. * * <p> If this channel is closed by another thread during an invocation of * this method then an {@link AsynchronousCloseException} will be thrown. * * <p> If the invoking thread is interrupted while waiting to acquire the * lock then its interrupt status will be set and a {@link * FileLockInterruptionException} will be thrown. If the invoker's * interrupt status is set when this method is invoked then that exception * will be thrown immediately; the thread's interrupt status will not be * changed. * * <p> The region specified by the <tt>position</tt> and <tt>size</tt> * parameters need not be contained within, or even overlap, the actual * underlying file. Lock regions are fixed in size; if a locked region * initially contains the end of the file and the file grows beyond the * region then the new portion of the file will not be covered by the lock. * If a file is expected to grow in size and a lock on the entire file is * required then a region starting at zero, and no smaller than the * expected maximum size of the file, should be locked. The zero-argument * {@link #lock()} method simply locks a region of size {@link * Long#MAX_VALUE}. * * <p> Some operating systems do not support shared locks, in which case a * request for a shared lock is automatically converted into a request for * an exclusive lock. Whether the newly-acquired lock is shared or * exclusive may be tested by invoking the resulting lock object's {@link * FileLock#isShared() isShared} method. * * <p> File locks are held on behalf of the entire Java virtual machine. * They are not suitable for controlling access to a file by multiple * threads within the same virtual machine. </p> * * @param position * The position at which the locked region is to start; must be * non-negative * * @param size * The size of the locked region; must be non-negative, and the sum * <tt>position</tt> + <tt>size</tt> must be non-negative * * @param shared * <tt>true</tt> to request a shared lock, in which case this * channel must be open for reading (and possibly writing); * <tt>false</tt> to request an exclusive lock, in which case this * channel must be open for writing (and possibly reading) * * @return A lock object representing the newly-acquired lock * * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * * @throws ClosedChannelException * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel while the invoking * thread is blocked in this method * * @throws FileLockInterruptionException * If the invoking thread is interrupted while blocked in this * method * * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * region * * @throws NonReadableChannelException * If <tt>shared</tt> is <tt>true</tt> this channel was not * opened for reading * * @throws NonWritableChannelException * If <tt>shared</tt> is <tt>false</tt> but this channel was not * opened for writing * * @throws IOException * If some other I/O error occurs * * @see #lock() * @see #tryLock() * @see #tryLock(long,long,boolean) */ public abstract FileLock lock(long position, long size, boolean shared) throws IOException; /** * Acquires an exclusive lock on this channel's file. * * <p> An invocation of this method of the form <tt>fc.lock()</tt> behaves * in exactly the same way as the invocation * * <pre> * fc.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false) </pre> * * @return A lock object representing the newly-acquired lock * * @throws ClosedChannelException * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel while the invoking * thread is blocked in this method * * @throws FileLockInterruptionException * If the invoking thread is interrupted while blocked in this * method * * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * region of the same file * * @throws NonReadableChannelException * If <tt>shared</tt> is <tt>true</tt> this channel was not * opened for reading * * @throws NonWritableChannelException * If <tt>shared</tt> is <tt>false</tt> but this channel was not * opened for writing * * @throws IOException * If some other I/O error occurs * * @see #lock(long,long,boolean) * @see #tryLock() * @see #tryLock(long,long,boolean) */ public final FileLock lock() throws IOException { return lock(0L, Long.MAX_VALUE, false); } /** * Attempts to acquire a lock on the given region of this channel's file. * * <p> This method does not block. An invocation of this always returns * immediately, either having acquired a lock on the requested region or * having failed to do so. If it fails to acquire a lock because an * overlapping lock is held by another program then it returns * <tt>null</tt>. If it fails to acquire a lock for any other reason then * an appropriate exception is thrown. * * <p> The region specified by the <tt>position</tt> and <tt>size</tt> * parameters need not be contained within, or even overlap, the actual * underlying file. Lock regions are fixed in size; if a locked region * initially contains the end of the file and the file grows beyond the * region then the new portion of the file will not be covered by the lock. * If a file is expected to grow in size and a lock on the entire file is * required then a region starting at zero, and no smaller than the * expected maximum size of the file, should be locked. The zero-argument * {@link #tryLock()} method simply locks a region of size {@link * Long#MAX_VALUE}. * * <p> Some operating systems do not support shared locks, in which case a * request for a shared lock is automatically converted into a request for * an exclusive lock. Whether the newly-acquired lock is shared or * exclusive may be tested by invoking the resulting lock object's {@link * FileLock#isShared() isShared} method. * * <p> File locks are held on behalf of the entire Java virtual machine. * They are not suitable for controlling access to a file by multiple * threads within the same virtual machine. </p> * * @param position * The position at which the locked region is to start; must be * non-negative * * @param size * The size of the locked region; must be non-negative, and the sum * <tt>position</tt> + <tt>size</tt> must be non-negative * * @param shared * <tt>true</tt> to request a shared lock, * <tt>false</tt> to request an exclusive lock * * @return A lock object representing the newly-acquired lock, * or <tt>null</tt> if the lock could not be acquired * because another program holds an overlapping lock * * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * * @throws ClosedChannelException * If this channel is closed * * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * region of the same file * * @throws IOException * If some other I/O error occurs * * @see #lock() * @see #lock(long,long,boolean) * @see #tryLock() */ public abstract FileLock tryLock(long position, long size, boolean shared) throws IOException; /** * Attempts to acquire an exclusive lock on this channel's file. * * <p> An invocation of this method of the form <tt>fc.tryLock()</tt> * behaves in exactly the same way as the invocation * * <pre> * fc.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre> * * @return A lock object representing the newly-acquired lock, * or <tt>null</tt> if the lock could not be acquired * because another program holds an overlapping lock * * @throws ClosedChannelException * If this channel is closed * * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * region * * @throws IOException * If some other I/O error occurs * * @see #lock() * @see #lock(long,long,boolean) * @see #tryLock(long,long,boolean) */ public final FileLock tryLock() throws IOException { return tryLock(0L, Long.MAX_VALUE, false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -