Kouhei Sutou
null+****@clear*****
Wed Jul 5 15:23:43 JST 2017
Kouhei Sutou 2017-07-05 15:23:43 +0900 (Wed, 05 Jul 2017) New Revision: f4e89e4a98d519cd9057aee52f40cf21a95f2513 https://github.com/ranguba/chupa-text/commit/f4e89e4a98d519cd9057aee52f40cf21a95f2513 Message: Use IO.copy_stream IO.copy_stream doesn't work with Gem::Package::TarReader::Entry because Gem::Package::TarReader::Entry#read and #readpartial don't support output buffer. Modified files: lib/chupa-text/decomposers/tar.rb lib/chupa-text/virtual-content.rb Modified: lib/chupa-text/decomposers/tar.rb (+24 -1) =================================================================== --- lib/chupa-text/decomposers/tar.rb 2017-07-05 14:31:46 +0900 (6bca425) +++ lib/chupa-text/decomposers/tar.rb 2017-07-05 15:23:43 +0900 (2794d5b) @@ -1,4 +1,4 @@ -# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2013-2017 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -31,12 +31,35 @@ module ChupaText Gem::Package::TarReader.new(StringIO.new(data.body)) do |reader| reader.each do |entry| next unless entry.file? + entry.extend(CopyStreamable) extracted = VirtualFileData.new(entry.full_name, entry) extracted.source = data yield(extracted) end end end + + # TODO: Supporting output buffer in #read and #readpartial + # should be done in RubyGems' tar implementation. + module CopyStreamable + def readpartial(max_length, buffer=nil) + data = super(max_length) + if data.nil? + if max_length.zero? + return "" + else + raise EOFError + end + end + + if buffer.nil? + data + else + buffer.replace(data) + buffer + end + end + end end end end Modified: lib/chupa-text/virtual-content.rb (+4 -27) =================================================================== --- lib/chupa-text/virtual-content.rb 2017-07-05 14:31:46 +0900 (0284c98) +++ lib/chupa-text/virtual-content.rb 2017-07-05 15:23:43 +0900 (3229a71) @@ -26,30 +26,14 @@ module ChupaText def initialize(input, original_path=nil) @file = nil @base_name = compute_base_name(original_path) - chunk = input.read(BUFFER_SIZE) || "" - if chunk.bytesize != BUFFER_SIZE - @path = nil - @body = chunk - @size =****@body***** - else - @body = nil - @size = chunk.bytesize - setup_file do |file| - file.write(chunk) - while (chunk = input.read(BUFFER_SIZE)) - @size += chunk.bytesize - file.write(chunk) - end - end + @body = nil + setup_file do |file| + @size = IO.copy_stream(input, file) end end def open(&block) - if @body - yield(StringIO.new(@body)) - else - File.open(path, "rb", &block) - end + File.open(path, "rb", &block) end def body @@ -57,9 +41,6 @@ module ChupaText end def path - ensure_setup_file do |file| - file.write(@body) - end @path end @@ -77,10 +58,6 @@ module ChupaText end end - def ensure_setup_file(&block) - setup_file(&block) unless @file - end - def setup_file @file = Tempfile.new(@base_name) @path =****@file***** -------------- next part -------------- HTML����������������������������... 下载