diff --git a/lib/zip/ioextras.rb b/lib/zip/ioextras.rb index 5e26f9e..dc2475a 100755 --- a/lib/zip/ioextras.rb +++ b/lib/zip/ioextras.rb @@ -2,6 +2,8 @@ module IOExtras #:nodoc: CHUNK_SIZE = 32768 + RANGE_ALL = 0..-1 + def self.copy_stream(ostream, istream) s = '' ostream.write(istream.read(CHUNK_SIZE, s)) until istream.eof? @@ -30,6 +32,32 @@ module IOExtras #:nodoc: attr_accessor :lineno + def read(numberOfBytes = nil, buf = nil) + buf = "" unless (buf) + + if numberOfBytes + if numberOfBytes <= @outputBuffer.length + buf[RANGE_ALL] = @outputBuffer.slice!(0, numberOfBytes) + else + if @outputBuffer.length > 0 + buf[RANGE_ALL] = @outputBuffer + sysread(numberOfBytes - @outputBuffer.length, buf) + @outputBuffer.slice!(RANGE_ALL) + else + buf[RANGE_ALL] = sysread(nil, buf) + end + end + else + if @outputBuffer.length > 0 + buf[RANGE_ALL] = @outputBuffer + sysread(nil, buf) + @outputBuffer.slice!(RANGE_ALL) + else + buf[RANGE_ALL] = sysread(nil, buf) + end + end + + buf + end + def readlines(aSepString = $/) retVal = [] each_line(aSepString) { |line| retVal << line } diff --git a/lib/zip/zip.rb b/lib/zip/zip.rb index 9788861..4d674f9 100755 --- a/lib/zip/zip.rb +++ b/lib/zip/zip.rb @@ -120,8 +120,8 @@ module Zip end # Modeled after IO.read - def read(numberOfBytes = nil) - @decompressor.read(numberOfBytes) + def sysread(numberOfBytes = nil, buf = nil) + @decompressor.sysread(numberOfBytes, buf) end protected @@ -170,7 +170,8 @@ module Zip @hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST end - def read(numberOfBytes = nil) + # BUG: do something with buf + def sysread(numberOfBytes = nil, buf = nil) readEverything = (numberOfBytes == nil) while (readEverything || @outputBuffer.length < numberOfBytes) break if internal_input_finished? @@ -221,7 +222,7 @@ module Zip end # TODO: Specialize to handle different behaviour in ruby > 1.7.0 ? - def read(numberOfBytes = nil) + def sysread(numberOfBytes = nil, buf = nil) if input_finished? hasReturnedEmptyStringVal=@hasReturnedEmptyString @hasReturnedEmptyString=true @@ -233,11 +234,11 @@ module Zip numberOfBytes = @charsToRead-@readSoFar end @readSoFar += numberOfBytes - @inputStream.read(numberOfBytes) + @inputStream.read(numberOfBytes, buf) end def produce_input - read(Decompressor::CHUNK_SIZE) + sysread(Decompressor::CHUNK_SIZE) end def input_finished? @@ -247,7 +248,7 @@ module Zip class NullDecompressor #:nodoc:all include Singleton - def read(numberOfBytes = nil) + def sysread(numberOfBytes = nil, buf = nil) nil end diff --git a/test/ziptest.rb b/test/ziptest.rb index 6a45dab..d1684be 100755 --- a/test/ziptest.rb +++ b/test/ziptest.rb @@ -276,12 +276,12 @@ module DecompressorTests end def test_readEverything - assert_equal(@refText, @decompressor.read) + assert_equal(@refText, @decompressor.sysread) end def test_readInChunks chunkSize = 5 - while (decompressedChunk = @decompressor.read(chunkSize)) + while (decompressedChunk = @decompressor.sysread(chunkSize)) assert_equal(@refText.slice!(0, chunkSize), decompressedChunk) end assert_equal(0, @refText.size) @@ -293,7 +293,7 @@ module DecompressorTests assert(@refLines.length > 40) - assert_equal(@refText[0...100], @decompressor.read(100)) + assert_equal(@refText[0...100], @decompressor.sysread(100)) assert(! @decompressor.input_finished?) buf = @decompressor.produce_input @@ -568,7 +568,7 @@ class DeflaterTest < Test::Unit::TestCase File.open(fileName, "rb") { |file| inflater = Inflater.new(file) - txt = inflater.read + txt = inflater.sysread } end