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

- old
+ new

@@ -4,14 +4,11 @@ # A wrapper around an IO object. The wrapper provides a consistent # interface for BinData objects to use when accessing the IO. module IO # Creates a StringIO around +str+. def self.create_string_io(str = "") - if str.respond_to?(:force_encoding) - str = str.dup.force_encoding(Encoding::BINARY) - end - StringIO.new(str) + StringIO.new(str.dup.force_encoding(Encoding::BINARY)) end # Create a new IO Read wrapper around +io+. +io+ must provide #read, # #pos if reading the current stream position and #seek if setting the # current stream position. If +io+ is a string it will be automatically @@ -188,12 +185,12 @@ (1 << nbits) - 1 end # Use #seek and #pos on seekable streams module SeekableStream - # Returns the current offset of the io stream. Offset will be rounded - # up when reading bitfields. + # Returns the current offset of the io stream. The exact value of + # the offset when reading bitfields is not defined. def offset raw_io.pos - @initial_pos end # The number of bytes remaining in the input stream. @@ -223,12 +220,12 @@ end end # Manually keep track of offset for unseekable streams. module UnSeekableStream - # Returns the current offset of the io stream. Offset will be rounded - # up when reading bitfields. + # Returns the current offset of the io stream. The exact value of + # the offset when reading bitfields is not defined. def offset @read_count ||= 0 end # The number of bytes remaining in the input stream. @@ -282,12 +279,10 @@ @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 @@ -307,16 +302,10 @@ 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 @@ -400,10 +389,9 @@ 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