lib/bindata/io.rb in bindata-1.8.2 vs lib/bindata/io.rb in bindata-1.8.3

- old
+ new

@@ -188,12 +188,12 @@ (1 << nbits) - 1 end # Use #seek and #pos on seekable streams module SeekableStream - # Returns the current offset of the io stream. The exact value of - # the offset when reading bitfields is not defined. + # Returns the current offset of the io stream. Offset will be rounded + # up when reading bitfields. def offset raw_io.pos - @initial_pos end # The number of bytes remaining in the input stream. @@ -223,12 +223,12 @@ end end # Manually keep track of offset for unseekable streams. module UnSeekableStream - # Returns the current offset of the io stream. The exact value of - # the offset when reading bitfields is not defined. + # Returns the current offset of the io stream. Offset will be rounded + # up when reading bitfields. def offset @read_count ||= 0 end # The number of bytes remaining in the input stream. @@ -282,10 +282,12 @@ @wnbits = 0 @wval = 0 @wendian = nil + @write_count = 0 + @bytes_remaining = nil end # Sets a buffer of +n+ bytes on the io stream. Any writes inside the # +block+ will be contained within this buffer. If less than +n+ bytes @@ -305,10 +307,16 @@ ensure @bytes_remaining = prev end end + # Returns the current offset of the io stream. Offset will be rounded + # up when writing bitfields. + def offset + @write_count + (@wnbits > 0 ? 1 : 0) + end + # Writes the given string of bytes to the io stream. def writebytes(str) flushbits write_raw(str) end @@ -392,9 +400,10 @@ data = data[0, @bytes_remaining] end @bytes_remaining -= data.size end + @write_count += data.size @raw_io.write(data) end def mask(nbits) (1 << nbits) - 1