Skip to content

Commit

Permalink
refactoring stream reader, cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-aladev committed Jul 15, 2020
1 parent d98eed3 commit 8ad5c3a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
4 changes: 4 additions & 0 deletions lib/zstds/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def self.compress(source, destination, options = {})
open_files(source, destination) do |source_io, destination_io|
ZSTDS._native_compress_io source_io, destination_io, options
end

nil
end

def self.decompress(source, destination, options = {})
Expand All @@ -33,6 +35,8 @@ def self.decompress(source, destination, options = {})
open_files(source, destination) do |source_io, destination_io|
ZSTDS._native_decompress_io source_io, destination_io, options
end

nil
end

private_class_method def self.open_files(source, destination, &_block)
Expand Down
4 changes: 4 additions & 0 deletions lib/zstds/stream/raw/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def initialize(native_stream)

def flush(&writer)
write_result(&writer)

nil
end

protected def flush_destination_buffer(&writer)
Expand All @@ -44,6 +46,8 @@ def close(&writer)

@native_stream.close
@is_closed = true

nil
end

def closed?
Expand Down
4 changes: 0 additions & 4 deletions lib/zstds/stream/raw/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ def flush(&writer)
end

super

nil
end

def close(&writer)
Expand All @@ -94,8 +92,6 @@ def close(&writer)
end

super

nil
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/zstds/stream/raw/decompressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ def flush(&writer)
Validation.validate_proc writer

super

nil
end

def close(&writer)
Expand All @@ -61,8 +59,6 @@ def close(&writer)
Validation.validate_proc writer

super

nil
end
end
end
Expand Down
34 changes: 16 additions & 18 deletions lib/zstds/stream/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def initialize(source_io, options = {}, *args)
@lineno = 0
end

protected def create_raw_stream
Raw::Decompressor.new @options
end

protected def initialize_source_buffer_length
source_buffer_length = @options[:source_buffer_length]
Validation.validate_not_negative_integer source_buffer_length unless source_buffer_length.nil?
Expand All @@ -34,10 +38,6 @@ def initialize(source_io, options = {}, *args)
@source_buffer_length = source_buffer_length
end

protected def create_raw_stream
Raw::Decompressor.new @options
end

protected def reset_io_remainder
@io_remainder = ::String.new :encoding => ::Encoding::BINARY
end
Expand Down Expand Up @@ -103,11 +103,13 @@ def close
super
end

def eof?
@buffer.bytesize.zero? && @io.eof?
end

# -- asynchronous --

def read_nonblock(bytes_to_read, out_buffer = nil, *options)
raise ::EOFError if eof?

read_more_to_buffer_nonblock(*options) until @buffer.bytesize >= bytes_to_read || @io.eof?

read_bytes_from_buffer bytes_to_read, out_buffer
Expand All @@ -120,8 +122,14 @@ def read_nonblock(bytes_to_read, out_buffer = nil, *options)

# -- common --

def eof?
@io.eof? && @buffer.bytesize.zero?
protected def append_io_data_to_buffer(io_data)
io_portion = @io_remainder + io_data
bytes_read = raw_wrapper :read, io_portion
@io_remainder = io_portion.byteslice bytes_read, io_portion.bytesize - bytes_read

# We should just ignore case when "io.eof?" appears but "io_remainder" is not empty.
# Ancient compress implementations can write bytes from not initialized buffer parts to output.
raw_wrapper :flush if @io.eof?
end

protected def read_bytes_from_buffer(bytes_to_read, out_buffer)
Expand All @@ -136,16 +144,6 @@ def eof?
result
end

protected def append_io_data_to_buffer(io_data)
io_portion = @io_remainder + io_data
bytes_read = raw_wrapper :read, io_portion
@io_remainder = io_portion.byteslice bytes_read, io_portion.bytesize - bytes_read

# We should just ignore case when "io.eof?" appears but "io_remainder" is not empty.
# Ancient compress implementations can write bytes from not initialized buffer parts to output.
raw_wrapper :flush if @io.eof?
end

protected def transcode_to_internal(data)
data = data.encode @internal_encoding, **@transcode_options unless @internal_encoding.nil?
data
Expand Down

0 comments on commit 8ad5c3a

Please sign in to comment.