Class: Cql::ByteBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/cql/byte_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_bytes = '') ⇒ ByteBuffer

Returns a new instance of ByteBuffer.



5
6
7
8
9
10
11
# File 'lib/cql/byte_buffer.rb', line 5

def initialize(initial_bytes='')
  @read_buffer = ''
  @write_buffer = ''
  @offset = 0
  @length = 0
  append(initial_bytes) unless initial_bytes.empty?
end

Instance Attribute Details

#lengthObject (readonly) Also known as: size, bytesize

Returns the value of attribute length.



13
14
15
# File 'lib/cql/byte_buffer.rb', line 13

def length
  @length
end

Instance Method Details

#append(bytes) ⇒ Object Also known as: <<



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cql/byte_buffer.rb', line 21

def append(bytes)
  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
  self
end

#cheap_peekObject



123
124
125
126
127
128
# File 'lib/cql/byte_buffer.rb', line 123

def cheap_peek
  if @offset >= @read_buffer.bytesize
    swap_buffers
  end
  @read_buffer[@offset, @read_buffer.bytesize - @offset]
end

#discard(n) ⇒ Object

Raises:

  • (RangeError)


34
35
36
37
38
39
# File 'lib/cql/byte_buffer.rb', line 34

def discard(n)
  raise RangeError, "#{n} bytes to discard but only #{@length} available" if @length < n
  @offset += n
  @length -= n
  self
end

#dupObject



139
140
141
# File 'lib/cql/byte_buffer.rb', line 139

def dup
  self.class.new(to_str)
end

#empty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/cql/byte_buffer.rb', line 17

def empty?
  length == 0
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


130
131
132
# File 'lib/cql/byte_buffer.rb', line 130

def eql?(other)
  self.to_str.eql?(other.to_str)
end

#hashObject



135
136
137
# File 'lib/cql/byte_buffer.rb', line 135

def hash
  to_str.hash
end

#inspectObject



148
149
150
# File 'lib/cql/byte_buffer.rb', line 148

def inspect
  %(#<#{self.class.name}: #{to_str.inspect}>)
end

#read(n) ⇒ Object

Raises:

  • (RangeError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cql/byte_buffer.rb', line 41

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

Raises:

  • (RangeError)


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cql/byte_buffer.rb', line 96

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_intObject

Raises:

  • (RangeError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cql/byte_buffer.rb', line 58

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_shortObject

Raises:

  • (RangeError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cql/byte_buffer.rb', line 79

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_strObject Also known as: to_s



143
144
145
# File 'lib/cql/byte_buffer.rb', line 143

def to_str
  (@read_buffer + @write_buffer)[@offset, @length]
end

#update(location, bytes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cql/byte_buffer.rb', line 108

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