class Puppet::Pops::Types::PBinaryType::Binary
Represents a binary buffer @api public
Attributes
Public Class Methods
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 26 def self.from_base64(str) 27 new(Base64.decode64(str)) 28 end
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 42 def self.from_base64_strict(str) 43 new(Base64.strict_decode64(str)) 44 end
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 33 def self.from_base64_urlsafe(str) 34 new(Base64.urlsafe_decode64(str)) 35 end
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 57 def self.from_binary_string(bin) 58 new(bin) 59 end
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 74 def self.from_string(encoded_string) 75 enc = encoded_string.encoding.name 76 unless encoded_string.valid_encoding? 77 raise ArgumentError, _("The given string in encoding '%{enc}' is invalid. Cannot create a Binary UTF-8 representation") % { enc: enc } 78 end 79 # Convert to UTF-8 (if not already UTF-8), and then to binary 80 encoded_string = (enc == "UTF-8") ? encoded_string.dup : encoded_string.encode('UTF-8') 81 encoded_string.force_encoding("ASCII-8BIT") 82 new(encoded_string) 83 end
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 93 def initialize(bin) 94 @binary_buffer = (bin.encoding.name == "ASCII-8BIT" ? bin : bin.b).freeze 95 end
Public Instance Methods
# File lib/puppet/pops/types/p_binary_type.rb 123 def ==(o) 124 self.eql?(o) 125 end
# File lib/puppet/pops/types/p_binary_type.rb 119 def eql?(o) 120 self.class == o.class && @binary_buffer == o.binary_buffer 121 end
# File lib/puppet/pops/types/p_binary_type.rb 115 def hash 116 @binary_buffer.hash 117 end
# File lib/puppet/pops/types/p_binary_type.rb 127 def length() 128 @binary_buffer.length 129 end
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 105 def relaxed_to_s 106 Base64.encode64(@binary_buffer) 107 end
Presents the binary content as a string base64 encoded string (without line breaks).
# File lib/puppet/pops/types/p_binary_type.rb 99 def to_s 100 Base64.strict_encode64(@binary_buffer) 101 end
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 111 def urlsafe_to_s 112 Base64.urlsafe_encode64(@binary_buffer) 113 end