class

IO::PrefixSuffixBuffer

Inherits IO / Reference / Object

PrefixSuffixBuffer is an IO that retains the first bytes in the prefix buffer and the last bytes in the suffix buffer.

When writing more bytes than fit in the buffers, only the start and end bytes are preserved. #to_s renders the buffer contents with a message indicating how many bytes were omitted.

Constructors

new(prefix : Bytes, suffix : Bytes)

Creates an instance using the given buffers.

The maximum size that this instance can consume without omitting data is the sum of the buffer sizes.

Source
new(size : Int32 = IO::DEFAULT_BUFFER_SIZE) : self

Creates an instance with prefix and suffix buffers of the given size.

The maximum size that this instance can consume without omitting data is size * 2.

Source

Instance methods

capacity
Source
read(slice : Bytes) : NoReturn

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
Source
to_s(io : IO) : Nil

Appends the buffer to the given IO.

When the total size of the consumed data exceeds the buffer size, the middle part is omitted and replaced by a message that indicates the number of skipped bytes.

Source
to_s

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.

Source
total_size
Source
write(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"
Source