lib/bindata/io.rb in bindata-2.3.3 vs lib/bindata/io.rb in bindata-2.3.4

- old
+ new

@@ -37,31 +37,31 @@ seek_raw(buffer_limited_n(n)) end def buffer_limited_n(n) if @buffer_end_points - if n.nil? or n > 0 + if n.nil? || n > 0 max = @buffer_end_points[1] - offset - n = max if n.nil? or n > max + n = max if n.nil? || n > max else min = @buffer_end_points[0] - offset n = min if n < min end end n end - def with_buffer_common(n, &block) + def with_buffer_common(n) prev = @buffer_end_points if prev avail = prev[1] - offset n = avail if n > avail end @buffer_end_points = [offset, offset + n] begin - block.call(*@buffer_end_points) + yield(*@buffer_end_points) ensure @buffer_end_points = prev end end @@ -85,14 +85,14 @@ bytes_remaining end # All io calls in +block+ are rolled back after this # method completes. - def with_readahead(&block) + def with_readahead mark = @raw_io.pos begin - block.call + yield ensure @raw_io.seek(mark, ::IO::SEEK_SET) end end @@ -131,22 +131,22 @@ raise IOError, "stream is unseekable" end # All io calls in +block+ are rolled back after this # method completes. - def with_readahead(&block) + def with_readahead mark = @offset @read_data = "" @in_readahead = true class << self alias_method :read_raw_without_readahead, :read_raw alias_method :read_raw, :read_raw_with_readahead end begin - block.call + yield ensure @offset = mark @in_readahead = false end end @@ -165,16 +165,16 @@ end def read_raw_with_readahead(n) data = "" - if @read_data.length > 0 and not @in_readahead + unless @read_data.empty? || @in_readahead bytes_to_consume = [n, @read_data.length].min data << @read_data.slice!(0, bytes_to_consume) n -= bytes_to_consume - if @read_data.length == 0 + if @read_data.empty? class << self alias_method :read_raw, :read_raw_without_readahead end end end @@ -245,13 +245,13 @@ @rendian = nil end # Sets a buffer of +n+ bytes on the io stream. Any reading or seeking # calls inside the +block+ will be contained within this buffer. - def with_buffer(n, &block) + def with_buffer(n) with_buffer_common(n) do - block.call + yield read end end # Returns the current offset of the io stream. Offset will be rounded @@ -382,13 +382,13 @@ # 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 # are written inside the block, the remainder will be padded with '\0' # bytes. - def with_buffer(n, &block) - with_buffer_common(n) do |buf_start, buf_end| - block.call + def with_buffer(n) + with_buffer_common(n) do |_buf_start, buf_end| + yield write("\0" * (buf_end - offset)) end end # Returns the current offset of the io stream. Offset will be rounded @@ -433,10 +433,10 @@ if @wnbits > 0 writebits(0, 8 - @wnbits, @wendian) end end - alias_method :flush, :flushbits + alias flush flushbits #--------------- private def write(data)