Module: Thrift::Bytes
- Defined in:
- lib/thrift/bytes.rb
Overview
A collection of utilities for working with bytes and byte buffers.
Class Method Summary collapse
-
.convert_to_string(utf8_buffer) ⇒ Object
Converts the given UTF-8 byte buffer into a String.
-
.convert_to_utf8_byte_buffer(string) ⇒ Object
Converts the given String to a UTF-8 byte buffer.
-
.empty_byte_buffer(size = nil) ⇒ Object
Creates and empty byte buffer (String with BINARY encoding).
-
.force_binary_encoding(buffer) ⇒ Object
Forces the encoding of the buffer to BINARY.
-
.get_string_byte(string, index) ⇒ Object
Gets the byte value of a given position in a String.
-
.set_string_byte(string, index, byte) ⇒ Object
Sets the byte value given to a given index in a String.
Class Method Details
.convert_to_string(utf8_buffer) ⇒ Object
Converts the given UTF-8 byte buffer into a String
utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
Returns a new String with UTF-8 encoding,
96 97 98 99 |
# File 'lib/thrift/bytes.rb', line 96 def self.convert_to_string(utf8_buffer) # duplicate the buffer, force encoding to UTF-8 utf8_buffer.dup.force_encoding(Encoding::UTF_8) end |
.convert_to_utf8_byte_buffer(string) ⇒ Object
Converts the given String to a UTF-8 byte buffer.
string - The String to convert.
Returns a new String with BINARY encoding, containing the UTF-8 bytes of the original string.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/thrift/bytes.rb', line 78 def self.convert_to_utf8_byte_buffer(string) string = string.to_s if string.is_a?(Symbol) if string.encoding != Encoding::UTF_8 # transcode to UTF-8 string = string.encode(Encoding::UTF_8) else # encoding is already UTF-8, but a duplicate is needed string = string.dup end string.force_encoding(Encoding::BINARY) end |
.empty_byte_buffer(size = nil) ⇒ Object
Creates and empty byte buffer (String with BINARY encoding)
size - The Integer size of the buffer (default: nil) to create
Returns a String with BINARY encoding, filled with null characters if size is greater than zero
31 32 33 34 35 36 37 |
# File 'lib/thrift/bytes.rb', line 31 def self.empty_byte_buffer(size = nil) if (size && size > 0) "\0".force_encoding(Encoding::BINARY) * size else ''.force_encoding(Encoding::BINARY) end end |
.force_binary_encoding(buffer) ⇒ Object
Forces the encoding of the buffer to BINARY. If the buffer passed is frozen, then it will be duplicated.
buffer - The String to force the encoding of.
Returns the String passed with an encoding of BINARY; returned String may be a duplicate.
46 47 48 49 |
# File 'lib/thrift/bytes.rb', line 46 def self.force_binary_encoding(buffer) buffer = buffer.dup if buffer.frozen? buffer.force_encoding(Encoding::BINARY) end |
.get_string_byte(string, index) ⇒ Object
Gets the byte value of a given position in a String.
string - The String to retrive the byte value from. index - The Integer location of the byte value to retrieve.
Returns an Integer value between 0 and 255.
57 58 59 |
# File 'lib/thrift/bytes.rb', line 57 def self.get_string_byte(string, index) string.getbyte(index) end |
.set_string_byte(string, index, byte) ⇒ Object
Sets the byte value given to a given index in a String.
string - The String to set the byte value in. index - The Integer location to set the byte value at. byte - The Integer value (0 to 255) to set in the string.
Returns an Integer value of the byte value to set.
68 69 70 |
# File 'lib/thrift/bytes.rb', line 68 def self.set_string_byte(string, index, byte) string.setbyte(index, byte) end |