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.



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

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.



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

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.



793
794
795
796
797
798
799
# File 'lib/gpgme.rb', line 793

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.



788
789
790
# File 'lib/gpgme.rb', line 788

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

.from_str(buf, copy = true) ⇒ Object

Create a new instance with internal buffer.



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

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.



757
758
759
760
761
762
763
764
765
766
767
# File 'lib/gpgme.rb', line 757

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.



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

def encoding
  GPGME::gpgme_data_get_encoding(self)
end

#encoding=(encoding) ⇒ Object

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



844
845
846
847
848
849
# File 'lib/gpgme.rb', line 844

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.



812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/gpgme.rb', line 812

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.



828
829
830
# File 'lib/gpgme.rb', line 828

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.



833
834
835
# File 'lib/gpgme.rb', line 833

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