BinaryReader
This mixin solely depends on method read(n), which must be defined in the class/module where you mix in this module.
Methods
- byte_order
- byte_order=
- byteorder
- byteorder=
- read_cstring
- read_int16_big
- read_int16_little
- read_int16_native
- read_int32_big
- read_int32_little
- read_int32_native
- read_int8
- read_uint8
- read_word16_big
- read_word16_little
- read_word16_native
- read_word32_big
- read_word32_little
- read_word32_native
- read_word8
- readn
- ru
- ru_swap
Classes and Modules
Module BinaryReader::ByteOrderPublic Instance methods
default is native byte-order
This method is also aliased as
byteorder
[ show source ]
# File lib/facets/more/binaryreader.rb, line 69 def byte_order @byte_order || ByteOrder::Native end
This method is also aliased as
byteorder=
[ show source ]
# File lib/facets/more/binaryreader.rb, line 73 def byte_order=(new_byteorder) @byte_order = new_byteorder end
Alias for byte_order
Alias for byte_order=
[ show source ]
# File lib/facets/more/binaryreader.rb, line 183 def read_cstring str = "" while (c=readn(1)) != "\0" str << c end str end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 119 def read_int16_big # swap bytes if native=little (but we want big) ru_swap(2, 's', ByteOrder::Little) end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 114 def read_int16_little # swap bytes if native=big (but we want little) ru_swap(2, 's', ByteOrder::Big) end
Signed
[ show source ]
# File lib/facets/more/binaryreader.rb, line 110 def read_int16_native ru(2, 's') end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 151 def read_int32_big # swap bytes if native=little (but we want big) ru_swap(4, 'l', ByteOrder::Little) end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 146 def read_int32_little # swap bytes if native=big (but we want little) ru_swap(4, 'l', ByteOrder::Big) end
Signed
[ show source ]
# File lib/facets/more/binaryreader.rb, line 142 def read_int32_native ru(4, 'l') end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 88 def read_int8 ru(1, 'c') end
Alias for read_word8
[ show source ]
# File lib/facets/more/binaryreader.rb, line 104 def read_word16_big ru(2, 'n') end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 100 def read_word16_little ru(2, 'v') end
Unsigned
[ show source ]
# File lib/facets/more/binaryreader.rb, line 96 def read_word16_native ru(2, 'S') end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 136 def read_word32_big ru(4, 'N') end
[ show source ]
# File lib/facets/more/binaryreader.rb, line 132 def read_word32_little ru(4, 'V') end
Unsigned
[ show source ]
# File lib/facets/more/binaryreader.rb, line 128 def read_word32_native ru(4, 'L') end
no byteorder for 8 bit!
This method is also aliased as
read_uint8
[ show source ]
# File lib/facets/more/binaryreader.rb, line 84 def read_word8 ru(1, 'C') end
read exactly n characters, otherwise raise an exception.
[ show source ]
# File lib/facets/more/binaryreader.rb, line 192 def readn(n) str = read(n) raise "couldn't read #{n} characters" if str.nil? or str.size != n str end
Private Instance methods
shortcut method for readn+unpack
[ show source ]
# File lib/facets/more/binaryreader.rb, line 201 def ru(size, template) readn(size).unpack(template).first end
same as method ru, but swap bytes if native byteorder == byteorder
[ show source ]
# File lib/facets/more/binaryreader.rb, line 206 def ru_swap(size, template, byteorder) str = readn(size) str.reverse! if ByteOrder.byteorder == byteorder str.unpack(template).first end