Class: Rnp::Output
- Inherits:
-
Object
- Object
- Rnp::Output
- Defined in:
- lib/rnp/output.rb
Overview
Note:
When dealing with very large data, prefer Output.to_path which should be the most efficient. Output.to_io is likely to have more overhead.
Class used to feed data out of RNP.
Class Method Summary collapse
-
.to_io(io) ⇒ Output
Create an Output to write to an IO object.
-
.to_null ⇒ Output
Create an Output to discard all writes.
-
.to_path(path) ⇒ Output
Create an Output to write to a path.
-
.to_string(max_alloc = 0) ⇒ Output
Create an Output to write to a string.
Instance Method Summary collapse
- #inspect ⇒ Object
-
#string ⇒ String?
Retrieve the data written.
-
#write(*strings) ⇒ Integer
Write to the output.
Class Method Details
.to_io(io) ⇒ Output
Create an Output to write to an IO object.
85 86 87 |
# File 'lib/rnp/output.rb', line 85 def self.to_io(io) to_callback(io.method(:write)) end |
.to_null ⇒ Output
Create an Output to discard all writes.
75 76 77 78 79 |
# File 'lib/rnp/output.rb', line 75 def self.to_null pptr = FFI::MemoryPointer.new(:pointer) Rnp.call_ffi(:rnp_output_to_null, pptr) Output.new(pptr.read_pointer) end |
.to_path(path) ⇒ Output
Create an Output to write to a path.
66 67 68 69 70 |
# File 'lib/rnp/output.rb', line 66 def self.to_path(path) pptr = FFI::MemoryPointer.new(:pointer) Rnp.call_ffi(:rnp_output_to_path, pptr, path) Output.new(pptr.read_pointer) end |
.to_string(max_alloc = 0) ⇒ Output
Create an Output to write to a string.
The resulting string can later be retrieved with #string.
56 57 58 59 60 |
# File 'lib/rnp/output.rb', line 56 def self.to_string(max_alloc = 0) pptr = FFI::MemoryPointer.new(:pointer) Rnp.call_ffi(:rnp_output_to_memory, pptr, max_alloc) Output.new(pptr.read_pointer) end |
Instance Method Details
#inspect ⇒ Object
45 46 47 |
# File 'lib/rnp/output.rb', line 45 def inspect Rnp.inspect_ptr(self) end |
#string ⇒ String?
Retrieve the data written. Only valid for #to_string.
106 107 108 109 110 111 112 |
# File 'lib/rnp/output.rb', line 106 def string pptr = FFI::MemoryPointer.new(:pointer) len = FFI::MemoryPointer.new(:size_t) Rnp.call_ffi(:rnp_output_memory_get_buf, @ptr, pptr, len, false) buf = pptr.read_pointer buf.read_bytes(len.read(:size_t)) unless buf.null? end |
#write(*strings) ⇒ Integer
Write to the output.
93 94 95 96 97 98 99 100 101 |
# File 'lib/rnp/output.rb', line 93 def write(*strings) total_written = 0 pwritten = FFI::MemoryPointer.new(:size_t) strings.each do |string| Rnp.call_ffi(:rnp_output_write, @ptr, string, string.size, pwritten) total_written += pwritten.read(:size_t) end total_written end |