Class: GPGME::Data

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

Overview

A class for managing data buffers.

Constant Summary collapse

BLOCK_SIZE =
4096

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emptyObject

Create a new instance with an empty buffer.



780
781
782
783
784
785
786
# File 'lib/gpgme.rb', line 780

def self.empty
  rdh = Array.new
  err = GPGME::gpgme_data_new(rdh)
  exc = GPGME::error_to_exception(err)
  raise exc if exc
  rdh[0]
end

.from_callbacks(callbacks, hook_value = nil) ⇒ Object

Create a new instance from the specified callbacks.



812
813
814
815
816
817
818
# File 'lib/gpgme.rb', line 812

def self.from_callbacks(callbacks, hook_value = nil)
  rdh = Array.new
  err = GPGME::gpgme_data_new_from_cbs(rdh, callbacks, hook_value)
  exc = GPGME::error_to_exception(err)
  raise exc if exc
  rdh[0]
end

.from_fd(fd) ⇒ Object

Create a new instance from the specified file descriptor.



803
804
805
806
807
808
809
# File 'lib/gpgme.rb', line 803

def self.from_fd(fd)
  rdh = Array.new
  err = GPGME::gpgme_data_new_from_fd(rdh, fd)
  exc = GPGME::error_to_exception(err)
  raise exc if exc
  rdh[0]
end

.from_io(io) ⇒ Object

Create a new instance associated with a given IO.



798
799
800
# File 'lib/gpgme.rb', line 798

def self.from_io(io)
  from_callbacks(IOCallbacks.new(arg))
end

.from_str(buf, copy = true) ⇒ Object

Create a new instance with internal buffer.



789
790
791
792
793
794
795
# File 'lib/gpgme.rb', line 789

def self.from_str(buf, copy = true)
  rdh = Array.new
  err = GPGME::gpgme_data_new_from_mem(rdh, buf, buf.length)
  exc = GPGME::error_to_exception(err)
  raise exc if exc
  rdh[0]
end

.new(arg = nil, copy = false) ⇒ Object

Create a new instance.

The created data types depend on arg. If arg is nil, it creates an instance with an empty buffer. Otherwise, arg is either a string, an IO, or a Pathname.



767
768
769
770
771
772
773
774
775
776
777
# File 'lib/gpgme.rb', line 767

def self.new(arg = nil, copy = false)
  if arg.nil?
    return empty
  elsif arg.respond_to? :to_str
    return from_str(arg.to_str, copy)
  elsif arg.respond_to? :to_io
    return from_io(arg.to_io)
  elsif arg.respond_to? :open
    return from_io(arg.open)
  end
end

Instance Method Details

#encodingObject

Return the encoding of the underlying data.



848
849
850
# File 'lib/gpgme.rb', line 848

def encoding
  GPGME::gpgme_data_get_encoding(self)
end

#encoding=(encoding) ⇒ Object

Set the encoding to a given encoding of the underlying data object.



854
855
856
857
858
859
# File 'lib/gpgme.rb', line 854

def encoding=(encoding)
  err = GPGME::gpgme_data_set_encoding(self, encoding)
  exc = GPGME::error_to_exception(err)
  raise exc if exc
  encoding
end

#read(length = nil) ⇒ Object

Read at most length bytes from the data object, or to the end of file if length is omitted or is nil.



822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/gpgme.rb', line 822

def read(length = nil)
  if length
	GPGME::gpgme_data_read(self, length)
  else
	buf = String.new
    loop do
      s = GPGME::gpgme_data_read(self, BLOCK_SIZE)
      break unless s
      buf << s
    end
    buf
  end
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object

Seek to a given offset in the data object according to the value of whence.



838
839
840
# File 'lib/gpgme.rb', line 838

def seek(offset, whence = IO::SEEK_SET)
  GPGME::gpgme_data_seek(self, offset, IO::SEEK_SET)
end

#write(buffer, length = buffer.length) ⇒ Object

Write length bytes from buffer into the data object.



843
844
845
# File 'lib/gpgme.rb', line 843

def write(buffer, length = buffer.length)
  GPGME::gpgme_data_write(self, buffer, length)
end