Class | BufferedIO |
In: |
lib/buffered_io.rb
|
Parent: | Object |
Simple class to allow for streamed (i.e. without pos support) IO to use the BinaryBlocker utilities, but buffering until flushed previously read information.
BLOCK_SIZE | = | 512 |
Rdoc
# File lib/buffered_io.rb, line 7 7: def initialize(io) 8: super 9: @io = io 10: @buffer = '' 11: @pos = 0 12: @iobase = @io.pos 13: end
# File lib/buffered_io.rb, line 15 15: def flush 16: @iobase += @pos 17: @buffer = '' 18: @pos = 0 19: @io.flush 20: end
# File lib/buffered_io.rb, line 22 22: def read(size, buffer = nil) 23: if (@buffer.size - @pos) < size 24: @buffer += @io.read(BLOCK_SIZE) 25: end 26: result = @buffer[@pos,size] 27: @pos += result.size 28: buffer.replace(result) if buffer 29: result 30: end
# File lib/buffered_io.rb, line 40 40: def seek(amount, whence=IO::SEEK_SET) 41: case whence 42: when IO::SEEK_CUR 43: raise "rewind before buffer start" if (amount < @pos) 44: @pos -= amount 45: @iobase + @pos 46: 47: when IO::SEEK_END 48: raise "Sorry this operation is not supported" 49: 50: when IO::SEEK_SET 51: raise "rewind before buffer start" if (amount < @iobase) 52: @pos = amount - @iobase 53: @iobase + @pos 54: end 55: end