lib/bindata/io.rb in bindata-2.3.5 vs lib/bindata/io.rb in bindata-2.4.0

- old
+ new

@@ -271,15 +271,11 @@ # If the data read is nil an EOFError is raised. # # If the data read is too short an IOError is raised. def readbytes(n) reset_read_bits - - str = read(n) - raise EOFError, "End of file reached" if str.nil? - raise IOError, "data truncated" if str.size < n - str + read(n) end # Reads all remaining bytes from the stream. def read_all_bytes reset_read_bits @@ -311,11 +307,16 @@ #--------------- private def read(n = nil) - read_raw(buffer_limited_n(n)) + str = read_raw(buffer_limited_n(n)) + if n + raise EOFError, "End of file reached" if str.nil? + raise IOError, "data truncated" if str.size < n + end + str end def read_big_endian_bits(nbits) while @rnbits < nbits accumulate_big_endian_bits @@ -327,14 +328,11 @@ val end def accumulate_big_endian_bits - byte = read(1) - raise EOFError, "End of file reached" if byte.nil? - byte = byte.unpack('C').at(0) & 0xff - + byte = read(1).unpack('C').at(0) & 0xff @rval = (@rval << 8) | byte @rnbits += 8 end def read_little_endian_bits(nbits) @@ -348,13 +346,10 @@ val end def accumulate_little_endian_bits - byte = read(1) - raise EOFError, "End of file reached" if byte.nil? - byte = byte.unpack('C').at(0) & 0xff - + byte = read(1).unpack('C').at(0) & 0xff @rval = @rval | (byte << @rnbits) @rnbits += 8 end def mask(nbits)