Class: Rnp::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/rnp/input.rb

Overview

Note:

When dealing with very large data sources, prefer Input.from_path which should be the most efficient. Input.from_io is likely to have more overhead.

Class used to feed data into RNP.

Examples:

input from a string

Rnp::Input.from_string('my data')

input from a file

Rnp::Input.from_path('/path/to/my/file')

input from a Ruby IO object

Rnp::Input.from_io(File.open('/path/to/file', 'rb'))

Constant Summary collapse

READER =
lambda do |reader, _ctx, buf, buf_len|
  begin
    data = reader.call(buf_len)
    return 0 unless data
    raise Rnp::Error, 'Read exceeded buffer size' if data.size > buf_len
    buf.write_bytes(data)
    return data.size
  rescue
    puts $ERROR_INFO
    return -1
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, reader = nil) ⇒ Input

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 Input.

Raises:



33
34
35
36
37
# File 'lib/rnp/input.rb', line 33

def initialize(ptr, reader = nil)
  raise Rnp::Error, 'NULL pointer' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
  @reader = reader
end

Instance Attribute Details

#ptrObject (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.



30
31
32
# File 'lib/rnp/input.rb', line 30

def ptr
  @ptr
end

Class Method Details

.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.



40
41
42
# File 'lib/rnp/input.rb', line 40

def self.destroy(ptr)
  LibRnp.rnp_input_destroy(ptr)
end

.from_callback(reader) ⇒ 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.



111
112
113
114
115
116
# File 'lib/rnp/input.rb', line 111

def self.from_callback(reader)
  pptr = FFI::MemoryPointer.new(:pointer)
  readercb = READER.curry[reader]
  Rnp.call_ffi(:rnp_input_from_callback, pptr, readercb, nil, nil)
  Input.new(pptr.read_pointer, readercb)
end

.from_io(io) ⇒ Input

Create an Input to read from an IO object.

Parameters:

  • io (IO, #read)

    the IO object

Returns:



73
74
75
# File 'lib/rnp/input.rb', line 73

def self.from_io(io)
  from_callback(io.method(:read))
end

.from_path(path) ⇒ Input

Create an Input to read from a path.

Parameters:

  • path (String)

    the path

Returns:



63
64
65
66
67
# File 'lib/rnp/input.rb', line 63

def self.from_path(path)
  pptr = FFI::MemoryPointer.new(:pointer)
  Rnp.call_ffi(:rnp_input_from_path, pptr, path)
  Input.new(pptr.read_pointer)
end

.from_string(data) ⇒ Input

Create an Input to read from a string.

Parameters:

  • data (String)

    the string data

Returns:



52
53
54
55
56
57
# File 'lib/rnp/input.rb', line 52

def self.from_string(data)
  pptr = FFI::MemoryPointer.new(:pointer)
  buf = FFI::MemoryPointer.from_data(data)
  Rnp.call_ffi(:rnp_input_from_memory, pptr, buf, buf.size, true)
  Input.new(pptr.read_pointer)
end

Instance Method Details

#inspectObject



44
45
46
# File 'lib/rnp/input.rb', line 44

def inspect
  Rnp.inspect_ptr(self)
end