Class: Cql::ByteBuffer
- Inherits:
-
Object
- Object
- Cql::ByteBuffer
- Defined in:
- lib/cql/byte_buffer.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
(also: #size, #bytesize)
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #append(bytes) ⇒ Object (also: #<<)
- #cheap_peek ⇒ Object
- #discard(n) ⇒ Object
- #dup ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(initial_bytes = '') ⇒ ByteBuffer
constructor
A new instance of ByteBuffer.
- #inspect ⇒ Object
- #read(n) ⇒ Object
- #read_byte(signed = false) ⇒ Object
- #read_int ⇒ Object
- #read_short ⇒ Object
- #to_str ⇒ Object (also: #to_s)
- #update(location, bytes) ⇒ Object
Constructor Details
#initialize(initial_bytes = '') ⇒ ByteBuffer
Returns a new instance of ByteBuffer.
6 7 8 9 10 11 12 |
# File 'lib/cql/byte_buffer.rb', line 6 def initialize(initial_bytes='') @read_buffer = '' @write_buffer = '' @offset = 0 @length = 0 append(initial_bytes) unless initial_bytes.empty? end |
Instance Attribute Details
#length ⇒ Object (readonly) Also known as: size, bytesize
Returns the value of attribute length.
14 15 16 |
# File 'lib/cql/byte_buffer.rb', line 14 def length @length end |
Instance Method Details
#append(bytes) ⇒ Object Also known as: <<
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cql/byte_buffer.rb', line 22 def append(bytes) if bytes.is_a?(self.class) bytes.append_to(self) else bytes = bytes.to_s unless bytes.ascii_only? bytes = bytes.dup.force_encoding(::Encoding::BINARY) end retag = @write_buffer.empty? @write_buffer << bytes @write_buffer.force_encoding(::Encoding::BINARY) if retag @length += bytes.bytesize end self end |
#cheap_peek ⇒ Object
128 129 130 131 132 133 |
# File 'lib/cql/byte_buffer.rb', line 128 def cheap_peek if @offset >= @read_buffer.bytesize swap_buffers end @read_buffer[@offset, @read_buffer.bytesize - @offset] end |
#discard(n) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/cql/byte_buffer.rb', line 39 def discard(n) raise RangeError, "#{n} bytes to discard but only #{@length} available" if @length < n @offset += n @length -= n self end |
#dup ⇒ Object
144 145 146 |
# File 'lib/cql/byte_buffer.rb', line 144 def dup self.class.new(to_str) end |
#empty? ⇒ Boolean
18 19 20 |
# File 'lib/cql/byte_buffer.rb', line 18 def empty? length == 0 end |
#eql?(other) ⇒ Boolean Also known as: ==
135 136 137 |
# File 'lib/cql/byte_buffer.rb', line 135 def eql?(other) self.to_str.eql?(other.to_str) end |
#hash ⇒ Object
140 141 142 |
# File 'lib/cql/byte_buffer.rb', line 140 def hash to_str.hash end |
#inspect ⇒ Object
153 154 155 |
# File 'lib/cql/byte_buffer.rb', line 153 def inspect %(#<#{self.class.name}: #{to_str.inspect}>) end |
#read(n) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cql/byte_buffer.rb', line 46 def read(n) raise RangeError, "#{n} bytes required but only #{@length} available" if @length < n if @offset >= @read_buffer.bytesize swap_buffers end if @offset + n > @read_buffer.bytesize s = read(@read_buffer.bytesize - @offset) s << read(n - s.bytesize) s else s = @read_buffer[@offset, n] @offset += n @length -= n s end end |
#read_byte(signed = false) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cql/byte_buffer.rb', line 101 def read_byte(signed=false) raise RangeError, "No bytes available to read byte" if empty? if @offset >= @read_buffer.bytesize swap_buffers end b = @read_buffer.getbyte(@offset) b = (b & 0x7f) - (b & 0x80) if signed @offset += 1 @length -= 1 b end |
#read_int ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cql/byte_buffer.rb', line 63 def read_int raise RangeError, "4 bytes required to read an int, but only #{@length} available" if @length < 4 if @offset >= @read_buffer.bytesize swap_buffers end if @read_buffer.bytesize >= @offset + 4 i0 = @read_buffer.getbyte(@offset + 0) i1 = @read_buffer.getbyte(@offset + 1) i2 = @read_buffer.getbyte(@offset + 2) i3 = @read_buffer.getbyte(@offset + 3) @offset += 4 @length -= 4 else i0 = read_byte i1 = read_byte i2 = read_byte i3 = read_byte end (i0 << 24) | (i1 << 16) | (i2 << 8) | i3 end |
#read_short ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cql/byte_buffer.rb', line 84 def read_short raise RangeError, "2 bytes required to read a short, but only #{@length} available" if @length < 2 if @offset >= @read_buffer.bytesize swap_buffers end if @read_buffer.bytesize >= @offset + 2 i0 = @read_buffer.getbyte(@offset + 0) i1 = @read_buffer.getbyte(@offset + 1) @offset += 2 @length -= 2 else i0 = read_byte i1 = read_byte end (i0 << 8) | i1 end |
#to_str ⇒ Object Also known as: to_s
148 149 150 |
# File 'lib/cql/byte_buffer.rb', line 148 def to_str (@read_buffer + @write_buffer)[@offset, @length] end |
#update(location, bytes) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/cql/byte_buffer.rb', line 113 def update(location, bytes) absolute_offset = @offset + location bytes_length = bytes.bytesize if absolute_offset >= @read_buffer.bytesize @write_buffer[absolute_offset - @read_buffer.bytesize, bytes_length] = bytes else overflow = absolute_offset + bytes_length - @read_buffer.bytesize read_buffer_portion = bytes_length - overflow @read_buffer[absolute_offset, read_buffer_portion] = bytes[0, read_buffer_portion] if overflow > 0 @write_buffer[0, overflow] = bytes[read_buffer_portion, bytes_length - 1] end end end |