Class: HBase::ByteArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hbase-jruby/byte_array.rb

Overview

Boxed class for Java byte arrays

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ ByteArray

Initializes ByteArray instance with the given objects, each converted to its byte array representation

Parameters:

  • values (*Object)


30
31
32
33
34
35
36
37
38
# File 'lib/hbase-jruby/byte_array.rb', line 30

def initialize *values
  HBase.import_java_classes!
  if defined?(Bytes) && defined?(Arrays)
    ByteArray.class_eval do
      alias initialize initialize_
    end
  end
  initialize_(*values)
end

Instance Attribute Details

#javabyte[] (readonly) Also known as: to_java, to_java_bytes

Returns The underlying native Java byte array.

Returns:

  • (byte[])

    The underlying native Java byte array



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/hbase-jruby/byte_array.rb', line 13

class ByteArray
  attr_reader :java
  alias to_java java
  alias to_java_bytes java

  include Enumerable

  # Shortcut method to HBase::ByteArray.new
  # @param [*Object] values
  # @return [HBase::ByteArray]
  def self.[] *values
    ByteArray.new(*values)
  end

  # Initializes ByteArray instance with the given objects,
  # each converted to its byte array representation
  # @param [*Object] values
  def initialize *values
    HBase.import_java_classes!
    if defined?(Bytes) && defined?(Arrays)
      ByteArray.class_eval do
        alias initialize initialize_
      end
    end
    initialize_(*values)
  end

  # Returns the String representation of the underlying byte array
  # @return [String]
  def to_s
    @java.to_s
  end

  def each
    return enum_for(:each) unless block_given?
    @java.to_a.each { |byte| yield byte }
  end

  # Checks if the two byte arrays are the same
  # @param [HBase::ByteArray] other
  def eql? other
    other = other_as_byte_array other
    Arrays.equals(@java, other.java)
  end
  alias == eql?

  # Compares two ByteArray objects
  # @param [HBase::ByteArray] other
  def <=> other
    other = other_as_byte_array other
    Bytes.compareTo(@java, other.java)
  end

  # Concats two byte arrays
  # @param [Object] other
  def + other
    ByteArray.new(@java, other)
  end

  # Appends the byte array of the given object on to the end
  # @param [Object] other
  # @return [ByteArray] Modified self
  def << other
    @java = Bytes.add @java, Util.to_bytes(other)
    self
  end

  # Prepends the byte array of the given object to the front
  # @param [*Object] args Objects to prepend
  # @return [ByteArray] Modified self
  def unshift *args
    @java = (ByteArray.new(*args) + self).java
    self
  end

  # Returns the length of the byte array
  # @return [Fixnum]
  def length
    @java.length
  end

  # Slices the byte array
  # @param [Object] index
  # @return [ByteArray]
  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

  # @param [Symbol] type
  # @return [Object]
  def decode type
    Util.from_bytes type, @java
  end
  alias as decode

  # 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.
  # @param [Symbol] type
  # @return [Object]
  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 enough 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

  # Returns the first byte array whose prefix doesn't match this byte array
  # @return [byte[]]
  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

  # Returns a hash number for the byte array
  # @return [Fixnum]
  def hash
    Arrays.java_send(:hashCode, [Util::JAVA_BYTE_ARRAY_CLASS], @java)
  end

  def inspect
    "HBase::ByteArray<#{@java.to_a.join ', '}>"
  end

private
  def initialize_ *values
    @java = values.inject(Util::JAVA_BYTE_ARRAY_EMPTY) { |sum, value|
      Bytes.add sum, Util.to_bytes(value)
    }
  end

  def other_as_byte_array other
    case other
    when ByteArray
      other
    else
      ByteArray[other]
    end
  end
end

Class Method Details

.[](*values) ⇒ HBase::ByteArray

Shortcut method to HBase::ByteArray.new

Parameters:

  • values (*Object)

Returns:



23
24
25
# File 'lib/hbase-jruby/byte_array.rb', line 23

def self.[] *values
  ByteArray.new(*values)
end

Instance Method Details

#+(other) ⇒ Object

Concats two byte arrays

Parameters:

  • other (Object)


68
69
70
# File 'lib/hbase-jruby/byte_array.rb', line 68

def + other
  ByteArray.new(@java, other)
end

#<<(other) ⇒ ByteArray

Appends the byte array of the given object on to the end

Parameters:

  • other (Object)

Returns:



75
76
77
78
# File 'lib/hbase-jruby/byte_array.rb', line 75

def << other
  @java = Bytes.add @java, Util.to_bytes(other)
  self
end

#<=>(other) ⇒ Object

Compares two ByteArray objects

Parameters:



61
62
63
64
# File 'lib/hbase-jruby/byte_array.rb', line 61

def <=> other
  other = other_as_byte_array other
  Bytes.compareTo(@java, other.java)
end

#[](*index) ⇒ ByteArray

Slices the byte array

Parameters:

  • index (Object)

Returns:



97
98
99
100
101
102
103
# File 'lib/hbase-jruby/byte_array.rb', line 97

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 Also known as: as

Parameters:

  • type (Symbol)

Returns:

  • (Object)


107
108
109
# File 'lib/hbase-jruby/byte_array.rb', line 107

def decode type
  Util.from_bytes type, @java
end

#eachObject



46
47
48
49
# File 'lib/hbase-jruby/byte_array.rb', line 46

def each
  return enum_for(:each) unless block_given?
  @java.to_a.each { |byte| yield byte }
end

#eql?(other) ⇒ Boolean Also known as: ==

Checks if the two byte arrays are the same

Parameters:

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/hbase-jruby/byte_array.rb', line 53

def eql? other
  other = other_as_byte_array other
  Arrays.equals(@java, other.java)
end

#hashFixnum

Returns a hash number for the byte array

Returns:

  • (Fixnum)


160
161
162
# File 'lib/hbase-jruby/byte_array.rb', line 160

def hash
  Arrays.java_send(:hashCode, [Util::JAVA_BYTE_ARRAY_CLASS], @java)
end

#inspectObject



164
165
166
# File 'lib/hbase-jruby/byte_array.rb', line 164

def inspect
  "HBase::ByteArray<#{@java.to_a.join ', '}>"
end

#lengthFixnum

Returns the length of the byte array

Returns:

  • (Fixnum)


90
91
92
# File 'lib/hbase-jruby/byte_array.rb', line 90

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.

Parameters:

  • type (Symbol)

Returns:

  • (Object)

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hbase-jruby/byte_array.rb', line 117

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 enough 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_prefixbyte[]

Returns the first byte array whose prefix doesn't match this byte array

Returns:

  • (byte[])


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hbase-jruby/byte_array.rb', line 143

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

#to_sString

Returns the String representation of the underlying byte array

Returns:

  • (String)


42
43
44
# File 'lib/hbase-jruby/byte_array.rb', line 42

def to_s
  @java.to_s
end

#unshift(*args) ⇒ ByteArray

Prepends the byte array of the given object to the front

Parameters:

  • args (*Object)

    Objects to prepend

Returns:



83
84
85
86
# File 'lib/hbase-jruby/byte_array.rb', line 83

def unshift *args
  @java = (ByteArray.new(*args) + self).java
  self
end