ruby-brs 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,145 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ require_relative "abstract"
5
+ require_relative "raw/compressor"
6
+ require_relative "writer_helpers"
7
+
8
+ module BRS
9
+ module Stream
10
+ class Writer < Abstract
11
+ include WriterHelpers
12
+
13
+ def initialize(destination_io, options = {}, *args)
14
+ @options = options
15
+
16
+ super destination_io, *args
17
+ end
18
+
19
+ protected def create_raw_stream
20
+ Raw::Compressor.new @options
21
+ end
22
+
23
+ # -- synchronous --
24
+
25
+ def write(*objects)
26
+ write_remaining_buffer
27
+
28
+ bytes_written = 0
29
+
30
+ objects.each do |object|
31
+ source = transcode object.to_s
32
+ bytes_written += raw_wrapper :write, source
33
+ end
34
+
35
+ @pos += bytes_written
36
+
37
+ bytes_written
38
+ end
39
+
40
+ def flush
41
+ finish :flush
42
+
43
+ @io.flush
44
+
45
+ self
46
+ end
47
+
48
+ def rewind
49
+ finish :close
50
+
51
+ super
52
+ end
53
+
54
+ def close
55
+ finish :close
56
+
57
+ super
58
+ end
59
+
60
+ protected def finish(method_name)
61
+ write_remaining_buffer
62
+
63
+ raw_wrapper method_name
64
+ end
65
+
66
+ protected def write_remaining_buffer
67
+ return nil if @buffer.bytesize == 0
68
+
69
+ @io.write @buffer
70
+
71
+ reset_buffer
72
+ end
73
+
74
+ protected def raw_wrapper(method_name, *args)
75
+ @raw_stream.send(method_name, *args) { |portion| @io.write portion }
76
+ end
77
+
78
+ # -- asynchronous --
79
+
80
+ def write_nonblock(object, *options)
81
+ return 0 unless write_remaining_buffer_nonblock(*options)
82
+
83
+ source = transcode object.to_s
84
+ bytes_written = raw_nonblock_wrapper :write, source
85
+ @pos += bytes_written
86
+
87
+ bytes_written
88
+ end
89
+
90
+ def flush_nonblock(*options)
91
+ return false unless finish_nonblock :flush, *options
92
+
93
+ @io.flush
94
+
95
+ true
96
+ end
97
+
98
+ def rewind_nonblock(*options)
99
+ return false unless finish_nonblock :close, *options
100
+
101
+ method(:rewind).super_method.call
102
+
103
+ true
104
+ end
105
+
106
+ def close_nonblock(*options)
107
+ return false unless finish_nonblock :close, *options
108
+
109
+ method(:close).super_method.call
110
+
111
+ true
112
+ end
113
+
114
+ protected def finish_nonblock(method_name, *options)
115
+ return false unless write_remaining_buffer_nonblock(*options)
116
+
117
+ raw_nonblock_wrapper method_name
118
+
119
+ write_remaining_buffer_nonblock(*options)
120
+ end
121
+
122
+ protected def write_remaining_buffer_nonblock(*options)
123
+ return true if @buffer.bytesize == 0
124
+
125
+ bytes_written = @io.write_nonblock @buffer, *options
126
+ return false if bytes_written == 0
127
+
128
+ @buffer = @buffer.byteslice bytes_written, @buffer.bytesize - bytes_written
129
+
130
+ @buffer.bytesize == 0
131
+ end
132
+
133
+ protected def raw_nonblock_wrapper(method_name, *args)
134
+ @raw_stream.send(method_name, *args) { |portion| @buffer << portion }
135
+ end
136
+
137
+ # -- common --
138
+
139
+ protected def transcode(data)
140
+ data = data.encode @external_encoding, @transcode_options unless @external_encoding.nil?
141
+ data
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,93 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ require_relative "../error"
5
+ require_relative "../validation"
6
+
7
+ module BRS
8
+ module Stream
9
+ module WriterHelpers
10
+ def <<(object)
11
+ write object
12
+ end
13
+
14
+ def print(*objects)
15
+ if objects.empty?
16
+ write $LAST_READ_LINE unless $LAST_READ_LINE.nil?
17
+ return nil
18
+ end
19
+
20
+ objects.each do |object|
21
+ write object
22
+ write $OUTPUT_FIELD_SEPARATOR unless $OUTPUT_FIELD_SEPARATOR.nil?
23
+ end
24
+
25
+ write $OUTPUT_RECORD_SEPARATOR unless $OUTPUT_RECORD_SEPARATOR.nil?
26
+
27
+ nil
28
+ end
29
+
30
+ def printf(*args)
31
+ write sprintf(*args)
32
+
33
+ nil
34
+ end
35
+
36
+ def putc(object, encoding: ::Encoding::BINARY)
37
+ if object.is_a? ::Numeric
38
+ write object.chr(encoding)
39
+ elsif object.is_a? ::String
40
+ write object[0]
41
+ else
42
+ raise ValidateError, "invalid object: \"#{object}\" for putc"
43
+ end
44
+
45
+ object
46
+ end
47
+
48
+ def puts(*objects)
49
+ objects.each do |object|
50
+ if object.is_a? ::Array
51
+ puts(*object)
52
+ next
53
+ end
54
+
55
+ source = object.to_s
56
+ newline = "\n".encode source.encoding
57
+
58
+ # Do not add newline if source ends with newline.
59
+ if source.end_with? newline
60
+ write source
61
+ else
62
+ write source + newline
63
+ end
64
+ end
65
+
66
+ nil
67
+ end
68
+
69
+ # -- etc --
70
+
71
+ module ClassMethods
72
+ def open(file_path, *args, &block)
73
+ Validation.validate_string file_path
74
+ Validation.validate_proc block
75
+
76
+ ::File.open file_path, "wb" do |io|
77
+ writer = new io, *args
78
+
79
+ begin
80
+ yield writer
81
+ ensure
82
+ writer.close
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ def self.included(klass)
89
+ klass.extend ClassMethods
90
+ end
91
+ end
92
+ end
93
+ end
data/lib/brs/string.rb ADDED
@@ -0,0 +1,29 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ require "brs_ext"
5
+
6
+ require_relative "option"
7
+ require_relative "validation"
8
+
9
+ module BRS
10
+ module String
11
+ BUFFER_LENGTH_NAMES = %i[destination_buffer_length].freeze
12
+
13
+ def self.compress(source, options = {})
14
+ Validation.validate_string source
15
+
16
+ options = Option.get_compressor_options options, BUFFER_LENGTH_NAMES
17
+
18
+ BRS._native_compress_string source, options
19
+ end
20
+
21
+ def self.decompress(source, options = {})
22
+ Validation.validate_string source
23
+
24
+ options = Option.get_decompressor_options options, BUFFER_LENGTH_NAMES
25
+
26
+ BRS._native_decompress_string source, options
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ require_relative "error"
5
+
6
+ module BRS
7
+ module Validation
8
+ def self.validate_bool(value)
9
+ raise ValidateError, "invalid bool" unless value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
10
+ end
11
+
12
+ def self.validate_positive_integer(value)
13
+ raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value > 0
14
+ end
15
+
16
+ def self.validate_not_negative_integer(value)
17
+ raise ValidateError, "invalid not negative integer" unless value.is_a?(::Integer) && value >= 0
18
+ end
19
+
20
+ def self.validate_string(value)
21
+ raise ValidateError, "invalid string" unless value.is_a? ::String
22
+ end
23
+
24
+ def self.validate_symbol(value)
25
+ raise ValidateError, "invalid symbol" unless value.is_a? ::Symbol
26
+ end
27
+
28
+ def self.validate_io(value)
29
+ raise ValidateError, "invalid io" unless value.is_a? ::IO
30
+ end
31
+
32
+ def self.validate_hash(value)
33
+ raise ValidateError, "invalid hash" unless value.is_a? ::Hash
34
+ end
35
+
36
+ def self.validate_proc(value)
37
+ raise ValidateError, "invalid proc" unless value.is_a?(::Proc) || value.is_a?(::Method) || value.is_a?(::UnboundMethod)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ module BRS
5
+ VERSION = "1.0.1".freeze
6
+ end
data/lib/brs.rb ADDED
@@ -0,0 +1,7 @@
1
+ # Ruby bindings for brotli library.
2
+ # Copyright (c) 2019 AUTHORS, MIT License.
3
+
4
+ require_relative "brs/stream/reader"
5
+ require_relative "brs/stream/writer"
6
+ require_relative "brs/file"
7
+ require_relative "brs/string"
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-brs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Aladjev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.74'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.74'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.2'
97
+ description:
98
+ email: aladjev.andrew@gmail.com
99
+ executables: []
100
+ extensions:
101
+ - ext/extconf.rb
102
+ extra_rdoc_files: []
103
+ files:
104
+ - AUTHORS
105
+ - LICENSE
106
+ - README.md
107
+ - ext/brs_ext/buffer.c
108
+ - ext/brs_ext/buffer.h
109
+ - ext/brs_ext/common.h
110
+ - ext/brs_ext/error.c
111
+ - ext/brs_ext/error.h
112
+ - ext/brs_ext/io.c
113
+ - ext/brs_ext/io.h
114
+ - ext/brs_ext/macro.h
115
+ - ext/brs_ext/main.c
116
+ - ext/brs_ext/option.c
117
+ - ext/brs_ext/option.h
118
+ - ext/brs_ext/stream/compressor.c
119
+ - ext/brs_ext/stream/compressor.h
120
+ - ext/brs_ext/stream/decompressor.c
121
+ - ext/brs_ext/stream/decompressor.h
122
+ - ext/brs_ext/string.c
123
+ - ext/brs_ext/string.h
124
+ - ext/extconf.rb
125
+ - lib/brs.rb
126
+ - lib/brs/error.rb
127
+ - lib/brs/file.rb
128
+ - lib/brs/option.rb
129
+ - lib/brs/stream/abstract.rb
130
+ - lib/brs/stream/delegates.rb
131
+ - lib/brs/stream/raw/abstract.rb
132
+ - lib/brs/stream/raw/compressor.rb
133
+ - lib/brs/stream/raw/decompressor.rb
134
+ - lib/brs/stream/reader.rb
135
+ - lib/brs/stream/reader_helpers.rb
136
+ - lib/brs/stream/stat.rb
137
+ - lib/brs/stream/writer.rb
138
+ - lib/brs/stream/writer_helpers.rb
139
+ - lib/brs/string.rb
140
+ - lib/brs/validation.rb
141
+ - lib/brs/version.rb
142
+ homepage: https://github.com/andrew-aladev/ruby-brs
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.0.6
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Ruby bindings for brotli library.
165
+ test_files: []