class
RemiLib::MmappedFile
- RemiLib::MmappedFile
- IO
- Reference
- Object
Overview
The MmappedFile class provides a convenient way to read from and write to
files using mmap(). This is only
supported on POSIX systems, and is not meant to be a complete interface to
everything you can do with mmap().
Defined in:
remilib/mmappedfile.crConstructors
-
.new(path : Path | String, size : Int, *, mode : Mode = Mode::Read, mapping : Mapping = Mapping::Private, syncMode : MsyncFlags = MsyncFlags::Async)
Creates a new
MmappedFileinstance by mapping the file at path into the memory space.
Class Method Summary
-
.open(path : Path | String, size : Int, *, mode : Mode = Mode::Read, mapping : Mapping = Mapping::Private, &)
Creates a new
MmappedFileinstance by mapping the file at path into the memory spacem, then yields that instance and executes the block.
Instance Method Summary
-
#close : Nil
Unmaps the file from memory.
-
#fd : Int64
The file descriptor this mapping was created from.
- #finalize
-
#getBytes(count : Int32, position : LibC::SizeT) : Bytes
Gets count bytes from the given position, then returns a new
Bytesslice containing those bytes. -
#getInt128(position : LibC::SizeT) : Int128
Returns an
Int128from the file at the given position. -
#getInt16(position : LibC::SizeT) : Int16
Returns an
Int16from the file at the given position. -
#getInt24(position : LibC::SizeT) : Int32
Gets three bytes from position and returns an
Int32. -
#getInt32(position : LibC::SizeT) : Int32
Returns an
Int32from the file at the given position. -
#getInt64(position : LibC::SizeT) : Int64
Returns an
Int64from the file at the given position. -
#getInt8(position : LibC::SizeT) : Int8
Returns an
Int8from the file at the given position. - #getString(count : Int32, position : LibC::SizeT) : String
-
#getUInt128(position : LibC::SizeT) : UInt128
Returns a
UInt128from the file at the given position. -
#getUInt16(position : LibC::SizeT) : UInt16
Returns a
UInt16from the file at the given position. -
#getUInt24(position : LibC::SizeT) : UInt32
Gets three bytes from position and returns a
UInt32. -
#getUInt32(position : LibC::SizeT) : UInt32
Returns a
UInt32from the file at the given position. -
#getUInt64(position : LibC::SizeT) : UInt64
Returns a
UInt64from the file at the given position. -
#getUInt8(position : LibC::SizeT) : UInt8
Returns a
UInt8from the file at the given position. -
#len : LibC::SizeT
Length of the file.
-
#mapping : Mapping
The
Mappingthis instance was created with. -
#mode : Mode
The
Modethis instance was opened with. -
#open? : Bool
Returns
trueif this instance is open, orfalseotherwise. -
#pos : LibC::SizeT
Current read position.
- #pos=(position : LibC::SizeT) : Nil
-
#pos=(value)
Sets the current position (in bytes) in this
IO. -
#putBytes(data : Bytes, position : LibC::SizeT) : Nil
Writes a
Bytesslice to the file at the given position. -
#putInt128(val : Int128, position : LibC::SizeT) : Nil
Writes an
Int128to the file at the given position. -
#putInt16(val : Int16, position : LibC::SizeT) : Nil
Writes an
Int16to the file at the given position. -
#putInt24(val : Int32, position : LibC::SizeT) : Nil
Writes a 24-bit integer stored in a
UInt32to the file at the given position. -
#putInt32(val : Int32, position : LibC::SizeT) : Nil
Writes an
Int32to the file at the given position. -
#putInt64(val : Int64, position : LibC::SizeT) : Nil
Writes an
Int64to the file at the given position. -
#putInt8(val : Int8, position : LibC::SizeT) : Nil
Writes an
Int8to the file at the given position. -
#putString(str : String, position : LibC::SizeT) : Nil
Writes a
Stringas raw bytes to the file at the given position. -
#putUInt128(val : UInt128, position : LibC::SizeT) : Nil
Writes a
UInt128to the file at the given position. -
#putUInt16(val : UInt16, position : LibC::SizeT) : Nil
Writes a
UInt16to the file at the given position. -
#putUInt24(val : UInt32, position : LibC::SizeT, littleEndian? : Bool = true) : Nil
Writes a 24-bit integer stored in a
UInt32to the file at the given position. -
#putUInt32(val : UInt32, position : LibC::SizeT) : Nil
Writes a
UInt32to the file at the given position. -
#putUInt64(val : UInt64, position : LibC::SizeT) : Nil
Writes a
UInt64to the file at the given position. -
#putUInt8(val : UInt8, position : LibC::SizeT) : Nil
Writes a
UInt8to the file at the given position. -
#read(slice : Bytes)
Reads at most slice.size bytes from this
IOinto slice. -
#readBytes(count : Int32) : Bytes
Reads count bytes, then returns a new
Bytesslice containing those bytes. -
#readInt128 : Int128
Reads an
Int128from the file at the current position. -
#readInt16 : Int16
Reads a
Int16from the file at the current position. -
#readInt24 : Int32
Reads three bytes from the file at the current position and returns a 24-bit integer as an
Int32. -
#readInt32 : Int32
Reads an
Int32from the file at the current position. -
#readInt64 : Int64
Reads an
Int64from the file at the current position. -
#readInt8 : Int8
Reads a
Int8from the file at the current position. - #readString(count : Int32) : String
-
#readUInt128 : UInt128
Reads a
UInt128from the file at the current position. -
#readUInt16 : UInt16
Reads a
UInt16from the file at the current position. -
#readUInt24 : UInt32
Reads three bytes from the file at the current position and returns a 24-bit integer as a
UInt32. -
#readUInt32 : UInt32
Reads a
UInt32from the file at the current position. -
#readUInt64 : UInt64
Reads a
UInt64from the file at the current position. -
#readUInt8 : UInt8
Reads a
UInt8from the file at the current position. -
#rewind : Nil
Rewinds this
IO. - #sync : Nil
- #syncMode : MsyncFlags
- #syncMode=(syncMode : MsyncFlags)
-
#write(slice : Bytes) : Nil
Writes the contents of slice into this
IO. -
#writeBytes(data : Bytes) : Nil
Writes a
Bytesslice to the file at the current position. -
#writeInt128(val : Int128) : Nil
Writes an
Int128to the file at the current position. -
#writeInt16(val : Int16) : Nil
Writes an
Int16to the file at the current position. -
#writeInt24(val : Int32) : Nil
Writes a 24-bit integer stored in an
Int32to the file at the current position. -
#writeInt32(val : Int32) : Nil
Writes an
Int32to the file at the current position. -
#writeInt64(val : Int64) : Nil
Writes an
Int64to the file at the current position. -
#writeInt8(val : Int8) : Nil
Writes an
Int8to the file at the current position. -
#writeString(str : String) : Nil
Writes a
Stringas raw bytes to the file at the current position. -
#writeUInt128(val : UInt128) : Nil
Writes a
UInt128to the file at the current position. -
#writeUInt16(val : UInt16) : Nil
Writes a
UInt16to the file at the current position. -
#writeUInt24(val : UInt32, littleEndian? : Bool = true) : Nil
Writes a 24-bit integer stored in a
UInt32to the file at the current position. -
#writeUInt32(val : UInt32) : Nil
Writes a
UInt32to the file at the current position. -
#writeUInt64(val : UInt64) : Nil
Writes a
UInt64to the file at the current position. -
#writeUInt8(val : UInt8) : Nil
Writes a
UInt8to the file at the current position.
Instance methods inherited from class IO
endBold : self
endBold,
endColor : self
endColor,
endConceal : self
endConceal,
endCrossedOut : self
endCrossedOut,
endEncircled : self
endEncircled,
endFaint : self
endFaint,
endFastBlink : self
endFastBlink,
endFraktur : self
endFraktur,
endFramed : self
endFramed,
endItalic : self
endItalic,
endOverlined : self
endOverlined,
endReverseVideo : self
endReverseVideo,
endSlowBlink : self
endSlowBlink,
endUnderline : self
endUnderline,
readBytes(count : Int) : Bytes
readBytes,
readFloat32
readFloat32,
readFloat32BE
readFloat32BE,
readFloat64
readFloat64,
readFloat64BE
readFloat64BE,
readInt128
readInt128,
readInt128BE
readInt128BE,
readInt16
readInt16,
readInt16BE
readInt16BE,
readInt24
readInt24,
readInt24BE
readInt24BE,
readInt32
readInt32,
readInt32BE
readInt32BE,
readInt64
readInt64,
readInt64BE
readInt64BE,
readInt8
readInt8,
readUInt128
readUInt128,
readUInt128BE
readUInt128BE,
readUInt16
readUInt16,
readUInt16BE
readUInt16BE,
readUInt32
readUInt32,
readUInt32BE
readUInt32BE,
readUInt64
readUInt64,
readUInt64BE
readUInt64BE,
readUInt8
readUInt8,
startBold : self
startBold,
startColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default) : self
startColor,
startConceal : self
startConceal,
startCrossedOut : self
startCrossedOut,
startEncircled : self
startEncircled,
startFaint : self
startFaint,
startFastBlink : self
startFastBlink,
startFraktur : self
startFraktur,
startFramed : self
startFramed,
startItalic : self
startItalic,
startOverlined : self
startOverlined,
startReverseVideo : self
startReverseVideo,
startSlowBlink : self
startSlowBlink,
startUnderline : self
startUnderline,
withBold(&)
withBold,
withColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default, &) : self
withColor,
withConceal(&)
withConceal,
withCrossedOut(&)
withCrossedOut,
withEncircled(&)
withEncircled,
withExcursion(gotoHere, &)withExcursion(&) withExcursion, withFaint(&) withFaint, withFastBlink(&) withFastBlink, withFraktur(&) withFraktur, withFramed(&) withFramed, withItalic(&) withItalic, withOverlined(&) withOverlined, withReverseVideo(&) withReverseVideo, withSlowBlink(&) withSlowBlink, withUnderline(&) withUnderline, writeFloat32(value : Float32) : self writeFloat32, writeFloat32BE(value : Float32) : self writeFloat32BE, writeFloat64(value : Float64) : self writeFloat64, writeFloat64BE(value : Float64) : self writeFloat64BE, writeInt128(value : Int128) : self writeInt128, writeInt128BE(value : Int128) : self writeInt128BE, writeInt16(value : Int16) : self writeInt16, writeInt16BE(value : Int16) : self writeInt16BE, writeInt24(data : Int32) writeInt24, writeInt24BE(data : Int32) writeInt24BE, writeInt32(value : Int32) : self writeInt32, writeInt32BE(value : Int32) : self writeInt32BE, writeInt64(value : Int64) : self writeInt64, writeInt64BE(value : Int64) : self writeInt64BE, writeInt8(value : Int8) : Nil writeInt8, writeUInt128(value : UInt128) : self writeUInt128, writeUInt128BE(value : UInt128) : self writeUInt128BE, writeUInt16(value : UInt16) : self writeUInt16, writeUInt16BE(value : UInt16) : self writeUInt16BE, writeUInt32(value : UInt32) : self writeUInt32, writeUInt32BE(value : UInt32) : self writeUInt32BE, writeUInt64(value : UInt64) : self writeUInt64, writeUInt64BE(value : UInt64) : self writeUInt64BE, writeUInt8(value : UInt8) : Nil writeUInt8
Class methods inherited from class IO
readBytes(io : IO, count : Int) : Bytes
readBytes,
readFloat32(io : IO) : Float32
readFloat32,
readFloat32BE(io : IO) : Float32
readFloat32BE,
readFloat64(io : IO) : Float64
readFloat64,
readFloat64BE(io : IO) : Float64
readFloat64BE,
readInt128(io : IO) : Int128
readInt128,
readInt128BE(io : IO) : Int128
readInt128BE,
readInt16(io : IO) : Int16
readInt16,
readInt16BE(io : IO) : Int16
readInt16BE,
readInt24(io : IO) : Int32
readInt24,
readInt24BE(io : IO) : Int32
readInt24BE,
readInt32(io : IO) : Int32
readInt32,
readInt32BE(io : IO) : Int32
readInt32BE,
readInt64(io : IO) : Int64
readInt64,
readInt64BE(io : IO) : Int64
readInt64BE,
readInt8(io : IO) : Int8
readInt8,
readUInt128(io : IO) : UInt128
readUInt128,
readUInt128BE(io : IO) : UInt128
readUInt128BE,
readUInt16(io : IO) : UInt16
readUInt16,
readUInt16BE(io : IO) : UInt16
readUInt16BE,
readUInt32(io : IO) : UInt32
readUInt32,
readUInt32BE(io : IO) : UInt32
readUInt32BE,
readUInt64(io : IO) : UInt64
readUInt64,
readUInt64BE(io : IO) : UInt64
readUInt64BE,
readUInt8(io : IO) : UInt8
readUInt8,
writeInt8(io : IO, value : Int8) : Nil
writeInt8,
writeUInt8(io : IO, value : UInt8) : Nil
writeUInt8
Instance methods inherited from class Object
toRsconf(io : IO, *, indentSize : Int = 2, alwaysQuoteKeys : Bool = false, explicitRootObject : Bool = false, commaAfterValues : Bool = false, extraNewlineBetweenToplevelKeys : Bool = false) : NiltoRsconf(*, indentSize : Int = 2, alwaysQuoteKeys : Bool = false, explicitRootObject : Bool = false, commaAfterValues : Bool = false, extraNewlineBetweenToplevelKeys : Bool = false) : String toRsconf
Class methods inherited from class Object
fromRsconf(toplevel : RemiLib::RSConf::RSValue)fromRsconf(data : String | IO | Path) fromRsconf
Constructor Detail
Creates a new MmappedFile instance by mapping the file at path into
the memory space.
If mode is Mode::Read, then the file must already exist.
If mode is Mode::ReadWrite, then the file is always overwritten if it
exists and is recreated with the requested size. If it does not exist, it
is created with the requested size.
In all cases, size must be at least 1.
When you are finished with the instance, you should call #close to unmap
the file and release the resources. This can also happen during
finalization.
Class Method Detail
Creates a new MmappedFile instance by mapping the file at path into
the memory spacem, then yields that instance and executes the block. The
file must already exist.
Look at MmappedFile.new for the explanation of the other parameters.
This will automatically call #close at the end.
Instance Method Detail
Gets count bytes from the given position, then returns a new Bytes
slice containing those bytes. This does not change the internal position
of the internal cursor (#pos).
Returns an Int128 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int128 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns an Int16 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int16 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Gets three bytes from position and returns an Int32.
MmappedFileEofError if there are not enough bytes to read a 24-bit
integer at the given position. This does not change the internal position
of the internal cursor (#pos).
Returns an Int32 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int32 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns an Int64 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int64 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns an Int8 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int8 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Gets a String of count bytes long at the given position, then returns
the new String. This does not change the internal position of the
internal cursor (#pos).
Returns a UInt128 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt128 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns a UInt16 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt16 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Gets three bytes from position and returns a UInt32.
MmappedFileEofError if there are not enough bytes to read a 24-bit
number at the given position. This does not change the internal position
of the internal cursor (#pos).
Returns a UInt32 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt32 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns a UInt64 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt64 at
the given position. This does not change the internal position of the
internal cursor (#pos).
Returns a UInt8 from the file at the given position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt8 at the
given position. This does not change the internal position of the
internal cursor (#pos).
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"
Writes a Bytes slice to the file at the given position. If there is not
enough space to write the value, then this raises a MmappedFileEofError.
This does not change the internal position of the internal cursor
(#pos).
Writes an Int128 to the file at the given position. If there is no
space to write the value, then this raises a MmappedFileEofError. This
does not change the internal position of the internal cursor (#pos).
Writes an Int16 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes a 24-bit integer stored in a UInt32 to the file at the given
position. If there is no space to write the value, then this raises a
MmappedFileEofError. This does not change the internal position of the
internal cursor (#pos).
If val does not contain a 24-bit integer, this raises an
ArgumentError.
Writes an Int32 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes an Int64 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes an Int8 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes a String as raw bytes to the file at the given position. If
there is not enough space to write the value, then this raises a
MmappedFileEofError. This does not change the internal position of the
internal cursor (#pos).
Writes a UInt128 to the file at the given position. If there is no
space to write the value, then this raises a MmappedFileEofError. This
does not change the internal position of the internal cursor (#pos).
Writes a UInt16 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes a 24-bit integer stored in a UInt32 to the file at the given
position. If there is no space to write the value, then this raises a
MmappedFileEofError. This does not change the internal position of the
internal cursor (#pos).
If val does not contain a 24-bit integer, this raises an
ArgumentError.
Writes a UInt32 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes a UInt64 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
Writes a UInt8 to the file at the given position. If there is no space
to write the value, then this raises a MmappedFileEofError. This does
not change the internal position of the internal cursor (#pos).
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
Reads count bytes, then returns a new Bytes slice containing those
bytes.
Reads an Int128 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int128.
Reads a Int16 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a Int16.
Reads three bytes from the file at the current position and returns a
24-bit integer as an Int32. Raises an MmappedFileEofError if there
are not enough bytes to read a 24-bit integer.
Reads an Int32 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int32.
Reads an Int64 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read an Int64.
Reads a Int8 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a Int8.
Reads a UInt128 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt128.
Reads a UInt16 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt16.
Reads three bytes from the file at the current position and returns a
24-bit integer as a UInt32. Raises an MmappedFileEofError if there
are not enough bytes to read a 24-bit unsigned integer.
Reads a UInt32 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt32.
Reads a UInt64 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt64.
Reads a UInt8 from the file at the current position. Raises an
MmappedFileEofError if there are not enough bytes to read a UInt8.
Rewinds this IO. By default this method raises, but including types
may implement it.
Specifies the behavior when #sync or #close are called and this
instance is writeable. If the instance isn't writeable, this is ignored.
Specifies the behavior when #sync or #close are called and this
instance is writeable. If the instance isn't writeable, this is ignored.
Writes the contents of slice into this IO.
io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"
Writes a Bytes slice to the file at the current position. If there is
not enough space to write the value, then this raises a
MmappedFileEofError.
Writes an Int128 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes an Int16 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a 24-bit integer stored in an Int32 to the file at the current
position. If there is no space to write the value, then this raises a
MmappedFileEofError.
Writes an Int32 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes an Int64 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes an Int8 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a String as raw bytes to the file at the current position. If
there is not enough space to write the value, then this raises a
MmappedFileEofError.
Writes a UInt128 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a UInt16 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a 24-bit integer stored in a UInt32 to the file at the current
position. If there is no space to write the value, then this raises a
MmappedFileEofError.
If val does not contain a 24-bit integer, this raises an
ArgumentError.
Writes a UInt32 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a UInt64 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.
Writes a UInt8 to the file at the current position. If there is no
space to write the value, then this raises a MmappedFileEofError.