Class: Feh::Bin::ArrayOStream
- Inherits:
-
Object
- Object
- Feh::Bin::ArrayOStream
- Defined in:
- lib/feh/bin/array_ostream.rb
Overview
Single-pass output array stream that writes little-endian integers.
Instance Method Summary collapse
-
#buf ⇒ Array<Integer>
The stream content.
-
#bytes_written ⇒ Integer
The number of bytes written so far.
-
#initialize ⇒ ArrayOStream
constructor
Initializes the stream.
-
#u16(x) ⇒ ArrayOStream
Writes an unsigned 16-bit integer.
-
#u32(x) ⇒ ArrayOStream
Writes an unsigned 32-bit integer.
-
#u8(x) ⇒ ArrayOStream
Writes an unsigned 8-bit integer.
-
#write(arr) ⇒ ArrayOStream
Writes an array of bytes.
Constructor Details
#initialize ⇒ ArrayOStream
Initializes the stream.
19 20 21 |
# File 'lib/feh/bin/array_ostream.rb', line 19 def initialize @buf = [] end |
Instance Method Details
#buf ⇒ Array<Integer>
Returns the stream content.
9 10 11 |
# File 'lib/feh/bin/array_ostream.rb', line 9 def buf @buf.dup end |
#bytes_written ⇒ Integer
Returns 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.
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.
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.
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.
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 |