lib/http/2/buffer.rb in http-2-0.6.1 vs lib/http/2/buffer.rb in http-2-0.6.3
- old
+ new
@@ -2,20 +2,37 @@
# Simple binary buffer backed by string.
#
class Buffer < String
+ UINT32 = "N"
+ BINARY = "binary"
+ private_constant :UINT32, :BINARY
+
# Forces binary encoding on the string
- def initialize(*args)
- force_encoding('binary')
- super(*args)
+ def initialize(data = '')
+ super(data.force_encoding(BINARY))
end
# Emulate StringIO#read: slice first n bytes from the buffer.
#
# @param n [Integer] number of bytes to slice from the buffer
def read(n)
- slice!(0,n)
+ Buffer.new(slice!(0,n))
+ end
+
+ # Alias getbyte to readbyte
+ alias :readbyte :getbyte
+
+ # Emulate StringIO#getbyte: slice first byte from buffer.
+ def getbyte
+ read(1).ord
+ end
+
+ # Slice unsigned 32-bit integer from buffer.
+ # @return [Integer]
+ def read_uint32
+ read(4).unpack(UINT32).first
end
end
end