Class: Buffer
Overview
Defined Under Namespace
Classes: EOF, Error
Class Method Summary
collapse
Instance Method Summary
collapse
#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
#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
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
38
39
40
|
# File 'lib/buffer.rb', line 38
def at_end?
@position == @size
end
|
#content ⇒ Object
42
43
44
|
# File 'lib/buffer.rb', line 42
def content
@content
end
|
#copy_from_stream(stream, n) ⇒ Object
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
|
#position ⇒ Object
29
30
31
|
# File 'lib/buffer.rb', line 29
def position
@position
end
|
#position=(new_pos) ⇒ Object
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
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_cstring ⇒ Object
returns a Ruby string without the trailing NUL character
78
79
80
81
82
83
84
85
86
|
# File 'lib/buffer.rb', line 78
def read_cstring
nul_pos = @content.index(0, @position)
raise Error, "no cstring found!" unless nul_pos
sz = nul_pos - @position
str = @content[@position, sz]
@position += sz + 1
return str
end
|
#read_rest ⇒ Object
read till the end of the buffer
89
90
91
|
# File 'lib/buffer.rb', line 89
def read_rest
read(self.size-@position)
end
|
#size ⇒ Object
25
26
27
|
# File 'lib/buffer.rb', line 25
def size
@size
end
|
#write(str) ⇒ Object
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
71
72
73
74
75
|
# File 'lib/buffer.rb', line 71
def write_cstring(cstr)
raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(0)
write(cstr)
write("\000")
end
|