abstract class IO

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:

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.cr
remilib/extensions.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.readBytes(io : IO, count : Int) : Bytes #

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.


[View source]
def self.readFloat32(io : IO) : Float32 #

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.


[View source]
def self.readFloat32BE(io : IO) : Float32 #

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.


[View source]
def self.readFloat64(io : IO) : Float64 #

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.


[View source]
def self.readFloat64BE(io : IO) : Float64 #

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.


[View source]
def self.readInt128(io : IO) : Int128 #

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.


[View source]
def self.readInt128BE(io : IO) : Int128 #

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.


[View source]
def self.readInt16(io : IO) : Int16 #

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.


[View source]
def self.readInt16BE(io : IO) : Int16 #

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.


[View source]
def self.readInt24(io : IO) : Int32 #

Reads a 24-bit signed little-endian integer from io.


[View source]
def self.readInt24BE(io : IO) : Int32 #

Reads a 24-bit signed big-endian integer from io.


[View source]
def self.readInt32(io : IO) : Int32 #

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.


[View source]
def self.readInt32BE(io : IO) : Int32 #

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.


[View source]
def self.readInt64(io : IO) : Int64 #

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.


[View source]
def self.readInt64BE(io : IO) : Int64 #

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.


[View source]
def self.readInt8(io : IO) : Int8 #

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.


[View source]
def self.readUInt128(io : IO) : UInt128 #

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.


[View source]
def self.readUInt128BE(io : IO) : UInt128 #

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.


[View source]
def self.readUInt16(io : IO) : UInt16 #

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.


[View source]
def self.readUInt16BE(io : IO) : UInt16 #

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.


[View source]
def self.readUInt32(io : IO) : UInt32 #

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.


[View source]
def self.readUInt32BE(io : IO) : UInt32 #

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.


[View source]
def self.readUInt64(io : IO) : UInt64 #

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.


[View source]
def self.readUInt64BE(io : IO) : UInt64 #

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.


[View source]
def self.readUInt8(io : IO) : UInt8 #

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.


[View source]
def self.writeInt8(io : IO, value : Int8) : Nil #

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.


[View source]
def self.writeUInt8(io : IO, value : UInt8) : Nil #

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.


[View source]

Instance Method Detail

def endBold : self #

[View source]
def endColor : self #

[View source]
def endConceal : self #

[View source]
def endCrossedOut : self #

[View source]
def endEncircled : self #

[View source]
def endFaint : self #

[View source]
def endFastBlink : self #

[View source]
def endFraktur : self #

[View source]
def endFramed : self #

[View source]
def endItalic : self #

[View source]
def endOverlined : self #

[View source]
def endReverseVideo : self #

[View source]
def endSlowBlink : self #

[View source]
def endUnderline : self #

[View source]
def readBytes(count : Int) : Bytes #

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.


[View source]
def readFloat32 #

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.


[View source]
def readFloat32BE #

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.


[View source]
def readFloat64 #

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.


[View source]
def readFloat64BE #

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.


[View source]
def readInt128 #

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.


[View source]
def readInt128BE #

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.


[View source]
def readInt16 #

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.


[View source]
def readInt16BE #

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.


[View source]
def readInt24 #

Reads a 24-bit signed little-endian integer from io.


[View source]
def readInt24BE #

Reads a 24-bit signed big-endian integer from io.


[View source]
def readInt32 #

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.


[View source]
def readInt32BE #

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.


[View source]
def readInt64 #

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.


[View source]
def readInt64BE #

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.


[View source]
def readInt8 #

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.


[View source]
def readUInt128 #

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.


[View source]
def readUInt128BE #

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.


[View source]
def readUInt16 #

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.


[View source]
def readUInt16BE #

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.


[View source]
def readUInt32 #

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.


[View source]
def readUInt32BE #

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.


[View source]
def readUInt64 #

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.


[View source]
def readUInt64BE #

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.


[View source]
def readUInt8 #

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.


[View source]
def startBold : self #

[View source]
def startColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default) : self #

[View source]
def startConceal : self #

[View source]
def startCrossedOut : self #

[View source]
def startEncircled : self #

[View source]
def startFaint : self #

[View source]
def startFastBlink : self #

[View source]
def startFraktur : self #

[View source]
def startFramed : self #

[View source]
def startItalic : self #

[View source]
def startOverlined : self #

[View source]
def startReverseVideo : self #

[View source]
def startSlowBlink : self #

[View source]
def startUnderline : self #

[View source]
def withBold(&) #

[View source]
def withColor(fg : RemiLib::Console::Color, bg : RemiLib::Console::Color = RemiLib::Console::Color::Default, &) : self #

[View source]
def withConceal(&) #

[View source]
def withCrossedOut(&) #

[View source]
def withEncircled(&) #

[View source]
def withExcursion(gotoHere, &) #

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.


[View source]
def withExcursion(&) #

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.


[View source]
def withFaint(&) #

[View source]
def withFastBlink(&) #

[View source]
def withFraktur(&) #

[View source]
def withFramed(&) #

[View source]
def withItalic(&) #

[View source]
def withOverlined(&) #

[View source]
def withReverseVideo(&) #

[View source]
def withSlowBlink(&) #

[View source]
def withUnderline(&) #

[View source]
def writeFloat32(value : Float32) : self #

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.


[View source]
def writeFloat32BE(value : Float32) : self #

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.


[View source]
def writeFloat64(value : Float64) : self #

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.


[View source]
def writeFloat64BE(value : Float64) : self #

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.


[View source]
def writeInt128(value : Int128) : self #

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.


[View source]
def writeInt128BE(value : Int128) : self #

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.


[View source]
def writeInt16(value : Int16) : self #

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.


[View source]
def writeInt16BE(value : Int16) : self #

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.


[View source]
def writeInt24(data : Int32) #

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.


[View source]
def writeInt24BE(data : Int32) #

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.


[View source]
def writeInt32(value : Int32) : self #

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.


[View source]
def writeInt32BE(value : Int32) : self #

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.


[View source]
def writeInt64(value : Int64) : self #

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.


[View source]
def writeInt64BE(value : Int64) : self #

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.


[View source]
def writeInt8(value : Int8) : Nil #

Convenience method to write a signed 8-bit byte.

This is the same as calling io.write_byte(value.to_u8!), just shorter.


[View source]
def writeUInt128(value : UInt128) : self #

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.


[View source]
def writeUInt128BE(value : UInt128) : self #

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.


[View source]
def writeUInt16(value : UInt16) : self #

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.


[View source]
def writeUInt16BE(value : UInt16) : self #

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.


[View source]
def writeUInt32(value : UInt32) : self #

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.


[View source]
def writeUInt32BE(value : UInt32) : self #

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.


[View source]
def writeUInt64(value : UInt64) : self #

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.


[View source]
def writeUInt64BE(value : UInt64) : self #

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.


[View source]
def writeUInt8(value : UInt8) : Nil #

Convenience method to write an unsigned 8-bit byte.

This is the same as calling io.write_byte(value) and is included for consistency.


[View source]