Class: Cabriolet::System::MemoryHandle
- Inherits:
-
Object
- Object
- Cabriolet::System::MemoryHandle
- Defined in:
- lib/cabriolet/system/memory_handle.rb
Overview
MemoryHandle provides in-memory I/O operations using a StringIO-like interface
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#close ⇒ void
Close the handle.
-
#closed? ⇒ Boolean
Check if the handle is closed.
-
#initialize(data = "", mode = Constants::MODE_READ) ⇒ MemoryHandle
constructor
Initialize a new memory handle.
-
#read(bytes) ⇒ String
Read bytes from memory.
-
#seek(offset, whence) ⇒ Integer
Seek to a position in memory.
-
#tell ⇒ Integer
Get current position in memory.
-
#to_s ⇒ String
(also: #buffer)
Get the complete data buffer.
-
#write(content) ⇒ Integer
Write bytes to memory.
Constructor Details
#initialize(data = "", mode = Constants::MODE_READ) ⇒ MemoryHandle
Initialize a new memory handle
13 14 15 16 17 18 |
# File 'lib/cabriolet/system/memory_handle.rb', line 13 def initialize(data = "", mode = Constants::MODE_READ) @data = data.dup.force_encoding(Encoding::BINARY) @mode = mode @pos = mode == Constants::MODE_APPEND ? @data.bytesize : 0 @closed = false end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/cabriolet/system/memory_handle.rb', line 7 def data @data end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
7 8 9 |
# File 'lib/cabriolet/system/memory_handle.rb', line 7 def mode @mode end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the handle
83 84 85 |
# File 'lib/cabriolet/system/memory_handle.rb', line 83 def close @closed = true end |
#closed? ⇒ Boolean
Check if the handle is closed
90 91 92 |
# File 'lib/cabriolet/system/memory_handle.rb', line 90 def closed? @closed end |
#read(bytes) ⇒ String
Read bytes from memory
24 25 26 27 28 29 30 |
# File 'lib/cabriolet/system/memory_handle.rb', line 24 def read(bytes) return "" if @pos >= @data.bytesize result = @data.byteslice(@pos, bytes) || "" @pos += result.bytesize result end |
#seek(offset, whence) ⇒ Integer
Seek to a position in memory
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cabriolet/system/memory_handle.rb', line 61 def seek(offset, whence) new_pos = case whence when Constants::SEEK_START then offset when Constants::SEEK_CUR then @pos + offset when Constants::SEEK_END then @data.bytesize + offset else raise ArgumentError, "Invalid whence value: #{whence}" end @pos = [[new_pos, 0].max, @data.bytesize].min end |
#tell ⇒ Integer
Get current position in memory
76 77 78 |
# File 'lib/cabriolet/system/memory_handle.rb', line 76 def tell @pos end |
#to_s ⇒ String Also known as: buffer
Get the complete data buffer
97 98 99 |
# File 'lib/cabriolet/system/memory_handle.rb', line 97 def to_s @data end |
#write(content) ⇒ Integer
Write bytes to memory
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cabriolet/system/memory_handle.rb', line 36 def write(content) raise IOError, "Handle is closed" if @closed raise IOError, "Handle not opened for writing" if @mode == Constants::MODE_READ content = content.dup.force_encoding(Encoding::BINARY) if @pos >= @data.bytesize # Append to end @data << content else # Overwrite existing data before = @data.byteslice(0, @pos) || "" after = @data.byteslice((@pos + content.bytesize)..-1) || "" @data = before + content + after end @pos += content.bytesize content.bytesize end |