Sha256: 6b176b7ae1635d2c0817ac221c47b591d5b01eb113630d3081dff57af3f6c1d5

Contents?: true

Size: 1.11 KB

Versions: 19

Compression:

Stored size: 1.11 KB

Contents

# Simple class to allow for streamed (i.e. without pos support)
# IO to use the BinaryBlocker utilities, but buffering until flushed
# previously read information.
class BufferedIO
  BLOCK_SIZE = 512
  # Rdoc
  def initialize(io)
    super
    @io = io
    @buffer = ''
    @pos = 0
    @iobase = @io.pos
  end
  
  def flush
    @iobase += @pos
    @buffer = ''
    @pos = 0
    @io.flush
  end
  
  def read(size, buffer = nil)
    if (@buffer.size - @pos) < size
      @buffer += @io.read(BLOCK_SIZE)
    end
    result = @buffer[@pos,size]
    @pos += result.size
    buffer.replace(result) if buffer
    result
  end
  
  def pos
    @iobase + @pos
  end
  
  def pos=(newpos)
    seek(newpos) 
  end
  
  def seek(amount, whence=IO::SEEK_SET)
    case whence
    when IO::SEEK_CUR
      raise "rewind before buffer start" if (amount < @pos)
      @pos -= amount
      @iobase + @pos
      
    when IO::SEEK_END
      raise "Sorry this operation is not supported"
      
    when IO::SEEK_SET
      raise "rewind before buffer start" if (amount < @iobase)
      @pos = amount - @iobase
      @iobase + @pos      
    end
  end
end


Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
binaryparse-1.1.2 lib/buffered_io.rb
binaryparse-1.1.1 lib/buffered_io.rb
binaryparse-1.1.0 lib/buffered_io.rb
binaryparse-1.0.1 lib/buffered_io.rb
binaryparse-0.1.4 lib/buffered_io.rb
binaryparse-0.1.5 lib/buffered_io.rb
binaryparse-0.1.6 lib/buffered_io.rb
binaryparse-0.2.0 lib/buffered_io.rb
binaryparse-0.2.2 lib/buffered_io.rb
binaryparse-0.1.0 lib/buffered_io.rb
binaryparse-0.2.5 lib/buffered_io.rb
binaryparse-0.2.6 lib/buffered_io.rb
binaryparse-0.2.7 lib/buffered_io.rb
binaryparse-0.3.3 lib/buffered_io.rb
binaryparse-1.0.0 lib/buffered_io.rb
binaryparse-0.2.3 lib/buffered_io.rb
binaryparse-0.1.2 lib/buffered_io.rb
binaryparse-0.1.3 lib/buffered_io.rb
binaryparse-0.2.4 lib/buffered_io.rb