ruby-lzws 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,6 @@
4
4
  require "lzws_ext"
5
5
 
6
6
  require_relative "abstract"
7
- require_relative "../../error"
8
7
  require_relative "../../option"
9
8
  require_relative "../../validation"
10
9
 
@@ -12,13 +11,13 @@ module LZWS
12
11
  module Stream
13
12
  module Raw
14
13
  class Decompressor < Abstract
14
+ BUFFER_LENGTH_NAMES = %i[destination_buffer_length].freeze
15
+
15
16
  def initialize(options = {})
16
- options = Option.get_decompressor_options options
17
+ options = Option.get_decompressor_options options, BUFFER_LENGTH_NAMES
17
18
  native_stream = NativeDecompressor.new options
18
19
 
19
20
  super native_stream
20
-
21
- @need_to_read_magic_header = !options[:without_magic_header]
22
21
  end
23
22
 
24
23
  def read(source, &writer)
@@ -29,19 +28,6 @@ module LZWS
29
28
 
30
29
  total_bytes_read = 0
31
30
 
32
- if @need_to_read_magic_header
33
- bytes_read = @native_stream.read_magic_header source
34
- if bytes_read == 0
35
- # Decompressor is not able to read full magic header.
36
- return 0
37
- end
38
-
39
- total_bytes_read += bytes_read
40
- source = source.byteslice bytes_read, source.bytesize - bytes_read
41
-
42
- @need_to_read_magic_header = false
43
- end
44
-
45
31
  loop do
46
32
  bytes_read, need_more_destination = @native_stream.read source
47
33
  total_bytes_read += bytes_read
@@ -59,19 +45,15 @@ module LZWS
59
45
  total_bytes_read
60
46
  end
61
47
 
62
- def flush(&writer)
63
- do_not_use_after_close
48
+ def close(&writer)
49
+ return nil if closed?
64
50
 
65
51
  Validation.validate_proc writer
66
52
 
67
- write_result(&writer)
53
+ super
68
54
 
69
55
  nil
70
56
  end
71
-
72
- protected def do_not_use_after_close
73
- raise UsedAfterCloseError, "decompressor used after close" if @is_closed
74
- end
75
57
  end
76
58
  end
77
59
  end
@@ -11,8 +11,6 @@ module LZWS
11
11
  class Reader < Abstract
12
12
  include ReaderHelpers
13
13
 
14
- DEFAULT_IO_PORTION_BYTESIZE = 1 << 15 # 32 KB
15
-
16
14
  attr_accessor :lineno
17
15
 
18
16
  def initialize(source_io, options = {}, *args)
@@ -20,17 +18,22 @@ module LZWS
20
18
 
21
19
  super source_io, *args
22
20
 
23
- io_portion_bytesize = @options[:io_portion_bytesize]
24
- @options.delete :io_portion_bytesize
25
-
26
- Validation.validate_positive_integer io_portion_bytesize unless io_portion_bytesize.nil?
27
- @io_portion_bytesize = io_portion_bytesize || DEFAULT_IO_PORTION_BYTESIZE
28
-
21
+ initialize_source_buffer_length
29
22
  reset_io_remainder
30
23
 
31
24
  @lineno = 0
32
25
  end
33
26
 
27
+ protected def initialize_source_buffer_length
28
+ source_buffer_length = @options[:source_buffer_length]
29
+ Validation.validate_not_negative_integer source_buffer_length unless source_buffer_length.nil?
30
+
31
+ source_buffer_length = Buffer::DEFAULT_SOURCE_BUFFER_LENGTH_FOR_DECOMPRESSOR \
32
+ if source_buffer_length == 0 || source_buffer_length.nil?
33
+
34
+ @source_buffer_length = source_buffer_length
35
+ end
36
+
34
37
  protected def create_raw_stream
35
38
  Raw::Decompressor.new @options
36
39
  end
@@ -69,7 +72,7 @@ module LZWS
69
72
  end
70
73
 
71
74
  protected def read_more_from_buffer
72
- io_data = @io.read @io_portion_bytesize
75
+ io_data = @io.read @source_buffer_length
73
76
  append_io_data_to_buffer io_data
74
77
  end
75
78
 
@@ -82,7 +85,7 @@ module LZWS
82
85
  end
