Interface FileChannel
- All Superinterfaces:
AutoCloseable, ByteChannel, Channel, Closeable, ReadableByteChannel, SeekableByteChannel, WritableByteChannel
File system implementations can support locking and memory mapping by returning either a
FileChannel or a FileChannel from
FileSystem.newByteChannel(Path, Set, FileAttribute[]).
- Since:
- 25.5
-
Method Summary
Modifier and TypeMethodDescriptiondefault FileLocklock()Acquires an exclusive lock on this channel's file, blocking until the lock is acquired, this channel is closed, or the invoking thread is interrupted.default FileLocklock(long position, long size, boolean shared) Acquires a lock on a region of this channel's file, blocking until the lock is acquired, this channel is closed, or the invoking thread is interrupted.default Objectmap(FileChannel.MapMode mode, long offset, long size, Object arena) Maps a region of this channel's file into a memory segment associated with the given arena.position(long newPosition) truncate(long size) default FileLocktryLock()Attempts to acquire an exclusive lock on this channel's file without blocking.default FileLocktryLock(long position, long size, boolean shared) Attempts to acquire a lock on a region of this channel's file without blocking.Methods inherited from interface SeekableByteChannel
position, read, size, write
-
Method Details
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException- Since:
- 25.5
-
truncate
- Specified by:
truncatein interfaceSeekableByteChannel- Throws:
IOException- Since:
- 25.5
-
lock
Acquires an exclusive lock on this channel's file, blocking until the lock is acquired, this channel is closed, or the invoking thread is interrupted. This method is equivalent to invokinglock(0, Long.MAX_VALUE, false).- Returns:
- a newly acquired exclusive lock
- Throws:
ClosedChannelException- if this channel is closedAsynchronousCloseException- if another thread closes this channel while the invoking thread is blockedFileLockInterruptionException- if the invoking thread is interrupted while blockedOverlappingFileLockException- if an overlapping lock is already held by this Java virtual machine, or another thread is waiting for an overlapping lockNonWritableChannelException- if this channel was not opened for writingUnsupportedOperationException- if this channel does not support file lockingIOException- if another I/O error occurs- Since:
- 25.5
-
lock
Acquires a lock on a region of this channel's file, blocking until the lock is acquired, this channel is closed, or the invoking thread is interrupted. The region need not be contained within or overlap the current file contents. A requested shared lock may be converted to an exclusive lock when shared locks are not supported by the operating system.- Parameters:
position- the position of the first byte in the region; must be non-negativesize- the size of the region; must be non-negative andposition + sizemust not overflowshared-trueto request a shared lock, orfalseto request an exclusive lock- Returns:
- the newly acquired lock
- Throws:
IllegalArgumentException- ifpositionorsizeis negative or their sum overflowsClosedChannelException- if this channel is closedAsynchronousCloseException- if another thread closes this channel while the invoking thread is blockedFileLockInterruptionException- if the invoking thread is interrupted while blockedOverlappingFileLockException- if an overlapping lock is already held by this Java virtual machine, or another thread is waiting for an overlapping lockNonReadableChannelException- if a shared lock is requested and this channel was not opened for readingNonWritableChannelException- if an exclusive lock is requested and this channel was not opened for writingUnsupportedOperationException- if this channel does not support file lockingIOException- if another I/O error occurs- Since:
- 25.5
-
tryLock
Attempts to acquire an exclusive lock on this channel's file without blocking. This method is equivalent to invokingtryLock(0, Long.MAX_VALUE, false).- Returns:
- a newly acquired exclusive lock, or
nullif another program holds an overlapping lock - Throws:
ClosedChannelException- if this channel is closedOverlappingFileLockException- if an overlapping lock is already held by this Java virtual machine, or another thread is waiting for an overlapping lockNonWritableChannelException- if this channel was not opened for writingUnsupportedOperationException- if this channel does not support file lockingIOException- if another I/O error occurs- Since:
- 25.5
-
tryLock
Attempts to acquire a lock on a region of this channel's file without blocking. The region need not be contained within or overlap the current file contents. A requested shared lock may be converted to an exclusive lock when shared locks are not supported by the operating system.- Parameters:
position- the position of the first byte in the region; must be non-negativesize- the size of the region; must be non-negative andposition + sizemust not overflowshared-trueto request a shared lock, orfalseto request an exclusive lock- Returns:
- the newly acquired lock, or
nullif another program holds an overlapping lock - Throws:
IllegalArgumentException- ifpositionorsizeis negative or their sum overflowsClosedChannelException- if this channel is closedOverlappingFileLockException- if an overlapping lock is already held by this Java virtual machine, or another thread is waiting for an overlapping lockNonReadableChannelException- if a shared lock is requested and this channel was not opened for readingNonWritableChannelException- if an exclusive lock is requested and this channel was not opened for writingUnsupportedOperationException- if this channel does not support file lockingIOException- if another I/O error occurs- Since:
- 25.5
-
map
default Object map(FileChannel.MapMode mode, long offset, long size, Object arena) throws IOException Maps a region of this channel's file into a memory segment associated with the given arena.This channel must have been opened for reading for a read-only mapping and for both reading and writing for a read/write or private mapping. Closing this channel does not invalidate the returned segment. Closing
arenainvalidates the segment and permits the mapped region to be unmapped.The arena parameter and return type are intentionally erased to
Objectto avoid a static dependency on the Foreign Function and Memory API, which is not available on all JDK versions supported by this API. Thearenaargument must be ajava.lang.foreign.Arena, and the returned object is ajava.lang.foreign.MemorySegment. When JDK 22 or later becomes the minimum supported version, this method is expected to be deprecated and replaced by a strongly typed variant.- Parameters:
mode- the mode in which the file region is mappedoffset- the offset within the file at which the mapped region starts; must be non-negativesize- the size of the mapped region, in bytes; must be non-negativearena- thejava.lang.foreign.Arenaassociated with the returned segment- Returns:
- a
java.lang.foreign.MemorySegmentwhose lifetime is controlled byarena - Throws:
IOException- if an I/O error occursNonReadableChannelException- ifmoderequires read access and this channel was not opened for readingNonWritableChannelException- ifmoderequires write access and this channel was not opened for writingUnsupportedOperationException- if this channel does not support memory mapping or the requestedmodeIllegalArgumentException- ifoffsetorsizeis negative or their sum overflows the range oflongIllegalStateException- ifarenahas already been closedClassCastException- ifarenais not ajava.lang.foreign.ArenaRuntimeException- ifarenais confined and this method is called from a thread other than the arena's owner thread; on JDK 22 and later, the concrete exception isjava.lang.WrongThreadExceptionNullPointerException- ifmodeorarenaisnullSecurityException- if theFileSystemdenied the operation- Since:
- 25.5
-