IO::Delimited
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"
Constructors
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.
Instance methods
Returns true if this IO is closed.
IO defines returns false, but including types may override.
Peeks into this IO, if possible.
It returns:
nilif this IO isn't peekable at this moment or at all- an empty slice if it is, but EOF was reached
- a non-empty slice if some data can be peeked
The returned bytes are only valid data until a next call to any method that reads from this IO is invoked.
By default this method returns nil, but IO implementations
that provide buffering or wrap other IOs should override
this method.
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