Class: Rnp::Output
- Inherits:
-
Object
- Object
- Rnp::Output
- Defined in:
- lib/rnp/output.rb
Overview
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.
Constant Summary collapse
- WRITER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
lambda do |writer, _ctx, buf, buf_len| begin data = buf.read_bytes(buf_len) written = writer.call(data) return written == data.bytesize rescue puts $ERROR_INFO return false end end
Instance Attribute Summary collapse
- #ptr ⇒ Object readonly private
Class Method Summary collapse
- .default(output) {|output| ... } ⇒ Object private
- .destroy(ptr) ⇒ Object private
- .to_callback(writer) ⇒ Object private
-
.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
-
#initialize(ptr, writer = nil) ⇒ Output
constructor
private
A new instance of Output.
- #inspect ⇒ Object
-
#string ⇒ String?
Retrieve the data written.
Constructor Details
#initialize(ptr, writer = nil) ⇒ Output
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Output.
34 35 36 37 38 |
# File 'lib/rnp/output.rb', line 34 def initialize(ptr, writer = nil) raise Rnp::Error, 'NULL pointer' if ptr.null? @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy)) @writer = writer end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 |
# File 'lib/rnp/output.rb', line 31 def ptr @ptr end |
Class Method Details
.default(output) {|output| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
121 122 123 124 125 126 |
# File 'lib/rnp/output.rb', line 121 def self.default(output) to_str = output.nil? output = Output.to_string if to_str yield output output.string if to_str end |
.destroy(ptr) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
41 42 43 |
# File 'lib/rnp/output.rb', line 41 def self.destroy(ptr) LibRnp.rnp_output_destroy(ptr) end |
.to_callback(writer) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
113 114 115 116 117 118 |
# File 'lib/rnp/output.rb', line 113 def self.to_callback(writer) pptr = FFI::MemoryPointer.new(:pointer) writercb = WRITER.curry[writer] Rnp.call_ffi(:rnp_output_to_callback, pptr, writercb, nil, nil) Output.new(pptr.read_pointer, writercb) end |
.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.
92 93 94 95 96 97 98 |
# File 'lib/rnp/output.rb', line 92 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 |