Merged patch from oss-ruby at technorama.net

This commit is contained in:
thomas 2005-09-03 07:57:50 +00:00
parent b2060aa257
commit 7cf8aabc8f
3 changed files with 40 additions and 11 deletions

View File

@ -2,6 +2,8 @@ module IOExtras #:nodoc:
CHUNK_SIZE = 32768 CHUNK_SIZE = 32768
RANGE_ALL = 0..-1
def self.copy_stream(ostream, istream) def self.copy_stream(ostream, istream)
s = '' s = ''
ostream.write(istream.read(CHUNK_SIZE, s)) until istream.eof? ostream.write(istream.read(CHUNK_SIZE, s)) until istream.eof?
@ -30,6 +32,32 @@ module IOExtras #:nodoc:
attr_accessor :lineno 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 = $/) def readlines(aSepString = $/)
retVal = [] retVal = []
each_line(aSepString) { |line| retVal << line } each_line(aSepString) { |line| retVal << line }

View File

@ -120,8 +120,8 @@ module Zip
end end
# Modeled after IO.read # Modeled after IO.read
def read(numberOfBytes = nil) def sysread(numberOfBytes = nil, buf = nil)
@decompressor.read(numberOfBytes) @decompressor.sysread(numberOfBytes, buf)
end end
protected protected
@ -170,7 +170,8 @@ module Zip
@hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST @hasReturnedEmptyString = ! EMPTY_FILE_RETURNS_EMPTY_STRING_FIRST
end end
def read(numberOfBytes = nil) # BUG: do something with buf
def sysread(numberOfBytes = nil, buf = nil)
readEverything = (numberOfBytes == nil) readEverything = (numberOfBytes == nil)
while (readEverything || @outputBuffer.length < numberOfBytes) while (readEverything || @outputBuffer.length < numberOfBytes)
break if internal_input_finished? break if internal_input_finished?
@ -221,7 +222,7 @@ module Zip
end end
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ? # TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
def read(numberOfBytes = nil) def sysread(numberOfBytes = nil, buf = nil)
if input_finished? if input_finished?
hasReturnedEmptyStringVal=@hasReturnedEmptyString hasReturnedEmptyStringVal=@hasReturnedEmptyString
@hasReturnedEmptyString=true @hasReturnedEmptyString=true
@ -233,11 +234,11 @@ module Zip
numberOfBytes = @charsToRead-@readSoFar numberOfBytes = @charsToRead-@readSoFar
end end
@readSoFar += numberOfBytes @readSoFar += numberOfBytes
@inputStream.read(numberOfBytes) @inputStream.read(numberOfBytes, buf)
end end
def produce_input def produce_input
read(Decompressor::CHUNK_SIZE) sysread(Decompressor::CHUNK_SIZE)
end end
def input_finished? def input_finished?
@ -247,7 +248,7 @@ module Zip
class NullDecompressor #:nodoc:all class NullDecompressor #:nodoc:all
include Singleton include Singleton
def read(numberOfBytes = nil) def sysread(numberOfBytes = nil, buf = nil)
nil nil
end end

View File

@ -276,12 +276,12 @@ module DecompressorTests
end end
def test_readEverything def test_readEverything
assert_equal(@refText, @decompressor.read) assert_equal(@refText, @decompressor.sysread)
end end
def test_readInChunks def test_readInChunks
chunkSize = 5 chunkSize = 5
while (decompressedChunk = @decompressor.read(chunkSize)) while (decompressedChunk = @decompressor.sysread(chunkSize))
assert_equal(@refText.slice!(0, chunkSize), decompressedChunk) assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
end end
assert_equal(0, @refText.size) assert_equal(0, @refText.size)
@ -293,7 +293,7 @@ module DecompressorTests
assert(@refLines.length > 40) assert(@refLines.length > 40)
assert_equal(@refText[0...100], @decompressor.read(100)) assert_equal(@refText[0...100], @decompressor.sysread(100))
assert(! @decompressor.input_finished?) assert(! @decompressor.input_finished?)
buf = @decompressor.produce_input buf = @decompressor.produce_input
@ -568,7 +568,7 @@ class DeflaterTest < Test::Unit::TestCase
File.open(fileName, "rb") { File.open(fileName, "rb") {
|file| |file|
inflater = Inflater.new(file) inflater = Inflater.new(file)
txt = inflater.read txt = inflater.sysread
} }
end end