Class: Puppet::Pops::Types::PBinaryType::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/p_binary_type.rb

Overview

Represents a binary buffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin) ⇒ Binary

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.

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.

Parameters:

  • A (String)

    string with binary data



93
94
95
# File 'lib/puppet/pops/types/p_binary_type.rb', line 93

def initialize(bin)
  @binary_buffer = (bin.encoding.name == "ASCII-8BIT" ? bin : bin.b).freeze
end

Instance Attribute Details

#binary_bufferObject (readonly)



21
22
23
# File 'lib/puppet/pops/types/p_binary_type.rb', line 21

def binary_buffer
  @binary_buffer
end

Class Method Details

.from_base64(str) ⇒ Object

Constructs an instance of Binary from a base64 urlsafe encoded string (RFC 2045).

Parameters:

  • A (String)

    string with RFC 2045 compliant encoded binary



26
27
28
# File 'lib/puppet/pops/types/p_binary_type.rb', line 26

def self.from_base64(str)
  new(Base64.decode64(str))
end

.from_base64_strict(str) ⇒ Object

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.

Parameters:

  • A (String)

    string with RFC 4648 compliant encoded binary



42
43
44
# File 'lib/puppet/pops/types/p_binary_type.rb', line 42

def self.from_base64_strict(str)
  new(Base64.strict_decode64(str))
end

.from_base64_urlsafe(str) ⇒ Object

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 ‘/’).



33
34
35
# File 'lib/puppet/pops/types/p_binary_type.rb', line 33

def self.from_base64_urlsafe(str)
  new(Base64.urlsafe_decode64(str))
end

.from_binary_string(bin) ⇒ Object

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.

Parameters:

  • A (String)

    string with binary data



57
58
59
# File 'lib/puppet/pops/types/p_binary_type.rb', line 57

def self.from_binary_string(bin)
  new(bin)
end

.from_string(encoded_string) ⇒ Puppet::Pops::Types::PBinaryType::Binary

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.

Parameters:

  • A (String)

    string with valid content in its given encoding

Returns:



74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/pops/types/p_binary_type.rb', line 74

def self.from_string(encoded_string)
  enc = encoded_string.encoding.name
  unless encoded_string.valid_encoding?
    raise ArgumentError, _("The given string in encoding '%{enc}' is invalid. Cannot create a Binary UTF-8 representation") % { enc: enc }
  end
  # Convert to UTF-8 (if not already UTF-8), and then to binary
  encoded_string = (enc == "UTF-8") ? encoded_string.dup : encoded_string.encode('UTF-8')
  encoded_string.force_encoding("ASCII-8BIT")
  new(encoded_string)
end

Instance Method Details

#==(o) ⇒ Object



123
124
125
# File 'lib/puppet/pops/types/p_binary_type.rb', line 123

def ==(o)
  self.eql?(o)
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/puppet/pops/types/p_binary_type.rb', line 119

def eql?(o)
  self.class == o.class && @binary_buffer == o.binary_buffer
end

#hashObject



115
116
117
# File 'lib/puppet/pops/types/p_binary_type.rb', line 115

def hash
  @binary_buffer.hash
end

#lengthObject



127
128
129
# File 'lib/puppet/pops/types/p_binary_type.rb', line 127

def length()
  @binary_buffer.length
end

#relaxed_to_sObject

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



105
106
107
# File 'lib/puppet/pops/types/p_binary_type.rb', line 105

def relaxed_to_s
  Base64.encode64(@binary_buffer)
end

#to_sObject

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



99
100
101
# File 'lib/puppet/pops/types/p_binary_type.rb', line 99

def to_s
  Base64.strict_encode64(@binary_buffer)
end

#urlsafe_to_sObject

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



111
112
113
# File 'lib/puppet/pops/types/p_binary_type.rb', line 111

def urlsafe_to_s
  Base64.urlsafe_encode64(@binary_buffer)
end