class Puppet::Pops::Types::PBinaryType::Binary

Represents a binary buffer @api public

Attributes

binary_buffer[R]

Public Class Methods

from_base64(str) click to toggle source

Constructs an instance of Binary from a base64 urlsafe encoded string (RFC 2045). @param [String] A string with RFC 2045 compliant encoded binary

   # File lib/puppet/pops/types/p_binary_type.rb
27 def self.from_base64(str)
28   new(Base64.decode64(str))
29 end
from_base64_strict(str) click to toggle source

Constructs an instance of Binary from a base64 strict encoded string (RFC 4648) Where correct padding must be used and line breaks causes an error to be raised.

@param [String] A string with RFC 4648 compliant encoded binary

   # File lib/puppet/pops/types/p_binary_type.rb
43 def self.from_base64_strict(str)
44   new(Base64.strict_decode64(str))
45 end
from_base64_urlsafe(str) click to toggle source

Constructs an instance of Binary from a base64 encoded string (RFC4648 with “URL and Filename Safe Alphabet” (That is with '-' instead of '+', and '_' instead of '/').

   # File lib/puppet/pops/types/p_binary_type.rb
34 def self.from_base64_urlsafe(str)
35   new(Base64.urlsafe_decode64(str))
36 end
from_binary_string(bin) click to toggle source

Creates a new Binary from a String containing binary data. If the string's encoding is not already ASCII-8BIT, a copy of the string is force encoded as ASCII-8BIT (that is Ruby's “binary” format). This means that this binary will have the exact same content, but the string will considered to hold a sequence of bytes in the range 0 to 255.

The given string will be frozen as a side effect if it is in ASCII-8BIT encoding. If this is not wanted, a copy should be given to this method.

@param [String] A string with binary data @api public

   # File lib/puppet/pops/types/p_binary_type.rb
58 def self.from_binary_string(bin)
59   new(bin)
60 end
from_string(encoded_string) click to toggle source

Creates a new Binary from a String containing text/binary in its given encoding. If the string's encoding is not already UTF-8, the string is first transcoded to UTF-8. This means that this binary will have the UTF-8 byte representation of the original string. For this to be valid, the encoding used in the given string must be valid. The validity of the given string is therefore asserted.

The given string will be frozen as a side effect if it is in ASCII-8BIT encoding. If this is not wanted, a copy should be given to this method.

@param [String] A string with valid content in its given encoding @return [Puppet::Pops::Types::PBinaryType::Binary] with the UTF-8 representation of the UTF-8 transcoded string @api public

   # File lib/puppet/pops/types/p_binary_type.rb
75 def self.from_string(encoded_string)
76   enc = encoded_string.encoding.name
77   unless encoded_string.valid_encoding?
78     raise ArgumentError, _("The given string in encoding '%{enc}' is invalid. Cannot create a Binary UTF-8 representation") % { enc: enc }
79   end
80   # Convert to UTF-8 (if not already UTF-8), and then to binary
81   encoded_string = (enc == "UTF-8") ? encoded_string.dup : encoded_string.encode('UTF-8')
82   encoded_string.force_encoding("ASCII-8BIT")
83   new(encoded_string)
84 end
new(bin) click to toggle source

Creates a new Binary from a String containing raw binary data of unknown encoding. If the string's encoding is not already ASCII-8BIT, a copy of the string is forced to ASCII-8BIT (that is Ruby's “binary” format). This means that this binary will have the exact same content, but the string will considered to hold a sequence of bytes in the range 0 to 255.

@param [String] A string with binary data @api private

   # File lib/puppet/pops/types/p_binary_type.rb
94 def initialize(bin)
95   @binary_buffer = (bin.encoding.name == "ASCII-8BIT" ? bin : bin.b).freeze
96 end

Public Instance Methods

==(o) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
124 def ==(o)
125   self.eql?(o)
126 end
eql?(o) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
120 def eql?(o)
121   self.class == o.class && @binary_buffer == o.binary_buffer
122 end
hash() click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
116 def hash
117   @binary_buffer.hash
118 end
length() click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
128 def length()
129   @binary_buffer.length
130 end
relaxed_to_s() click to toggle source

Returns the binary content as a “relaxed” base64 (standard) encoding where the string is broken up with new lines.

    # File lib/puppet/pops/types/p_binary_type.rb
106 def relaxed_to_s
107   Base64.encode64(@binary_buffer)
108 end
to_s() click to toggle source

Presents the binary content as a string base64 encoded string (without line breaks).

    # File lib/puppet/pops/types/p_binary_type.rb
100 def to_s
101   Base64.strict_encode64(@binary_buffer)
102 end
urlsafe_to_s() click to toggle source

Returns the binary content as a url safe base64 string (where + and / are replaced by - and _)

    # File lib/puppet/pops/types/p_binary_type.rb
112 def urlsafe_to_s
113   Base64.urlsafe_encode64(@binary_buffer)
114 end