class Xlsxrb::Ooxml::ZipReader::EntryStreamIO
IO-like stream wrapper that decompresses entry chunks on demand without full buffering.
Constants
- DEFAULT_CHUNK_SIZE
Attributes
compressed_size
[R]
untyped
compression_method
[R]
untyped
data_offset
[R]
untyped
Public Class Methods
(untyped io, Integer data_offset, Integer compressed_size, Integer compression_method, ?max_uncompressed_size: Integer?, ?encoding: Encoding?) → void
Source
# File lib/xlsxrb/ooxml/zip_reader.rb, line 41 def initialize(io, data_offset, compressed_size, compression_method, max_uncompressed_size: nil, encoding: nil) @io = io @data_offset = data_offset @compressed_size = compressed_size @compression_method = compression_method @max_uncompressed_size = max_uncompressed_size || ZipReader::MAX_UNCOMPRESSED_SIZE @encoding = encoding @buffer = +"".b # Read offset into @buffer. Consuming by offset instead of slice! avoids moving the # rest of the inflated chunk (often ten times the requested length) on every read. @buffer_pos = 0 @bytes_read_compressed = 0 @total_uncompressed = 0 @eof = false @closed = false @bom_checked = false @inflater = @compression_method == 8 ? Zlib::Inflate.new(-Zlib::MAX_WBITS) : nil end
Public Instance Methods
() → void
Source
# File lib/xlsxrb/ooxml/zip_reader.rb, line 111 def close return if @closed @closed = true begin @inflater&.reset rescue StandardError nil end @inflater&.close @inflater = nil @buffer.clear @buffer_pos = 0 nil end
(?Integer chunk_size) { (String) → void } → void
(?Integer chunk_size) → Enumerator[String, void]
Source
# File lib/xlsxrb/ooxml/zip_reader.rb, line 99 def each_chunk(chunk_size = DEFAULT_CHUNK_SIZE, &block) return enum_for(:each_chunk, chunk_size) unless block raise IOError, "closed stream" if @closed while (chunk = read(chunk_size)) break if chunk.nil? || chunk.empty? yield chunk end end
() → bool
Source
# File lib/xlsxrb/ooxml/zip_reader.rb, line 133 def eof? @eof && @buffer.bytesize == @buffer_pos end
(?Integer? length) → String?
Source
# File lib/xlsxrb/ooxml/zip_reader.rb, line 61 def read(length = nil) raise IOError, "closed stream" if @closed check_bom_encoding! if length.nil? result = @buffer.byteslice(@buffer_pos, @buffer.bytesize - @buffer_pos).b @buffer = +"".b @buffer_pos = 0 while (chunk = fetch_chunk(DEFAULT_CHUNK_SIZE)) result << chunk end result.force_encoding(@encoding) if @encoding return result end return String.new(encoding: @encoding || Encoding::BINARY) if length.zero? while @buffer.bytesize - @buffer_pos < length && !@eof chunk = fetch_chunk([length - (@buffer.bytesize - @buffer_pos), DEFAULT_CHUNK_SIZE].max) break unless chunk compact_buffer @buffer << chunk end available = @buffer.bytesize - @buffer_pos return nil if available.zero? && @eof sliced = @buffer.byteslice(@buffer_pos, [length, available].min) @buffer_pos += sliced.bytesize compact_buffer if @buffer_pos == @buffer.bytesize sliced.force_encoding(@encoding) if @encoding sliced end