easy-nio.file
Wrapper around java.nio.channels.FileChannel.
file-channel
(file-channel p & opts)Opens a FileChannel at p (a Path) with the given opts (StandardOpenOption constants).
Examples: (file-channel p opt-read) (file-channel p opt-write opt-create opt-truncate)
force!
(force! ch metadata?)Forces any updates to the file to be written to storage. If metadata? is true, file metadata (timestamps etc.) is also flushed. Returns ch.
force-map!
(force-map! buf)Flushes changes in a read-write mapped region to storage. No-op for read-only or copy-on-write mappings. Returns buf.
load!
(load! buf)Loads the mapped region into physical memory where possible. Returns buf.
loaded?
(loaded? buf)Returns true if the mapped region is likely to be resident in physical memory. This is only a hint and not guarantee.
lock!
(lock! ch)Acquires an exclusive lock on the entire file. Blocks until the lock is available. Returns a FileLock. Throws OverlappingFileLockException if the JVM already holds a lock that overlaps this region.
lock-region!
(lock-region! ch pos size shared?)Acquires a lock on a region of the file. Blocks until available. pos — starting byte position size — number of bytes to lock shared? — true for a shared lock, false for exclusive.
lock-valid?
(lock-valid? lock)Returns true if lock is valid. A lock object remains valid until it is released or the associated file channel is closed, whichever comes first.
mmap
(mmap ch mode pos size)Maps a region of ch into memory. mode is one of: map-mode-read-only — shared read-only mapping map-mode-read-write — shared read-write mapping map-mode-private — private (copy on write) mapping pos is the starting file position, size is the region size in bytes. Returns a MappedByteBuffer. All easy-nio.buffer functions apply to it directly.
mmap-private
(mmap-private ch pos size)Maps size bytes of ch starting at pos as a copy-on-write mapping. Writes affect only the in-memory copy, not the file.
mmap-read
(mmap-read ch pos size)Maps size bytes of ch starting at pos as a read-only mapping.
mmap-read-write
(mmap-read-write ch pos size)Maps size bytes of ch starting at pos as a read-write mapping.
read!
(read! ch buf)Reads bytes from ch into buf at the channel’s current position. Advances the position. Returns bytes read, or -1 at end-of-file.
read-at!
(read-at! ch buf pos)Reads bytes from ch into buf starting at absolute file position pos. Does not affect the channel’s current position.
transfer-from!
(transfer-from! ch src pos count)Transfers bytes from src (a ReadableByteChannel) into ch. Writes up to count bytes starting at pos in ch. May transfer fewer bytes than requested. Returns bytes transferred.
transfer-to!
(transfer-to! ch pos count dst)Transfers bytes from ch to dst (a WritableByteChannel). Reads count bytes starting at pos in ch. May transfer fewer bytes than requested. Returns bytes transferred.
truncate!
(truncate! ch size)Truncates the file to size bytes. If the current position exceeds size it is set to size. Returns ch.
try-lock!
(try-lock! ch)Attempts to acquire an exclusive lock on the entire file without blocking. Returns a FileLock if successful, nil if the lock is unavailable. Throws OverlappingFileLockException if the JVM already holds an overlapping lock.
try-lock-region!
(try-lock-region! ch pos size shared?)Attempts to acquire a lock on a region of the file without blocking. pos — starting byte position size — number of bytes to lock shared? — true for a shared lock, false for exclusive. Returns a FileLock if successful, nil if unavailable.
write!
(write! ch buf)Writes bytes from buf to ch at the channel’s current position. Advances the position. Returns bytes written.
write-at!
(write-at! ch buf pos)Writes bytes from buf to ch at absolute file position pos. Does not affect the channel’s current position.