Class: ReDNS::Buffer
- Inherits:
-
String
- Object
- String
- ReDNS::Buffer
- Defined in:
- lib/redns/buffer.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#size ⇒ Object
(also: #total_size, #length)
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #advance(chars = 1) ⇒ Object
- #append(contents, length = nil) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(contents = nil, offset = nil, size = nil) ⇒ Buffer
constructor
Create a buffer with arbitrary String contents.
- #inspect ⇒ Object
- #pack(format, *contents) ⇒ Object
- #read(chars = 1) ⇒ Object
- #restore_state {|_self| ... } ⇒ Object
- #rewind(chars = nil) ⇒ Object
- #slice(chars = 1) ⇒ Object
- #to_s ⇒ Object
- #unpack(format) ⇒ Object
- #write(contents, length = nil) ⇒ Object
Constructor Details
#initialize(contents = nil, offset = nil, size = nil) ⇒ Buffer
Create a buffer with arbitrary String contents. The offset parameter indicates where to start reading, which defaults to character 0 at the start of the string. The size parameter is used to limit how much of the content is used
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/redns/buffer.rb', line 22 def initialize(contents = nil, offset = nil, size = nil) if (contents.respond_to?(:serialize)) super('') @offset = 0 @size = string_length contents.serialize(self) else super(contents || '') @offset = offset ? offset.to_i : 0 @size = size ? size.to_i : string_length end advance(0) end |
Instance Attribute Details
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
9 10 11 |
# File 'lib/redns/buffer.rb', line 9 def offset @offset end |
#size ⇒ Object (readonly) Also known as: total_size, length
Returns the value of attribute size.
10 11 12 |
# File 'lib/redns/buffer.rb', line 10 def size @size end |
Instance Method Details
#advance(chars = 1) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/redns/buffer.rb', line 96 def advance(chars = 1) if (chars < 0) rewind(-chars) else @offset += chars if (@offset > string_length) @offset = string_length end max_length = (string_length - @offset) if (!@size or @size > max_length) @size = max_length end end @size ||= string_length - @offset self end |
#append(contents, length = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/redns/buffer.rb', line 86 def append(contents, length = nil) insertion = length ? contents[0, length] : contents self << insertion @size += insertion.length self end |
#empty? ⇒ Boolean
40 41 42 |
# File 'lib/redns/buffer.rb', line 40 def empty? @offset >= string_length end |
#inspect ⇒ Object
151 152 153 |
# File 'lib/redns/buffer.rb', line 151 def inspect "\#<#{self.class} [#{to_s.bytes.collect { |c| '%02x' % c }.join(' ')}]>" end |
#pack(format, *contents) ⇒ Object
57 58 59 |
# File 'lib/redns/buffer.rb', line 57 def pack(format, *contents) append(contents.flatten.pack(format)) end |
#read(chars = 1) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/redns/buffer.rb', line 70 def read(chars = 1) return if (@offset + chars > string_length) result = to_str[@offset, chars] advance(chars) result end |
#restore_state {|_self| ... } ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/redns/buffer.rb', line 139 def restore_state offset = @offset size = @size yield(self) if (block_given?) @offset = offset @size = size end |
#rewind(chars = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/redns/buffer.rb', line 118 def rewind(chars = nil) if (chars) if (chars < 0) advance(-chars) else @offset -= chars @size += chars if (@offset < 0) @size += @offset @offset = 0 end end else @size += @offset @offset = 0 end self end |
#slice(chars = 1) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/redns/buffer.rb', line 61 def slice(chars = 1) return if (@offset + chars > string_length) result = to_str[@offset, chars] advance(chars) self.class.new(result) end |
#to_s ⇒ Object
147 148 149 |
# File 'lib/redns/buffer.rb', line 147 def to_s to_str[@offset, @size] end |
#unpack(format) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/redns/buffer.rb', line 44 def unpack(format) return [ ] if (@size <= 0) return if (@offset > string_length) data = to_s.unpack(format) advance(data.pack(format).length) data rescue TypeError [ ] end |
#write(contents, length = nil) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/redns/buffer.rb', line 78 def write(contents, length = nil) insertion = length ? contents[0, length] : contents self[@offset, 0] = insertion advance(insertion.length) end |