Class: HBase::ByteArray
- Inherits:
-
Object
- Object
- HBase::ByteArray
- Includes:
- Enumerable
- Defined in:
- lib/hbase-jruby/byte_array.rb
Overview
@return [byte The underlying native Java byte array
Instance Attribute Summary collapse
-
#java ⇒ Object
(also: #to_java, #to_java_bytes)
readonly
Returns the value of attribute java.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concats two byte arrays.
-
#<<(other) ⇒ ByteArray
Appends the byte array of the given object on to the end.
-
#<=>(other) ⇒ Object
Compares two ByteArray objects.
-
#[](*index) ⇒ ByteArray
Slices the byte array.
- #decode(type) ⇒ Object
- #each ⇒ Object
-
#eql?(other) ⇒ Boolean
(also: #==)
Checks if the two byte arrays are the same.
-
#hash ⇒ Fixnum
Returns a hash number for the byte array.
-
#initialize(*values) ⇒ ByteArray
constructor
Initializes ByteArray instance with the given objects, each converted to its byte array representation.
-
#length ⇒ Fixnum
Returns the length of the byte array.
-
#shift(type, length = nil) ⇒ Object
Returns the first element decoded as the given type and removes the portion from the byte array.
-
#stopkey_bytes_for_prefix ⇒ byte[]
Returns the first byte array whose prefix doesn’t match this byte array.
-
#unshift(*args) ⇒ ByteArray
Prepends the byte array of the given object to the front.
Constructor Details
#initialize(*values) ⇒ ByteArray
Initializes ByteArray instance with the given objects, each converted to its byte array representation
22 23 24 25 26 |
# File 'lib/hbase-jruby/byte_array.rb', line 22 def initialize *values @java = values.inject(Util::JAVA_BYTE_ARRAY_EMPTY) { |sum, value| Bytes.add sum, Util.to_bytes(value) } end |
Instance Attribute Details
#java ⇒ Object (readonly) Also known as: to_java, to_java_bytes
Returns the value of attribute java.
13 14 15 |
# File 'lib/hbase-jruby/byte_array.rb', line 13 def java @java end |
Instance Method Details
#+(other) ⇒ Object
Concats two byte arrays
51 52 53 |
# File 'lib/hbase-jruby/byte_array.rb', line 51 def + other ByteArray.new(@java, other) end |
#<<(other) ⇒ ByteArray
Appends the byte array of the given object on to the end
58 59 60 61 |
# File 'lib/hbase-jruby/byte_array.rb', line 58 def << other @java = Bytes.add @java, Util.to_bytes(other) self end |
#<=>(other) ⇒ Object
Compares two ByteArray objects
45 46 47 |
# File 'lib/hbase-jruby/byte_array.rb', line 45 def <=> other Bytes.compareTo(@java, other.java) end |
#[](*index) ⇒ ByteArray
Slices the byte array
80 81 82 83 84 85 86 |
# File 'lib/hbase-jruby/byte_array.rb', line 80 def [] *index if index.length == 1 && index.first.is_a?(Fixnum) @java.to_a[*index] else ByteArray.new(@java.to_a[*index].to_java(Java::byte)) end end |
#decode(type) ⇒ Object
90 91 92 |
# File 'lib/hbase-jruby/byte_array.rb', line 90 def decode type Util.from_bytes type, @java end |
#each ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/hbase-jruby/byte_array.rb', line 28 def each if block_given? @java.to_a.each { |byte| yield byte } else self end end |
#eql?(other) ⇒ Boolean Also known as: ==
Checks if the two byte arrays are the same
38 39 40 |
# File 'lib/hbase-jruby/byte_array.rb', line 38 def eql? other Arrays.equals(@java, other.java) end |
#hash ⇒ Fixnum
Returns a hash number for the byte array
142 143 144 |
# File 'lib/hbase-jruby/byte_array.rb', line 142 def hash Arrays.java_send(:hashCode, [Util::JAVA_BYTE_ARRAY_CLASS], @java) end |
#length ⇒ Fixnum
Returns the length of the byte array
73 74 75 |
# File 'lib/hbase-jruby/byte_array.rb', line 73 def length @java.length end |
#shift(type, length = nil) ⇒ Object
Returns the first element decoded as the given type and removes the portion from the byte array. For types of variable lengths, such as :string and :bigdecimal, byte size must be given.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/hbase-jruby/byte_array.rb', line 99 def shift type, length = nil length = case type when :fixnum, :long, :float, :double 8 when :int 4 when :short 2 when :boolean, :bool, :byte 1 else length end raise ArgumentError.new("Byte length must be specified for type: #{type}") unless length raise ArgumentError.new("Not enought bytes for #{type}") if length > @java.length arr = @java.to_a val = arr[0, length].to_java(Java::byte) @java = arr[length..-1].to_java(Java::byte) Util.from_bytes type, val end |
#stopkey_bytes_for_prefix ⇒ byte[]
Returns the first byte array whose prefix doesn’t match this byte array
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hbase-jruby/byte_array.rb', line 125 def stopkey_bytes_for_prefix arr = @java.to_a csr = arr.length - 1 arr[csr] += 1 while csr >= 0 && arr[csr] > 127 csr -= 1 arr[csr] += 1 end if csr < 0 nil else arr[0..csr].to_java(Java::byte) end end |