lib/bindata/io.rb in bindata-2.0.0 vs lib/bindata/io.rb in bindata-2.1.0

- old
+ new

@@ -185,12 +185,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. @@ -220,12 +220,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. @@ -279,10 +279,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 @@ -302,10 +304,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 @@ -389,9 +397,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