abstract class IO
- IO
- Reference
- Object
Overview
The IO
class is the basis for all input and output in Crystal.
This class is inherited by types like File
, Socket
and IO::Memory
and
provides many useful methods for reading from and writing to an IO, like print
, puts
,
gets
and printf
.
The only requirement for a type including the IO
module is to define
these two methods:
read(slice : Bytes)
: read at most slice.size bytes from IO into slice and return the number of bytes readwrite(slice : Bytes)
: write the whole slice into the IO
For example, this is a simple IO
on top of a Bytes
:
class SimpleSliceIO < IO
def initialize(@slice : Bytes)
end
def read(slice : Bytes)
slice.size.times { |i| slice[i] = @slice[i] }
@slice += slice.size
slice.size
end
def write(slice : Bytes) : Nil
slice.size.times { |i| @slice[i] = slice[i] }
@slice += slice.size
end
end
slice = Slice.new(9) { |i| ('a'.ord + i).to_u8 }
String.new(slice) # => "abcdefghi"
io = SimpleSliceIO.new(slice)
io.gets(3) # => "abc"
io.print "xyz"
String.new(slice) # => "abcxyzghi"
Encoding
An IO
can be set an encoding with the #set_encoding
method. When this is
set, all string operations (gets
, gets_to_end
, read_char
, <<
, print
, puts
printf
) will write in the given encoding, and read from the given encoding.
Byte operations (read
, write
, read_byte
, write_byte
, getb_to_end
) never do
encoding/decoding operations.
If an encoding is not set, the default one is UTF-8.
Mixing string and byte operations might not give correct results and should be avoided, as string operations might need to read extra bytes in order to get characters in the given encoding.
Direct Known Subclasses
Defined in:
remilib/console/ansi.crremilib/extensions.cr
Class Method Summary
-
.readBytes(io : IO, count : Int) : Bytes
Reads count bytes into a
::Bytes
and returns the new::Bytes
. -
.readFloat32(io : IO) : Float32
Convenience method to read a 32-bit little-endian floating point number from
io
. -
.readFloat32BE(io : IO) : Float32
Convenience method to read a 32-bit big-endian floating point number from
io
. -
.readFloat64(io : IO) : Float64
Convenience method to read a 64-bit little-endian floating point number from
io
. -
.readFloat64BE(io : IO) : Float64
Convenience method to read a 64-bit big-endian floating point number from
io
. -
.readInt128(io : IO) : Int128
Convenience method to read a signed 128-bit little-endian integer from
io
. -
.readInt128BE(io : IO) : Int128
Convenience method to read a signed 128-bit big-endian integer from
io
. -
.readInt16(io : IO) : Int16
Convenience method to read a signed 16-bit little-endian integer from
io
. -
.readInt16BE(io : IO) : Int16
Convenience method to read a signed 16-bit big-endian integer from
io
. -
.readInt24(io : IO) : Int32
Reads a 24-bit signed little-endian integer from
io
. -
.readInt24BE(io : IO) : Int32
Reads a 24-bit signed big-endian integer from
io
. -
.readInt32(io : IO) : Int32
Convenience method to read a signed 32-bit little-endian integer from
io
. -
.readInt32BE(io : IO) : Int32
Convenience method to read a signed 32-bit big-endian integer from
io
. -
.readInt64(io : IO) : Int64
Convenience method to read a signed 64-bit little-endian integer from
io
. -
.readInt64BE(io : IO) : Int64
Convenience method to read a signed 64-bit big-endian integer from
io
. -
.readInt8(io : IO) : Int8
Convenience method to read a signed 8-bit byte from
io
. -
.readUInt128(io : IO) : UInt128
Convenience method to read a signed 128-bit little-endian integer from
io
. -
.readUInt128BE(io : IO) : UInt128
Convenience method to read a signed 128-bit big-endian integer from
io
. -
.readUInt16(io : IO) : UInt16
Convenience method to read a signed 16-bit little-endian integer from
io
. -
.readUInt16BE(io : IO) : UInt16
Convenience method to read a signed 16-bit big-endian integer from
io
. -
.readUInt32(io : IO) : UInt32
Convenience method to read a signed 32-bit little-endian integer from
io
. -
.readUInt32BE(io : IO) : UInt32
Convenience method to read a signed 32-bit big-endian integer from
io
. -
.readUInt64(io : IO) : UInt64
Convenience method to read a signed 64-bit little-endian integer from
io
. -
.readUInt64BE(io : IO) : UInt64
Convenience method to read a signed 64-bit big-endian integer from
io
. -
.readUInt8(io : IO) : UInt8
Convenience method to read an unsigned 8-bit byte from
io
. -
.writeInt8(io : IO, value : Int8) : Nil
Convenience method to write a signed 8-bit byte to
io
. -
.writeUInt8(io : IO, value : UInt8) : Nil
Convenience method to write an unsigned 8-bit byte to
io
.
Instance Method Summary
- #endBold : self
- #endColor : self
- #endConceal : self
- #endCrossedOut : self
- #endEncircled : self
- #endFaint : self
- #endFastBlink : self
- #endFraktur : self
- #endFramed : self
- #endItalic : self
- #endOverlined : self
- #endReverseVideo : self
- #endSlowBlink : self
- #endUnderline : self
-
#readBytes(count : Int) : Bytes
Reads count bytes into a
::Bytes
and returns the new::Bytes
. -
#readFloat32
Convenience method to read a 32-bit little-endian floating point number.
-
#readFloat32BE
Convenience method to read a 32-bit big-endian floating point number.
-
#readFloat64
Convenience method to read a 64-bit little-endian floating point number.
-
#readFloat64BE
Convenience method to read a 64-bit big-endian floating point number.
-
#readInt128
Convenience method to read a signed 128-bit little-endian integer.
-
#readInt128BE
Convenience method to read a signed 128-bit big-endian integer.
-
#readInt16
Convenience method to read a signed 16-bit little-endian integer.
-
#readInt16BE
Convenience method to read a signed 16-bit big-endian integer.
-
#readInt24
Reads a 24-bit signed little-endian integer from
io
. -
#readInt24BE
Reads a 24-bit signed big-endian integer from
io
. -
#readInt32
Convenience method to read a signed 32-bit little-endian integer.
-
#readInt32BE
Convenience method to read a signed 32-bit big-endian integer.
-
#readInt64
Convenience method to read a signed 64-bit little-endian integer.
-
#readInt64BE
Convenience method to read a signed 64-bit big-endian integer.
-
#readInt8
Convenience method to read a signed 8-bit byte.
-
#readUInt128
Convenience method to read a signed 128-bit little-endian integer.
-
#readUInt128BE
Convenience method to read a signed 128-bit big-endian integer.
-
#readUInt16
Convenience method to read a signed 16-bit little-endian integer.
-
#readUInt16BE
Convenience method to read a signed 16-bit big-endian integer.
-
#readUInt32
Convenience method to read a signed 32-bit little-endian integer.
-
#readUInt32BE
Convenience method to read a signed 32-bit big-endian integer.
-
#readUInt64
Convenience method to read a signed 64-bit little-endian integer.
-
#readUInt64BE
Convenience method to read a signed 64-bit big-endian integer.
-
#readUInt8
Convenience method to read an unsigned 8-bit byte.
- #startBold : self
- #startColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default) : self
- #startConceal : self
- #startCrossedOut : self
- #startEncircled : self
- #startFaint : self
- #startFastBlink : self
- #startFraktur : self
- #startFramed : self
- #startItalic : self
- #startOverlined : self
- #startReverseVideo : self
- #startSlowBlink : self
- #startUnderline : self
- #withBold(&)
- #withColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default, &) : self
- #withConceal(&)
- #withCrossedOut(&)
- #withEncircled(&)
-
#withExcursion(gotoHere, &)
Temporarily changes the stream position to
gotoHere
, then yields. -
#withExcursion(&)
Yields, then automatically returns to the position before calling this method before it returns.
- #withFaint(&)
- #withFastBlink(&)
- #withFraktur(&)
- #withFramed(&)
- #withItalic(&)
- #withOverlined(&)
- #withReverseVideo(&)
- #withSlowBlink(&)
- #withUnderline(&)
-
#writeFloat32(value : Float32) : self
Convenience method to write a signed 32-bit little-endian float.
-
#writeFloat32BE(value : Float32) : self
Convenience method to write a signed 32-bit big-endian float.
-
#writeFloat64(value : Float64) : self
Convenience method to write a signed 64-bit little-endian float.
-
#writeFloat64BE(value : Float64) : self
Convenience method to write a signed 64-bit big-endian float.
-
#writeInt128(value : Int128) : self
Convenience method to write a signed 128-bit little-endian integer.
-
#writeInt128BE(value : Int128) : self
Convenience method to write a signed 128-bit big-endian integer.
-
#writeInt16(value : Int16) : self
Convenience method to write a signed 16-bit little-endian integer.
-
#writeInt16BE(value : Int16) : self
Convenience method to write a signed 16-bit big-endian integer.
-
#writeInt24(data : Int32)
Writes
data
as a 24-bit signed little-endian integer. -
#writeInt24BE(data : Int32)
Writes
data
as a 24-bit signed big-endian integer. -
#writeInt32(value : Int32) : self
Convenience method to write a signed 32-bit little-endian integer.
-
#writeInt32BE(value : Int32) : self
Convenience method to write a signed 32-bit big-endian integer.
-
#writeInt64(value : Int64) : self
Convenience method to write a signed 64-bit little-endian integer.
-
#writeInt64BE(value : Int64) : self
Convenience method to write a signed 64-bit big-endian integer.
-
#writeInt8(value : Int8) : Nil
Convenience method to write a signed 8-bit byte.
-
#writeUInt128(value : UInt128) : self
Convenience method to write a signed 128-bit little-endian integer.
-
#writeUInt128BE(value : UInt128) : self
Convenience method to write a signed 128-bit big-endian integer.
-
#writeUInt16(value : UInt16) : self
Convenience method to write a signed 16-bit little-endian integer.
-
#writeUInt16BE(value : UInt16) : self
Convenience method to write a signed 16-bit big-endian integer.
-
#writeUInt32(value : UInt32) : self
Convenience method to write a signed 32-bit little-endian integer.
-
#writeUInt32BE(value : UInt32) : self
Convenience method to write a signed 32-bit big-endian integer.
-
#writeUInt64(value : UInt64) : self
Convenience method to write a signed 64-bit little-endian integer.
-
#writeUInt64BE(value : UInt64) : self
Convenience method to write a signed 64-bit big-endian integer.
-
#writeUInt8(value : UInt8) : Nil
Convenience method to write an unsigned 8-bit byte.
Class Method Detail
Reads count bytes into a ::Bytes
and returns the new ::Bytes
. If
count bytes could not be read, this will return a ::Bytes
that is equal
in length to the number of bytes actually read.
Convenience method to read a 32-bit little-endian floating point number from io
.
This is the same as calling io.read_bytes(Float32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a 32-bit big-endian floating point number from io
.
This is the same as calling io.read_bytes(Float32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a 64-bit little-endian floating point number from io
.
This is the same as calling io.read_bytes(Float64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a 64-bit big-endian floating point number from io
.
This is the same as calling io.read_bytes(Float64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 128-bit little-endian integer from io
.
This is the same as calling io.read_bytes(Int128, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 128-bit big-endian integer from io
.
This is the same as calling io.read_bytes(Int128, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 16-bit little-endian integer from io
.
This is the same as calling io.read_bytes(Int16, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 16-bit big-endian integer from io
.
This is the same as calling io.read_bytes(Int16, IO::ByteFormat::BigEndian)
,
just shorter.
Reads a 24-bit signed little-endian integer from io
.
Reads a 24-bit signed big-endian integer from io
.
Convenience method to read a signed 32-bit little-endian integer from io
.
This is the same as calling io.read_bytes(Int32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 32-bit big-endian integer from io
.
This is the same as calling io.read_bytes(Int32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 64-bit little-endian integer from io
.
This is the same as calling io.read_bytes(Int64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 64-bit big-endian integer from io
.
This is the same as calling io.read_bytes(Int64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 8-bit byte from io
. If the byte
cannot be read, this will raise an IO::EOFError
.
This is the same as calling io.read_byte.try &.to_i8! || raise IO::EOFError.new
,
just shorter.
Convenience method to read a signed 128-bit little-endian integer from io
.
This is the same as calling io.read_bytes(UInt128, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 128-bit big-endian integer from io
.
This is the same as calling io.read_bytes(UInt128, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 16-bit little-endian integer from io
.
This is the same as calling io.read_bytes(UInt16, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 16-bit big-endian integer from io
.
This is the same as calling io.read_bytes(UInt16, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 32-bit little-endian integer from io
.
This is the same as calling io.read_bytes(UInt32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 32-bit big-endian integer from io
.
This is the same as calling io.read_bytes(UInt32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 64-bit little-endian integer from io
.
This is the same as calling io.read_bytes(UInt64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 64-bit big-endian integer from io
.
This is the same as calling io.read_bytes(UInt64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read an unsigned 8-bit byte from io
. If the byte
cannot be read, this will raise an IO::EOFError
.
This is the same as calling io.read_byte || raise IO::EOFError.new
, just
shorter.
Convenience method to write a signed 8-bit byte to io
.
This is the same as calling io.write_byte(value.to_u8!)
, just shorter.
Convenience method to write an unsigned 8-bit byte to io
.
This is the same as calling io.write_byte(value)
and is included for
consistency.
Instance Method Detail
Reads count bytes into a ::Bytes
and returns the new ::Bytes
. If
count bytes could not be read, this will return a ::Bytes
that is equal
in length to the number of bytes actually read.
Convenience method to read a 32-bit little-endian floating point number.
This is the same as calling io.read_bytes(Float32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a 32-bit big-endian floating point number.
This is the same as calling io.read_bytes(Float32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a 64-bit little-endian floating point number.
This is the same as calling io.read_bytes(Float64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a 64-bit big-endian floating point number.
This is the same as calling io.read_bytes(Float64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 128-bit little-endian integer.
This is the same as calling read_bytes(Int128, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 128-bit big-endian integer.
This is the same as calling read_bytes(Int128, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 16-bit little-endian integer.
This is the same as calling read_bytes(Int16, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 16-bit big-endian integer.
This is the same as calling read_bytes(Int16, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 32-bit little-endian integer.
This is the same as calling read_bytes(Int32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 32-bit big-endian integer.
This is the same as calling read_bytes(Int32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 64-bit little-endian integer.
This is the same as calling read_bytes(Int64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 64-bit big-endian integer.
This is the same as calling read_bytes(Int64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 8-bit byte. If the byte cannot be read,
this will raise an IO::EOFError
.
This is the same as calling io.read_byte.try &.to_i8! || raise IO::EOFError.new
,
just shorter.
Convenience method to read a signed 128-bit little-endian integer.
This is the same as calling read_bytes(UInt128, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 128-bit big-endian integer.
This is the same as calling read_bytes(UInt128, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 16-bit little-endian integer.
This is the same as calling read_bytes(UInt16, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 16-bit big-endian integer.
This is the same as calling read_bytes(UInt16, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 32-bit little-endian integer.
This is the same as calling read_bytes(UInt32, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 32-bit big-endian integer.
This is the same as calling read_bytes(UInt32, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read a signed 64-bit little-endian integer.
This is the same as calling read_bytes(UInt64, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to read a signed 64-bit big-endian integer.
This is the same as calling read_bytes(UInt64, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to read an unsigned 8-bit byte. If the byte cannot be
read, this will raise an IO::EOFError
.
This is the same as calling io.read_byte || raise IO::EOFError.new
, just
shorter.
Temporarily changes the stream position to gotoHere
, then yields. This
automatically returns to the position before calling this method before it
returns.
This only works for types that can get and set their position.
Yields, then automatically returns to the position before calling this method before it returns.
This only works for types that can get and set their position.
Convenience method to write a signed 32-bit little-endian float. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 32-bit big-endian float. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 64-bit little-endian float. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 64-bit big-endian float. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 128-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 128-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 16-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 16-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Writes data
as a 24-bit signed little-endian integer. This does not check
to see if data
is within the range of a 24-bit integer and always writes
it as 24-bits.
Writes data
as a 24-bit signed big-endian integer. This does not check to
see if data
is within the range of a 24-bit integer and always writes it
as 24-bits.
Convenience method to write a signed 32-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 32-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 64-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 64-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 8-bit byte.
This is the same as calling io.write_byte(value.to_u8!)
, just shorter.
Convenience method to write a signed 128-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 128-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 16-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 16-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 32-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 32-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write a signed 64-bit little-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::LittleEndian)
,
just shorter.
Convenience method to write a signed 64-bit big-endian integer. This returns `self.
This is the same as calling write_bytes(value, IO::ByteFormat::BigEndian)
,
just shorter.
Convenience method to write an unsigned 8-bit byte.
This is the same as calling io.write_byte(value)
and is included
for consistency.