Class: Feh::Bin::ArrayOStream

Inherits:
Object
  • Object
show all
Defined in:
lib/feh/bin/array_ostream.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeArrayOStream

Initializes the stream.



19
20
21
# File 'lib/feh/bin/array_ostream.rb', line 19

def initialize
  @buf = []
end

Instance Method Details

#bufArray<Integer>

Returns the stream content.

Returns:

  • (Array<Integer>)

    the stream content



9
10
11
# File 'lib/feh/bin/array_ostream.rb', line 9

def buf
  @buf.dup
end

#bytes_writtenInteger

Returns the number of bytes written so far.

Returns:

  • (Integer)

    the number of bytes written so far



14
15
16
# File 'lib/feh/bin/array_ostream.rb', line 14

def bytes_written
  @buf.size
end

#u16(x) ⇒ ArrayOStream

Writes an unsigned 16-bit integer.

Parameters:

  • x (Integer)

    integer value to write

Returns:



33
34
35
# File 'lib/feh/bin/array_ostream.rb', line 33

def u16(x)
  write [x & 0xFF, (x >> 8) & 0xFF]
end

#u32(x) ⇒ ArrayOStream

Writes an unsigned 32-bit integer.

Parameters:

  • x (Integer)

    integer value to write

Returns:



40
41
42
# 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) ⇒ ArrayOStream

Writes an unsigned 8-bit integer.

Parameters:

  • x (Integer)

    integer value to write

Returns:



26
27
28
# File 'lib/feh/bin/array_ostream.rb', line 26

def u8(x)
  write [x & 0xFF]
end

#write(arr) ⇒ ArrayOStream

Writes an array of bytes.

Parameters:

  • arr (Array<Integer>)

    an array of byte values

Returns:

Raises:

  • (ArgumentError)

    if arr is not a byte array



48
49
50
51
52
53
# 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