module Thrift::Bytes
A collection of utilities for working with bytes and byte buffers.
Public Class Methods
Source
# File lib/thrift/bytes.rb 96 def self.convert_to_string(utf8_buffer) 97 # duplicate the buffer, force encoding to UTF-8 98 utf8_buffer.dup.force_encoding(Encoding::UTF_8) 99 end
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,
Source
# File lib/thrift/bytes.rb 80 def self.convert_to_utf8_byte_buffer(string) 81 if string.encoding != Encoding::UTF_8 82 # transcode to UTF-8 83 string = string.encode(Encoding::UTF_8) 84 else 85 # encoding is already UTF-8, but a duplicate is needed 86 string = string.dup 87 end 88 string.force_encoding(Encoding::BINARY) 89 end
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.
Source
# File lib/thrift/bytes.rb 31 def self.empty_byte_buffer(size = nil) 32 if size && size > 0 33 "\0".b * size 34 else 35 "".b 36 end 37 end
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
Source
# File lib/thrift/bytes.rb 46 def self.force_binary_encoding(buffer) 47 return buffer if buffer.encoding == Encoding::BINARY 48 49 buffer = buffer.dup if buffer.frozen? 50 buffer.force_encoding(Encoding::BINARY) 51 end
Forces the encoding of the buffer to BINARY. If the buffer passed is frozen and not already BINARY, 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.
Source
# File lib/thrift/bytes.rb 59 def self.get_string_byte(string, index) 60 string.getbyte(index) 61 end
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.
Source
# File lib/thrift/bytes.rb 70 def self.set_string_byte(string, index, byte) 71 string.setbyte(index, byte) 72 end
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.