Method: HBase::ByteArray#shift

Defined in:
lib/hbase-jruby/byte_array.rb

#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