class
Amber::Router::Parsers::BoundedIO
- Amber::Router::Parsers::BoundedIO
- IO
- Reference
- Object
Overview
Wraps an IO and raises Amber::Exceptions::ParameterLimitExceeded once
more than limit bytes have been consumed through it.
A segment limit can be opened on top of the total, for example around a multipart part's headers, so one long header line is rejected as soon as it passes the segment allowance.
Defined in:
amber/router/parsers/bounded_io.crConstructors
Instance Method Summary
-
#begin_segment(limit : Int, limit_name : String) : Nil
Starts counting a segment that may consume at most limit bytes.
- #bytes_read : Int64
- #end_segment : Nil
-
#peek : Bytes | Nil
Peeking does not consume, so it is not charged;
#skipis. -
#read(slice : Bytes) : Int32
Reads at most slice.size bytes from this
IOinto slice. -
#skip(bytes_count : Int) : Nil
Reads and discards exactly bytes_count bytes.
-
#write(slice : Bytes) : Nil
Writes the contents of slice into this
IO.
Constructor Detail
Instance Method Detail
Starts counting a segment that may consume at most limit bytes.
Peeking does not consume, so it is not charged; #skip is. The peeked
bytes are trimmed to one byte past the remaining allowance, so a reader
that scans the peek buffer (such as gets) cannot copy more than that
before its #skip trips the limit.
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 and discards exactly bytes_count bytes.
Raises IO::EOFError if there aren't at least bytes_count bytes.
io = IO::Memory.new "hello world"
io.skip(6)
io.gets # => "world"
io.skip(1) # raises IO::EOFError
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"