class Feh::Bin::ArrayOStream

Single-pass output array stream that writes little-endian integers.

Public Class Methods

new() click to toggle source

Initializes the stream.

# File lib/feh/bin/array_ostream.rb, line 19
def initialize
  @buf = []
end

Public Instance Methods

buf() click to toggle source

@return [Array<Integer>] the stream content

# File lib/feh/bin/array_ostream.rb, line 9
def buf
  @buf.dup
end
bytes_written() click to toggle source

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

# File lib/feh/bin/array_ostream.rb, line 14
def bytes_written
  @buf.size
end
u16(x) click to toggle source

Writes an unsigned 16-bit integer. @param x [Integer] integer value to write @return [ArrayOStream] self

# File lib/feh/bin/array_ostream.rb, line 33
def u16(x)
  write [x & 0xFF, (x >> 8) & 0xFF]
end
u32(x) click to toggle source

Writes an unsigned 32-bit integer. @param x [Integer] integer value to write @return [ArrayOStream] self

# File lib/feh/bin/array_ostream.rb, line 40
def u32(x)
  write [x & 0xFF, (x >> 8) & 0xFF, (x >> 16) & 0xFF, (x >> 24) & 0xFF]
end
u8(x) click to toggle source

Writes an unsigned 8-bit integer. @param x [Integer] integer value to write @return [ArrayOStream] self

# File lib/feh/bin/array_ostream.rb, line 26
def u8(x)
  write [x & 0xFF]
end
write(arr) click to toggle source

Writes an array of bytes. @param arr [Array<Integer>] an array of byte values @return [ArrayOStream] self @raise [ArgumentError] if arr is not a byte array

# File lib/feh/bin/array_ostream.rb, line 48
def write(arr)
  raise ArgumentError, 'Input is not a byte array' unless
    arr.is_a?(Array) &&
    arr.all? {|x| x.is_a?(Integer) && x.between?(0, 255)}
  write2(arr)
end

Private Instance Methods

write2(arr) click to toggle source
# File lib/feh/bin/array_ostream.rb, line 56
def write2(arr)
  @buf += arr
  self
end