Class: Distillery::Archiver::LibArchive::InputStream
- Inherits:
-
InputStream
- Object
- InputStream
- Distillery::Archiver::LibArchive::InputStream
- Defined in:
- lib/distillery/archiver/libarchive.rb
Instance Method Summary collapse
-
#initialize(ar) ⇒ InputStream
constructor
A new instance of InputStream.
- #read(length = nil) ⇒ Object
Constructor Details
#initialize(ar) ⇒ InputStream
Returns a new instance of InputStream.
26 27 28 29 |
# File 'lib/distillery/archiver/libarchive.rb', line 26 def initialize(ar) @read_block = ar.to_enum(:read_data, 16*1024) @buffer = StringIO.new end |
Instance Method Details
#read(length = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/distillery/archiver/libarchive.rb', line 31 def read(length=nil) return '' if length&.zero? # Zero length request return nil if @buffer.nil? # End of stream # Read data data = @buffer.read(length) || "" while data.size < length do # We are short on data, that means buffer has been exhausted # request new data block from the archive block = @read_block.next # Break if we already read all the archive data if block.nil? || block.empty? @buffer = nil break end # Refill buffer from block @buffer.string = block # Continue reading data.concat(@buffer.read(length - data.size)) end data.empty? ? nil : data end |