class MFile
Overview
Memory mapped file
If no #capacity is given the file is open in read only mode and
#capacity is set to the current file size.
If the process crashes the file will be #capacity large,
not #size large, only on graceful close is the file truncated to its #size.
The file does not expand further than initial #capacity, unless manually expanded.
Defined in:
lavinmq/mfile.crstdlib/copy_file_range.cr
Constructors
-
.new(path : String, capacity : Int | Nil = nil, writeonly : Bool = false)
Map a file, if no capacity is given the file must exists and the file will be mapped as readonly The file won't be truncated if the capacity is smaller than current size
Class Method Summary
Instance Method Summary
- #advise(advice : Advice, addr = @buffer, length = @capacity) : Nil
- #capacity : Int64
-
#close(truncate_to_size = true)
The file will be truncated to the current position unless readonly or deleted
-
#closed?
Returns
trueif thisIOis closed. - #copy_file_range(src : File, limit : Int)
- #copy_file_range(src : MFile, limit : Int)
- #delete(*, raise_on_missing = true) : Nil
- #dontneed
-
#flush
Flushes buffered data, if any.
-
#inspect(io : IO)
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
- #msync
- #path : String
-
#pos : Int64
Returns the current position (in bytes) in this
IO. -
#pos=(pos)
Sets the current position (in bytes) in this
IO. -
#read(slice : Bytes)
Reads at most slice.size bytes from this
IOinto slice. -
#read_at(pos, slice)
Read from a specific position in the file
- #rename(new_path : String) : Nil
-
#resize(new_size : Int) : Nil
Resize the file, so that read operations can't happen beyond
new_sizethis does not change the actual file size on disk and you can still write beyondnew_size -
#rewind
Rewinds this
IO. - #seek(offset : Int, whence : IO::Seek = IO::Seek::Set)
- #size : Int64
-
#skip(bytes_count : Int) : Int
Reads and discards exactly bytes_count bytes.
- #to_slice(pos, size)
- #to_slice
-
#truncate(new_capacity) : Nil
Truncate the file to the given capacity (contracting only, no expansion) The truncated part is unmapped from memory
-
#write(slice : Bytes) : Nil
Append only
Class methods inherited from class IO
copy(src, dst, limit : Int) : Int64copy(src, dst) : Int64 copy
Constructor Detail
Map a file, if no capacity is given the file must exists and the file will be mapped as readonly The file won't be truncated if the capacity is smaller than current size
Class Method Detail
Instance Method Detail
The file will be truncated to the current position unless readonly or deleted
Returns true if this IO is closed.
IO defines returns false, but including types may override.
Flushes buffered data, if any.
IO defines this is a no-op method, but including types may override.
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Returns the current position (in bytes) in this IO.
The IO class raises on this method, but some subclasses, notable
IO::FileDescriptor and IO::Memory implement it.
File.write("testfile", "hello")
file = File.new("testfile")
file.pos # => 0
file.gets(2) # => "he"
file.pos # => 2
Sets the current position (in bytes) in this IO.
The IO class raises on this method, but some subclasses, notable
IO::FileDescriptor and IO::Memory implement it.
File.write("testfile", "hello")
file = File.new("testfile")
file.pos = 3
file.gets_to_end # => "lo"
Reads at most slice.size bytes from this IO into slice.
Returns the number of bytes read, which is 0 if and only if there is no
more data to read (so checking for 0 is the way to detect end of file).
io = IO::Memory.new "hello"
slice = Bytes.new(4)
io.read(slice) # => 4
slice # => Bytes[104, 101, 108, 108]
io.read(slice) # => 1
slice # => Bytes[111, 101, 108, 108]
io.read(slice) # => 0
Resize the file, so that read operations can't happen beyond new_size
this does not change the actual file size on disk
and you can still write beyond new_size
Rewinds this IO. By default this method raises, but including types
may implement it.
Reads and discards exactly bytes_count bytes.
Raises IO::EOFError if there aren't at least bytes_count bytes.
io = IO::Memory.new "hello world"
io.skip(6)
io.gets # => "world"
io.skip(1) # raises IO::EOFError
Truncate the file to the given capacity (contracting only, no expansion) The truncated part is unmapped from memory