ruby-zstds 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,156 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require_relative "delegates"
5
- require_relative "stat"
6
- require_relative "../error"
7
- require_relative "../validation"
8
-
9
- module ZSTDS
10
- module Stream
11
- class Abstract
12
- # Native stream is not seekable by design.
13
- # Related methods like "seek" and "pos=" can't be implemented.
14
-
15
- # It is not possible to maintain correspondance between bytes
16
- # consumed from source and bytes written to destination by design.
17
- # We will consume all source bytes and maintain buffer with remaining destination data.
18
-
19
- include Delegates
20
-
21
- attr_reader :io, :stat, :external_encoding, :internal_encoding, :transcode_options, :pos
22
-
23
- alias tell pos
24
-
25
- def initialize(io, options = {})
26
- @raw_stream = create_raw_stream
27
- @io = io
28
-
29
- @stat = Stat.new @io.stat if @io.respond_to? :stat
30
-
31
- set_encoding options[:external_encoding], options[:internal_encoding], options[:transcode_options]
32
- reset_buffer
33
- reset_io_advise
34
-
35
- @pos = 0
36
- end
37
-
38
- # -- buffer --
39
-
40
- protected def reset_buffer
41
- @buffer = ::String.new :encoding => ::Encoding::BINARY
42
- end
43
-
44
- # -- advise --
45
-
46
- protected def reset_io_advise
47
- # Both compressor and decompressor need sequential io access.
48
- @io.advise :sequential if @io.respond_to? :advise
49
- rescue ::Errno::ESPIPE
50
- # ok
51
- end
52
-
53
- def advise
54
- # Noop
55
- nil
56
- end
57
-
58
- # -- encoding --
59
-
60
- def set_encoding(*args)
61
- external_encoding, internal_encoding, transcode_options = process_set_encoding_arguments(*args)
62
-
63
- set_target_encoding :@external_encoding, external_encoding
64
- set_target_encoding :@internal_encoding, internal_encoding
65
- @transcode_options = transcode_options
66
-
67
- self
68
- end
69
-
70
- protected def process_set_encoding_arguments(*args)
71
- external_encoding = args[0]
72
-
73
- unless external_encoding.nil? || external_encoding.is_a?(::Encoding)
74
- Validation.validate_string external_encoding
75
-
76
- # First argument can be "external_encoding:internal_encoding".
77
- match = %r{(.+?):(.+)}.match external_encoding
78
-
79
- unless match.nil?
80
- external_encoding = match[0]
81
- internal_encoding = match[1]
82
-
83
- transcode_options = args[1]
84
- Validation.validate_hash transcode_options unless transcode_options.nil?
85
-
86
- return [external_encoding, internal_encoding, transcode_options]
87
- end
88
- end
89
-
90
- internal_encoding = args[1]
91
- unless internal_encoding.nil? || internal_encoding.is_a?(::Encoding)
92
- Validation.validate_string internal_encoding
93
- end
94
-
95
- transcode_options = args[2]
96
- Validation.validate_hash transcode_options unless transcode_options.nil?
97
-
98
- [external_encoding, internal_encoding, transcode_options]
99
- end
100
-
101
- protected def set_target_encoding(name, value)
102
- unless value.nil? || value.is_a?(::Encoding)
103
- begin
104
- value = ::Encoding.find value
105
- rescue ::ArgumentError
106
- raise ValidateError, "invalid #{name} encoding"
107
- end
108
- end
109
-
110
- instance_variable_set name, value
111
- end
112
-
113
- protected def target_encoding
114
- return @internal_encoding unless @internal_encoding.nil?
115
- return @external_encoding unless @external_encoding.nil?
116
-
117
- ::Encoding::BINARY
118
- end
119
-
120
- # -- etc --
121
-
122
- def rewind
123
- @raw_stream = create_raw_stream
124
-
125
- @io.rewind if @io.respond_to? :rewind
126
-
127
- reset_buffer
128
- reset_io_advise
129
-
130
- @pos = 0
131
-
132
- 0
133
- end
134
-
135
- def close
136
- @io.close if @io.respond_to? :close
137
-
138
- nil
139
- end
140
-
141
- def closed?
142
- return false unless @raw_stream.closed?
143
-
144
- if @io.respond_to? :closed
145
- @io.closed?
146
- else
147
- true
148
- end
149
- end
150
-
151
- def to_io
152
- self
153
- end
154
- end
155
- end
156
- end
@@ -1,36 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "forwardable"
5
-
6
- module ZSTDS
7
- module Stream
8
- module Delegates
9
- DELEGATES = %i[
10
- autoclose=
11
- autoclose?
12
- binmode
13
- binmode?
14
- close_on_exec=
15
- close_on_exec?
16
- fcntl
17
- fdatasync
18
- fileno
19
- fsync
20
- ioctl
21
- isatty
22
- pid
23
- sync
24
- sync=
25
- to_i
26
- tty?
27
- ]
28
- .freeze
29
-
30
- def self.included(klass)
31
- klass.extend ::Forwardable
32
- klass.def_delegators :@io, *DELEGATES
33
- end
34
- end
35
- end
36
- end
@@ -1,59 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "zstds_ext"
5
-
6
- require_relative "../../error"
7
- require_relative "../../validation"
8
-
9
- module ZSTDS
10
- module Stream
11
- module Raw
12
- class Abstract
13
- def initialize(native_stream)
14
- @native_stream = native_stream
15
- @is_closed = false
16
- end
17
-
18
- # -- write --
19
-
20
- def flush(&writer)
21
- write_result(&writer)
22
-
23
- nil
24
- end
25
-
26
- protected def more_destination(&writer)
27
- result_bytesize = write_result(&writer)
28
- raise NotEnoughDestinationError, "not enough destination" if result_bytesize.zero?
29
- end
30
-
31
- protected def write_result(&_writer)
32
- result = @native_stream.read_result
33
- yield result
34
-
35
- result.bytesize
36
- end
37
-
38
- # -- close --
39
-
40
- protected def do_not_use_after_close
41
- raise UsedAfterCloseError, "used after close" if closed?
42
- end
43
-
44
- def close(&writer)
45
- write_result(&writer)
46
-
47
- @native_stream.close
48
- @is_closed = true
49
-
50
- nil
51
- end
52
-
53
- def closed?
54
- @is_closed
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,194 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "English"
5
-
6
- require_relative "../validation"
7
-
8
- module ZSTDS
9
- module Stream
10
- module ReaderHelpers
11
- def getbyte
12
- read 1
13
- end
14
-
15
- def each_byte(&block)
16
- each_string method(:getbyte), &block
17
- end
18
-
19
- def readbyte
20
- readstring method(:getbyte)
21
- end
22
-
23
- def ungetbyte(byte)
24
- Validation.validate_string byte
25
-
26
- @buffer.prepend byte
27
-
28
- nil
29
- end
30
-
31
- # -- char --
32
-
33
- def getc
34
- if @external_encoding.nil?
35
- byte = getbyte
36
- return nil if byte.nil?
37
-
38
- return transcode_to_internal byte
39
- end
40
-
41
- char = ::String.new :encoding => ::Encoding::BINARY
42
-
43
- # Read one byte until valid string will appear.
44
- loop do
45
- byte = getbyte
46
- return nil if byte.nil?
47
-
48
- char << byte
49
-
50
- char.force_encoding @external_encoding
51
- return transcode_to_internal char if char.valid_encoding?
52
-
53
- char.force_encoding ::Encoding::BINARY
54
- end
55
- end
56
-
57
- def readchar
58
- readstring method(:getc)
59
- end
60
-
61
- def each_char(&block)
62
- each_string method(:getc), &block
63
- end
64
-
65
- def ungetc(char)
66
- ungetstring char
67
- end
68
-
69
- # -- lines --
70
-
71
- def gets(separator = $OUTPUT_RECORD_SEPARATOR, limit = nil)
72
- # Limit can be a first argument.
73
- if separator.is_a? ::Numeric
74
- limit = separator
75
- separator = $OUTPUT_RECORD_SEPARATOR
76
- end
77
-
78
- line_ending =
79
- if separator.nil?
80
- nil
81
- else
82
- Validation.validate_string separator
83
- ::String.new separator, :encoding => target_encoding
84
- end
85
-
86
- Validation.validate_positive_integer limit unless limit.nil?
87
-
88
- line = ::String.new :encoding => target_encoding
89
-
90
- loop do
91
- char = getc
92
-
93
- if char.nil?
94
- return nil if line.empty?
95
-
96
- break
97
- end
98
-
99
- line << char
100
-
101
- break if
102
- (!line_ending.nil? && line.end_with?(line_ending)) ||
103
- (!limit.nil? && line.length >= limit)
104
- end
105
-
106
- @lineno += 1
107
-
108
- line
109
- end
110
-
111
- def readline
112
- readstring method(:gets)
113
- end
114
-
115
- def readlines
116
- lines = []
117
- each_line { |line| lines << line }
118
-
119
- lines
120
- end
121
-
122
- def each_line(&block)
123
- each_string method(:gets), &block
124
- end
125
-
126
- alias each each_line
127
-
128
- def ungetline(line)
129
- ungetstring line
130
-
131
- @lineno -= 1
132
-
133
- nil
134
- end
135
-
136
- # -- common --
137
-
138
- protected def readstring(each_proc)
139
- string = each_proc.call
140
- raise ::EOFError if string.nil?
141
-
142
- string
143
- end
144
-
145
- protected def each_string(each_proc, &block)
146
- return enum_for __method__, each_proc unless block.is_a? ::Proc
147
-
148
- loop do
149
- string = each_proc.call
150
- break if string.nil?
151
-
152
- yield string
153
- end
154
-
155
- nil
156
- end
157
-
158
- protected def ungetstring(string)
159
- Validation.validate_string string
160
-
161
- string = ::String.new string, :encoding => @internal_encoding unless @internal_encoding.nil?
162
- string = transcode_to_external string unless @external_encoding.nil?
163
-
164
- string.force_encoding ::Encoding::BINARY
165
- @buffer.prepend string
166
-
167
- nil
168
- end
169
-
170
- # -- etc --
171
-
172
- module ClassMethods
173
- def open(file_path, *args, &block)
174
- Validation.validate_string file_path
175
- Validation.validate_proc block
176
-
177
- ::File.open file_path, "rb" do |io|
178
- reader = new io, *args
179
-
180
- begin
181
- yield reader
182
- ensure
183
- reader.close
184
- end
185
- end
186
- end
187
- end
188
-
189
- def self.included(klass)
190
- klass.extend ClassMethods
191
- end
192
- end
193
- end
194
- end
@@ -1,78 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "forwardable"
5
-
6
- module ZSTDS
7
- module Stream
8
- class Stat
9
- # Libraries like minitar tries to access stat to know whether stream is seekable.
10
- # We need to mark stream as not directory, file, etc, because it is not seekable.
11
-
12
- # User can use disabled delegates using :io reader.
13
-
14
- extend ::Forwardable
15
-
16
- METHODS_RETURNING_FALSE = %i[
17
- blockdev?
18
- chardev?
19
- directory?
20
- executable?
21
- executable_real?
22
- file?
23
- grpowned?
24
- owned?
25
- pipe?
26
- setgid?
27
- setuid?
28
- socket?
29
- sticky?
30
- symlink?
31
- zero?
32
- ]
33
- .freeze
34
-
35
- DELEGATES = %i[
36
- <=>
37
- atime
38
- birthtime
39
- blksize
40
- blocks
41
- ctime
42
- dev
43
- dev_major
44
- dev_minor
45
- ftype
46
- gid
47
- ino
48
- inspect
49
- mode
50
- mtime
51
- nlink
52
- rdev
53
- rdev_major
54
- rdev_minor
55
- readable?
56
- readable_real?
57
- size
58
- size?
59
- uid
60
- world_readable?
61
- world_writable?
62
- writable?
63
- writable_real?
64
- ]
65
- .freeze
66
-
67
- def initialize(stat)
68
- @stat = stat
69
- end
70
-
71
- METHODS_RETURNING_FALSE.each do |method_name|
72
- define_method(method_name) { false }
73
- end
74
-
75
- def_delegators :@stat, *DELEGATES
76
- end
77
- end
78
- end
@@ -1,91 +0,0 @@
1
- # Ruby bindings for zstd library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "English"
5
-
6
- require_relative "../error"
7
- require_relative "../validation"
8
-
9
- module ZSTDS
10
- module Stream
11
- module WriterHelpers
12
- def <<(object)
13
- write object
14
- end
15
-
16
- def print(*objects, field_separator: $OUTPUT_FIELD_SEPARATOR, record_separator: $OUTPUT_RECORD_SEPARATOR)
17
- objects.each do |object|
18
- write object
19
- write field_separator unless field_separator.nil?
20
- end
21
-
22
- write record_separator unless record_separator.nil?
23
-
24
- nil
25
- end
26
-
27
- def printf(*args)
28
- write sprintf(*args)
29
-
30
- nil
31
- end
32
-
33
- def putc(object, encoding: ::Encoding::BINARY)
34
- case object
35
- when ::Numeric
36
- write object.chr(encoding)
37
- when ::String
38
- write object[0]
39
- else
40
- raise ValidateError, "invalid object: \"#{object}\" for putc"
41
- end
42
-
43
- object
44
- end
45
-
46
- def puts(*objects)
47
- objects.each do |object|
48
- if object.is_a? ::Array
49
- puts(*object)
50
- next
51
- end
52
-
53
- source = object.to_s
54
- newline = "\n".encode source.encoding
55
-
56
- # Do not add newline if source ends with newline.
57
- if source.end_with? newline
58
- write source
59
- else
60
- write source + newline
61
- end
62
- end
63
-
64
- nil
65
- end
66
-
67
- # -- etc --
68
-
69
- module ClassMethods
70
- def open(file_path, *args, &block)
71
- Validation.validate_string file_path
72
- Validation.validate_proc block
73
-
74
- ::File.open file_path, "wb" do |io|
75
- writer = new io, *args
76
-
77
- begin
78
- yield writer
79
- ensure
80
- writer.close
81
- end
82
- end
83
- end
84
- end
85
-
86
- def self.included(klass)
87
- klass.extend ClassMethods
88
- end
89
- end
90
- end
91
- end