class IO::Stream::Generic

def consume_read_buffer(size = nil)

@parameter size [Integer|nil] The amount of data to consume. If nil, consume entire buffer.
Consumes at most `size` bytes from the buffer.
def consume_read_buffer(size = nil)
	# If we are at eof, and the read buffer is empty, we can't consume anything.
	return nil if @eof && @read_buffer.empty?
	
	result = nil
	
	if size.nil? or size >= @read_buffer.bytesize
		# Consume the entire read buffer:
		result = @read_buffer
		@read_buffer = StringBuffer.new
	else
		# This approach uses more memory.
		# result = @read_buffer.slice!(0, size)
		
		# We know that we are not going to reuse the original buffer.
		# But byteslice will generate a hidden copy. So let's freeze it first:
		@read_buffer.freeze
		
		result = @read_buffer.byteslice(0, size)
		@read_buffer = @read_buffer.byteslice(size, @read_buffer.bytesize)
	end
	
	return result
end