class IO::Delimited
inherits IO
¶
An IO
that wraps another IO
, and only reads up to the beginning of a
specified delimiter.
This is useful for exposing part of an underlying stream to a client.
io = IO::Memory.new "abc||123"
delimited = IO::Delimited.new(io, read_delimiter: "||")
delimited.gets_to_end # => "abc"
delimited.gets_to_end # => ""
io.gets_to_end # => "123"
Class methods¶
.new(io : IO, read_delimiter : String, sync_close : Bool = false)
¶
(io : IO, read_delimiter : String, sync_close : Bool = false)
Creates a new IO::Delimited
which wraps io, and can read until the
byte sequence read_delimiter (interpreted as UTF-8) is found. If
sync_close is set, calling #close
calls #close
on the underlying
IO
.
.new(io : IO, read_delimiter : Bytes, sync_close : Bool = false)
¶
(io : IO, read_delimiter : Bytes, sync_close : Bool = false)
Creates a new IO::Delimited
which wraps io, and can read until the
byte sequence read_delimiter is found. If sync_close is set, calling
#close
calls #close
on the underlying IO
.
Methods¶
#close
¶
View source
#closed? : Bool
¶
View source
: Bool
#read(slice : Bytes)
¶
(slice : Bytes)
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
#sync_close=(sync_close)
¶
(sync_close)
If #sync_close?
is true
, closing this IO
will close the underlying IO
.
#sync_close? : Bool
¶
: Bool
If #sync_close?
is true
, closing this IO
will close the underlying IO
.
#write(slice : Bytes) : Nil
¶
(slice : Bytes) : Nil
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"