Class: Puppet::Pops::Types::PBinaryType::Binary
- Defined in:
- lib/puppet/pops/types/p_binary_type.rb
Overview
Represents a binary buffer
Instance Attribute Summary collapse
- #binary_buffer ⇒ Object readonly
Class Method Summary collapse
-
.from_base64(str) ⇒ Object
Constructs an instance of Binary from a base64 urlsafe encoded string (RFC 2045).
-
.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.
-
.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 ‘/’)..
-
.from_binary_string(bin) ⇒ Object
Creates a new Binary from a String containing binary data.
-
.from_string(encoded_string) ⇒ Puppet::Pops::Types::PBinaryType::Binary
Creates a new Binary from a String containing text/binary in its given encoding.
Instance Method Summary collapse
- #==(o) ⇒ Object
- #eql?(o) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(bin) ⇒ Binary
constructor
private
Creates a new Binary from a String containing raw binary data of unknown encoding.
- #length ⇒ Object
-
#relaxed_to_s ⇒ Object
Returns the binary content as a “relaxed” base64 (standard) encoding where the string is broken up with new lines.
-
#to_s ⇒ Object
Presents the binary content as a string base64 encoded string (without line breaks).
-
#urlsafe_to_s ⇒ Object
Returns the binary content as a url safe base64 string (where + and / are replaced by - and _).
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.
86 87 88 89 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 86 def initialize(bin) # TODO: When Ruby 1.9.3 support is dropped change this to `bin.b` for binary encoding instead of force_encoding @binary_buffer = (bin.encoding.name == "ASCII-8BIT" ? bin : bin.dup.force_encoding("ASCII-8BIT")).freeze end |
Instance Attribute Details
#binary_buffer ⇒ Object (readonly)
14 15 16 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 14 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).
19 20 21 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 19 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.
35 36 37 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 35 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 ‘/’).
26 27 28 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 26 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.
50 51 52 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 50 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.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 67 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
117 118 119 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 117 def ==(o) self.eql?(o) end |
#eql?(o) ⇒ Boolean
113 114 115 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 113 def eql?(o) self.class == o.class && @binary_buffer == o.binary_buffer end |
#hash ⇒ Object
109 110 111 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 109 def hash @binary_buffer.hash end |
#length ⇒ Object
121 122 123 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 121 def length() @binary_buffer.length end |
#relaxed_to_s ⇒ Object
Returns the binary content as a “relaxed” base64 (standard) encoding where the string is broken up with new lines.
99 100 101 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 99 def relaxed_to_s Base64.encode64(@binary_buffer) end |
#to_s ⇒ Object
Presents the binary content as a string base64 encoded string (without line breaks).
93 94 95 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 93 def to_s Base64.strict_encode64(@binary_buffer) end |
#urlsafe_to_s ⇒ Object
Returns the binary content as a url safe base64 string (where + and / are replaced by - and _)
105 106 107 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 105 def urlsafe_to_s Base64.urlsafe_encode64(@binary_buffer) end |