class Feh::Bin::ArrayIStream

Single-pass input array stream that reads little-endian integers.

Attributes

bytes_read[R]

@return [Integer] the number of bytes read so far

Public Class Methods

new(buffer) click to toggle source

Initializes the stream. @param buffer [Array<Integer>] an array of byte values between 0 and 255 @raise [ArgumentError] if arr is not a byte array

# File lib/feh/bin/array_istream.rb, line 20
def initialize(buffer)
  raise ArgumentError, 'Input is not a byte array' unless
    buffer.is_a?(Array) &&
    buffer.all? {|x| x.is_a?(Integer) && x.between?(0, 255)}
  @buf = buffer
  @bytes_read = 0
end

Public Instance Methods

remaining() click to toggle source

Returns the unread bytes of the stream. @return [Array<Integer>] An array of unread bytes.

# File lib/feh/bin/array_istream.rb, line 64
def remaining
  @buf[@bytes_read..-1]
end
u16() click to toggle source

Attempts to read an unsigned 16-bit integer. @return [Integer] the integer read @return [nil] if not enough bytes remaining are present to form an integer

# File lib/feh/bin/array_istream.rb, line 41
def u16
  return nil if @bytes_read > @buf.size - 2
  x = @buf[@bytes_read]
  x |= @buf[@bytes_read + 1] << 8
  @bytes_read += 2
  x
end
u32() click to toggle source

Attempts to read an unsigned 32-bit integer. @return [Integer] the integer read @return [nil] if not enough bytes remaining are present to form an integer

# File lib/feh/bin/array_istream.rb, line 52
def u32
  return nil if @bytes_read > @buf.size - 4
  x = @buf[@bytes_read]
  x |= @buf[@bytes_read + 1] << 8
  x |= @buf[@bytes_read + 2] << 16
  x |= @buf[@bytes_read + 3] << 24
  @bytes_read += 4
  x
end
u8() click to toggle source

Attempts to read an unsigned 8-bit integer. @return [Integer] the integer read @return [nil] if not enough bytes remaining are present to form an integer

# File lib/feh/bin/array_istream.rb, line 31
def u8
  return nil if @bytes_read > @buf.size - 1
  x = @buf[@bytes_read]
  @bytes_read += 1
  x
end