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 = nil) ⇒ String
Read bytes from memory.
-
#rewind ⇒ Integer
Rewind to the beginning of the handle.
-
#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
96 97 98 |
# File 'lib/cabriolet/system/memory_handle.rb', line 96 def close @closed = true end |
#closed? ⇒ Boolean
Check if the handle is closed
103 104 105 |
# File 'lib/cabriolet/system/memory_handle.rb', line 103 def closed? @closed end |
#read(bytes = nil) ⇒ String
Read bytes from memory
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cabriolet/system/memory_handle.rb', line 24 def read(bytes = nil) return "" if @pos >= @data.bytesize if bytes.nil? # Read all remaining data result = @data.byteslice(@pos..-1) || "" @pos = @data.bytesize else result = @data.byteslice(@pos, bytes) || "" @pos += result.bytesize end result end |
#rewind ⇒ Integer
Rewind to the beginning of the handle
89 90 91 |
# File 'lib/cabriolet/system/memory_handle.rb', line 89 def rewind @pos = 0 end |
#seek(offset, whence) ⇒ Integer
Seek to a position in memory
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cabriolet/system/memory_handle.rb', line 67 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
82 83 84 |
# File 'lib/cabriolet/system/memory_handle.rb', line 82 def tell @pos end |
#to_s ⇒ String Also known as: buffer
Get the complete data buffer
110 111 112 |
# File 'lib/cabriolet/system/memory_handle.rb', line 110 def to_s @data end |
#write(content) ⇒ Integer
Write bytes to memory
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cabriolet/system/memory_handle.rb', line 42 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 |