83
86
 
84
87
  protected def readpartial_from_buffer
85
- io_data = @io.readpartial @io_portion_bytesize
88
+ io_data = @io.readpartial @source_buffer_length
86
89
  append_io_data_to_buffer io_data
87
90
  end
88
91
 
@@ -111,7 +114,7 @@ module LZWS
111
114
  end
112
115
 
113
116
  protected def read_more_from_buffer_nonblock(*options)
114
- io_data = @io.read_nonblock @io_portion_bytesize, *options
117
+ io_data = @io.read_nonblock @source_buffer_length, *options
115
118
  append_io_data_to_buffer io_data
116
119
  end
117
120
 
@@ -156,7 +156,7 @@ module LZWS
156
156
  protected def ungetstring(string)
157
157
  Validation.validate_string string
158
158
 
159
- string = ::String.new string, :encoding => @internal_encoding
159
+ string = ::String.new string, :encoding => @internal_encoding unless @internal_encoding.nil?
160
160
  string = transcode_to_external string unless @external_encoding.nil?
161
161
 
162
162
  string.force_encoding ::Encoding::BINARY
@@ -172,8 +172,8 @@ module LZWS
172
172
  Validation.validate_string file_path
173
173
  Validation.validate_proc block
174
174
 
175
- ::File.open file_path, "rb" do |file|
176
- reader = new file, *args
175
+ ::File.open file_path, "rb" do |io|
176
+ reader = new io, *args
177
177
 
178
178
  begin
179
179
  yield reader
@@ -73,8 +73,8 @@ module LZWS
73
73
  Validation.validate_string file_path
74
74
  Validation.validate_proc block
75
75
 
76
- ::File.open file_path, "wb" do |file|
77
- writer = new file, *args
76
+ ::File.open file_path, "wb" do |io|
77
+ writer = new io, *args
78
78
 
79
79
  begin
80
80
  yield writer
data/lib/lzws/string.rb CHANGED
@@ -8,10 +8,12 @@ require_relative "validation"
8
8
 
9
9
  module LZWS
10
10
  module String
11
+ BUFFER_LENGTH_NAMES = %i[destination_buffer_length].freeze
12
+
11
13
  def self.compress(source, options = {})
12
14
  Validation.validate_string source
13
15
 
14
- options = Option.get_compressor_options options
16
+ options = Option.get_compressor_options options, BUFFER_LENGTH_NAMES
15
17
 
16
18
  LZWS._native_compress_string source, options
17
19
  end
@@ -19,7 +21,7 @@ module LZWS
19
21
  def self.decompress(source, options = {})
20
22
  Validation.validate_string source
21
23
 
22
- options = Option.get_decompressor_options options
24
+ options = Option.get_decompressor_options options, BUFFER_LENGTH_NAMES
23
25
 
24
26
  LZWS._native_decompress_string source, options
25
27
  end
data/lib/lzws/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
4
  module LZWS
5
- VERSION = "1.0.0".freeze
5
+ VERSION = "1.1.0".freeze
6
6
  end
data/lib/lzws.rb CHANGED
@@ -1,4 +1,7 @@
1
- require_relative "lzws/file"
1
+ # Ruby bindings for lzws library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
2
4
  require_relative "lzws/stream/reader"
3
5
  require_relative "lzws/stream/writer"
6
+ require_relative "lzws/file"
4
7
  require_relative "lzws/string"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lzws
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Aladjev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-08 00:00:00.000000000 Z
11
+ date: 2019-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitar
@@ -105,6 +105,8 @@ files:
105
105
  - LICENSE
106
106
  - README.md
107
107
  - ext/extconf.rb
108
+ - ext/lzws_ext/buffer.c
109
+ - ext/lzws_ext/buffer.h
108
110
  - ext/lzws_ext/common.h
109
111
  - ext/lzws_ext/error.c
110
112
  - ext/lzws_ext/error.h
@@ -156,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
158
  - !ruby/object:Gem::Version
157
159
  version: '0'
158
160
  requirements: []
159
- rubygems_version: 3.0.3
161
+ rubygems_version: 3.0.6
160
162
  signing_key:
161
163
  specification_version: 4
162
164
  summary: Ruby bindings for lzws library.