Class: HTTPX::Buffer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/buffer.rb

Overview

Internal class to abstract a string buffer, by wrapping a string and providing the minimum possible API and functionality required.

buffer = Buffer.new(640)
buffer.full? #=> false
buffer << "aa"
buffer.capacity #=> 638

Direct Known Subclasses

Plugins::StreamBidi::BidiBuffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ Buffer

Returns a new instance of Buffer.



32
33
34
35
# File 'lib/httpx/buffer.rb', line 32

def initialize(limit)
  @buffer = String.new("", encoding: Encoding::BINARY, capacity: limit)
  @limit = limit
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



29
30
31
# File 'lib/httpx/buffer.rb', line 29

def limit
  @limit
end

Instance Method Details

#<<(chunk) ⇒ Object



37
38
39
# File 'lib/httpx/buffer.rb', line 37

def <<(chunk)
  @buffer.append_as_bytes(chunk)
end

#capacityObject



53
54
55
# File 'lib/httpx/buffer.rb', line 53

def capacity
  @limit - @buffer.bytesize
end

#full?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/httpx/buffer.rb', line 49

def full?
  @buffer.bytesize >= @limit
end

#shift!(fin) ⇒ Object



57
58
59
# File 'lib/httpx/buffer.rb', line 57

def shift!(fin)
  @buffer = @buffer.byteslice(fin..-1) || "".b
end