Class: Buffer

Inherits:
Object
  • Object
show all
Includes:
BinaryReaderMixin, BinaryWriterMixin
Defined in:
lib/buffer.rb

Overview

Fixed size buffer.

Defined Under Namespace

Classes: EOF, Error

Constant Summary collapse

NUL =
"\000"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BinaryReaderMixin

#read_int16_big, #read_int16_little, #read_int16_native, #read_int32_big, #read_int32_little, #read_int32_native, #read_int8, #read_word16_big, #read_word16_little, #read_word16_native, #read_word32_big, #read_word32_little, #read_word32_native, #read_word8, #readn

Methods included from BinaryWriterMixin

#write_int16_little, #write_int16_native, #write_int16_network, #write_int32_little, #write_int32_native, #write_int32_network, #write_int8, #write_word16_little, #write_word16_native, #write_word16_network, #write_word32_little, #write_word32_native, #write_word32_network, #write_word8

Constructor Details

#initialize(content) ⇒ Buffer

Returns a new instance of Buffer.


19
20
21
22
23
# File 'lib/buffer.rb', line 19

def initialize(content)
  @size = content.size
  @content = content
  @position = 0
end

Class Method Details

.from_string(str) ⇒ Object


10
11
12
# File 'lib/buffer.rb', line 10

def self.from_string(str)
  new(str)
end

.of_size(size) ⇒ Object

Raises:

  • (ArgumentError)

14
15
16
17
# File 'lib/buffer.rb', line 14

def self.of_size(size)
  raise ArgumentError if size < 0
  new('#' * size)
end

Instance Method Details

#at_end?Boolean

Returns:

  • (Boolean)

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

def at_end?
  @position == @size
end

#contentObject


42
43
44
# File 'lib/buffer.rb', line 42

def content
  @content
end

#copy_from_stream(stream, n) ⇒ Object

Raises:

  • (ArgumentError)

61
62
63
64
65
66
67
68
69
# File 'lib/buffer.rb', line 61

def copy_from_stream(stream, n)
  raise ArgumentError if n < 0
  while n > 0
    str = stream.read(n) 
    write(str)
    n -= str.size
  end
  raise if n < 0 
end

#positionObject


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

def position
  @position
end

#position=(new_pos) ⇒ Object

Raises:

  • (ArgumentError)

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

def position=(new_pos)
  raise ArgumentError if new_pos < 0 or new_pos > @size
  @position = new_pos
end

#read(n) ⇒ Object

Raises:


46
47
48
49
50
51
# File 'lib/buffer.rb', line 46

def read(n)
  raise EOF, 'cannot read beyond the end of buffer' if @position + n > @size
  str = @content[@position, n]
  @position += n
  str
end

#read_cstringObject

returns a Ruby string without the trailing NUL character

Raises:


80
81
82
83
84
85
86
87
88
# File 'lib/buffer.rb', line 80

def read_cstring
  nul_pos = @content.index(NUL, @position)
  raise Error, "no cstring found!" unless nul_pos

  sz = nul_pos - @position
  str = @content[@position, sz]
  @position += sz + 1
  return str
end

#read_restObject

read till the end of the buffer


91
92
93
# File 'lib/buffer.rb', line 91

def read_rest
  read(self.size-@position)
end

#sizeObject


25
26
27
# File 'lib/buffer.rb', line 25

def size
  @size
end

#write(str) ⇒ Object

Raises:


53
54
55
56
57
58
59
# File 'lib/buffer.rb', line 53

def write(str)
  sz = str.size
  raise EOF, 'cannot write beyond the end of buffer' if @position + sz > @size
  @content[@position, sz] = str
  @position += sz
  self
end

#write_cstring(cstr) ⇒ Object

Raises:

  • (ArgumentError)

73
74
75
76
77
# File 'lib/buffer.rb', line 73

def write_cstring(cstr)
  raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(NUL)
  write(cstr)
  write(NUL)
end