Class: Baykit::BayServer::Util::SimpleBuffer

Inherits:
Object
  • Object
show all
Includes:
Reusable
Defined in:
lib/baykit/bayserver/util/simple_buffer.rb

Constant Summary collapse

INITIAL_BUFFER_SIZE =
32768

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = INITIAL_BUFFER_SIZE) ⇒ SimpleBuffer

Returns a new instance of SimpleBuffer.



15
16
17
18
19
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 15

def initialize(init=INITIAL_BUFFER_SIZE)
  @capacity = init
  @buf = StringUtil.alloc(@capacity)
  @length = 0
end

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



11
12
13
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 11

def buf
  @buf
end

#capacityObject (readonly)

Returns the value of attribute capacity.



13
14
15
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 13

def capacity
  @capacity
end

#lengthObject (readonly)

Returns the value of attribute length.



12
13
14
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 12

def length
  @length
end

Instance Method Details

#bytesObject



21
22
23
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 21

def bytes()
  return buf
end

#extend_bufObject



46
47
48
49
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 46

def extend_buf()
  @capacity *= 2
  @buf = StringUtil.realloc(@buf, @capacity)
end

#put(bytes, pos = 0, len = bytes.length) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 35

def put(bytes, pos=0, len=bytes.length)
  while @length + len > capacity
    extend_buf
  end

  len.times do |i|
    @buf[@length + i] = bytes[pos + i]
  end
  @length += len
end

#put_byte(b) ⇒ Object



31
32
33
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 31

def put_byte(b)
  put([b], 0, 1);
end

#resetObject



25
26
27
28
29
# File 'lib/baykit/bayserver/util/simple_buffer.rb', line 25

def reset()
  # clear for security reason
  @buf.clear()
  @length = 0
end