ruby-brs 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/brs/file.rb +4 -0
- data/lib/brs/stream/abstract.rb +1 -6
- data/lib/brs/stream/raw/abstract.rb +6 -2
- data/lib/brs/stream/raw/compressor.rb +3 -7
- data/lib/brs/stream/raw/decompressor.rb +1 -5
- data/lib/brs/stream/reader.rb +70 -51
- data/lib/brs/stream/writer.rb +9 -4
- data/lib/brs/stream/writer_helpers.rb +3 -2
- data/lib/brs/validation.rb +1 -1
- data/lib/brs/version.rb +1 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e466dd321946a82c2f6cf3c392c1c0dac066940d93cda2dc149409ef57b7491
|
4
|
+
data.tar.gz: d775c3fe8399adb3a8f514cf79c3fa1aa1eca505fa2e231595511e2606eb2a86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dd95aa6451b85d38f41c157a92907fdd14c2e3e54183ce49a498cde78bdefe94058c7646c2cc68035bccbd0fd9807d623da7f87a307c72b96603fe050c3817a
|
7
|
+
data.tar.gz: bc5ef430e925afb01859726b4103f6835813222cad3918431cb65c1df3e9ea7f1fb12390de0fa509121b90210cddc68f3599a89e34ce1a9372cabc0b13da57f0
|
data/README.md
CHANGED
@@ -36,6 +36,27 @@ BRS::File.decompress "file.txt.br", "file.txt"
|
|
36
36
|
|
37
37
|
BRS::Stream::Writer.open("file.txt.br") { |writer| writer << "sample string" }
|
38
38
|
puts BRS::Stream::Reader.open("file.txt.br") { |reader| reader.read }
|
39
|
+
|
40
|
+
writer = BRS::Stream::Writer.new output_socket
|
41
|
+
begin
|
42
|
+
bytes_written = writer.write_nonblock "sample string"
|
43
|
+
# handle "bytes_written"
|
44
|
+
rescue IO::WaitWritable
|
45
|
+
# handle wait
|
46
|
+
ensure
|
47
|
+
writer.close
|
48
|
+
end
|
49
|
+
|
50
|
+
reader = BRS::Stream::Reader.new input_socket
|
51
|
+
begin
|
52
|
+
puts reader.read_nonblock(512)
|
53
|
+
rescue IO::WaitReadable
|
54
|
+
# handle wait
|
55
|
+
rescue ::EOFError
|
56
|
+
# handle eof
|
57
|
+
ensure
|
58
|
+
reader.close
|
59
|
+
end
|
39
60
|
```
|
40
61
|
|
41
62
|
You can create and read `tar.br` archives with `minitar` for example.
|
data/lib/brs/file.rb
CHANGED
@@ -22,6 +22,8 @@ module BRS
|
|
22
22
|
open_files(source, destination) do |source_io, destination_io|
|
23
23
|
BRS._native_compress_io source_io, destination_io, options
|
24
24
|
end
|
25
|
+
|
26
|
+
nil
|
25
27
|
end
|
26
28
|
|
27
29
|
def self.decompress(source, destination, options = {})
|
@@ -33,6 +35,8 @@ module BRS
|
|
33
35
|
open_files(source, destination) do |source_io, destination_io|
|
34
36
|
BRS._native_decompress_io source_io, destination_io, options
|
35
37
|
end
|
38
|
+
|
39
|
+
nil
|
36
40
|
end
|
37
41
|
|
38
42
|
private_class_method def self.open_files(source, destination, &_block)
|
data/lib/brs/stream/abstract.rb
CHANGED
@@ -17,12 +17,7 @@ module BRS
|
|
17
17
|
|
18
18
|
include Delegates
|
19
19
|
|
20
|
-
attr_reader :io
|
21
|
-
attr_reader :stat
|
22
|
-
attr_reader :external_encoding
|
23
|
-
attr_reader :internal_encoding
|
24
|
-
attr_reader :transcode_options
|
25
|
-
attr_reader :pos
|
20
|
+
attr_reader :io, :stat, :external_encoding, :internal_encoding, :transcode_options, :pos
|
26
21
|
|
27
22
|
alias tell pos
|
28
23
|
|
@@ -19,11 +19,13 @@ module BRS
|
|
19
19
|
|
20
20
|
def flush(&writer)
|
21
21
|
write_result(&writer)
|
22
|
+
|
23
|
+
nil
|
22
24
|
end
|
23
25
|
|
24
|
-
protected def
|
26
|
+
protected def more_destination(&writer)
|
25
27
|
result_bytesize = write_result(&writer)
|
26
|
-
raise NotEnoughDestinationError, "not enough destination" if result_bytesize
|
28
|
+
raise NotEnoughDestinationError, "not enough destination" if result_bytesize.zero?
|
27
29
|
end
|
28
30
|
|
29
31
|
protected def write_result(&_writer)
|
@@ -44,6 +46,8 @@ module BRS
|
|
44
46
|
|
45
47
|
@native_stream.close
|
46
48
|
@is_closed = true
|
49
|
+
|
50
|
+
nil
|
47
51
|
end
|
48
52
|
|
49
53
|
def closed?
|
@@ -39,7 +39,7 @@ module BRS
|
|
39
39
|
|
40
40
|
if need_more_destination
|
41
41
|
source = source.byteslice bytes_written, source.bytesize - bytes_written
|
42
|
-
|
42
|
+
more_destination(&writer)
|
43
43
|
next
|
44
44
|
end
|
45
45
|
|
@@ -65,7 +65,7 @@ module BRS
|
|
65
65
|
need_more_destination = @native_stream.flush
|
66
66
|
|
67
67
|
if need_more_destination
|
68
|
-
|
68
|
+
more_destination(&writer)
|
69
69
|
next
|
70
70
|
end
|
71
71
|
|
@@ -73,8 +73,6 @@ module BRS
|
|
73
73
|
end
|
74
74
|
|
75
75
|
super
|
76
|
-
|
77
|
-
nil
|
78
76
|
end
|
79
77
|
|
80
78
|
def close(&writer)
|
@@ -86,7 +84,7 @@ module BRS
|
|
86
84
|
need_more_destination = @native_stream.finish
|
87
85
|
|
88
86
|
if need_more_destination
|
89
|
-
|
87
|
+
more_destination(&writer)
|
90
88
|
next
|
91
89
|
end
|
92
90
|
|
@@ -94,8 +92,6 @@ module BRS
|
|
94
92
|
end
|
95
93
|
|
96
94
|
super
|
97
|
-
|
98
|
-
nil
|
99
95
|
end
|
100
96
|
end
|
101
97
|
end
|
@@ -34,7 +34,7 @@ module BRS
|
|
34
34
|
|
35
35
|
if need_more_destination
|
36
36
|
source = source.byteslice bytes_read, source.bytesize - bytes_read
|
37
|
-
|
37
|
+
more_destination(&writer)
|
38
38
|
next
|
39
39
|
end
|
40
40
|
|
@@ -51,8 +51,6 @@ module BRS
|
|
51
51
|
Validation.validate_proc writer
|
52
52
|
|
53
53
|
super
|
54
|
-
|
55
|
-
nil
|
56
54
|
end
|
57
55
|
|
58
56
|
def close(&writer)
|
@@ -61,8 +59,6 @@ module BRS
|
|
61
59
|
Validation.validate_proc writer
|
62
60
|
|
63
61
|
super
|
64
|
-
|
65
|
-
nil
|
66
62
|
end
|
67
63
|
end
|
68
64
|
end
|
data/lib/brs/stream/reader.rb
CHANGED
@@ -20,79 +20,60 @@ module BRS
|
|
20
20
|
|
21
21
|
initialize_source_buffer_length
|
22
22
|
reset_io_remainder
|
23
|
+
reset_need_to_flush
|
23
24
|
|
24
25
|
@lineno = 0
|
25
26
|
end
|
26
27
|
|
28
|
+
protected def create_raw_stream
|
29
|
+
Raw::Decompressor.new @options
|
30
|
+
end
|
31
|
+
|
27
32
|
protected def initialize_source_buffer_length
|
28
33
|
source_buffer_length = @options[:source_buffer_length]
|
29
34
|
Validation.validate_not_negative_integer source_buffer_length unless source_buffer_length.nil?
|
30
35
|
|
31
36
|
source_buffer_length = Buffer::DEFAULT_SOURCE_BUFFER_LENGTH_FOR_DECOMPRESSOR \
|
32
|
-
if source_buffer_length
|
37
|
+
if source_buffer_length.nil? || source_buffer_length.zero?
|
33
38
|
|
34
39
|
@source_buffer_length = source_buffer_length
|
35
40
|
end
|
36
41
|
|
37
|
-
protected def create_raw_stream
|
38
|
-
Raw::Decompressor.new @options
|
39
|
-
end
|
40
|
-
|
41
42
|
protected def reset_io_remainder
|
42
43
|
@io_remainder = ::String.new :encoding => ::Encoding::BINARY
|
43
44
|
end
|
44
45
|
|
46
|
+
protected def reset_need_to_flush
|
47
|
+
@need_to_flush = false
|
48
|
+
end
|
49
|
+
|
45
50
|
# -- synchronous --
|
46
51
|
|
47
52
|
def read(bytes_to_read = nil, out_buffer = nil)
|
48
53
|
Validation.validate_not_negative_integer bytes_to_read unless bytes_to_read.nil?
|
49
54
|
Validation.validate_string out_buffer unless out_buffer.nil?
|
50
55
|
|
51
|
-
return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read == 0
|
52
|
-
|
53
56
|
unless bytes_to_read.nil?
|
57
|
+
return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
|
54
58
|
return nil if eof?
|
55
59
|
|
56
|
-
|
60
|
+
append_io_data @io.read(@source_buffer_length) while @buffer.bytesize < bytes_to_read && !@io.eof?
|
61
|
+
flush_io_data if @buffer.bytesize < bytes_to_read
|
57
62
|
|
58
63
|
return read_bytes_from_buffer bytes_to_read, out_buffer
|
59
64
|
end
|
60
65
|
|
61
|
-
|
62
|
-
|
63
|
-
result = @buffer
|
64
|
-
reset_buffer
|
65
|
-
@pos += result.bytesize
|
66
|
+
append_io_data @io.read(@source_buffer_length) until @io.eof?
|
67
|
+
flush_io_data
|
66
68
|
|
67
|
-
|
68
|
-
result = transcode_to_internal result
|
69
|
-
result = out_buffer.replace result unless out_buffer.nil?
|
70
|
-
|
71
|
-
result
|
72
|
-
end
|
73
|
-
|
74
|
-
protected def read_more_to_buffer
|
75
|
-
io_data = @io.read @source_buffer_length
|
76
|
-
append_io_data_to_buffer io_data
|
77
|
-
end
|
78
|
-
|
79
|
-
def readpartial(bytes_to_read = nil, out_buffer = nil)
|
80
|
-
raise ::EOFError if eof?
|
81
|
-
|
82
|
-
readpartial_to_buffer until @buffer.bytesize >= bytes_to_read || @io.eof?
|
83
|
-
|
84
|
-
read_bytes_from_buffer bytes_to_read, out_buffer
|
85
|
-
end
|
86
|
-
|
87
|
-
protected def readpartial_to_buffer
|
88
|
-
io_data = @io.readpartial @source_buffer_length
|
89
|
-
append_io_data_to_buffer io_data
|
69
|
+
read_buffer out_buffer
|
90
70
|
end
|
91
71
|
|
92
72
|
def rewind
|
93
73
|
raw_wrapper :close
|
94
74
|
|
95
75
|
reset_io_remainder
|
76
|
+
reset_need_to_flush
|
96
77
|
|
97
78
|
super
|
98
79
|
end
|
@@ -103,25 +84,61 @@ module BRS
|
|
103
84
|
super
|
104
85
|
end
|
105
86
|
|
87
|
+
def eof?
|
88
|
+
empty? && @io.eof?
|
89
|
+
end
|
90
|
+
|
106
91
|
# -- asynchronous --
|
107
92
|
|
93
|
+
def readpartial(bytes_to_read, out_buffer = nil)
|
94
|
+
read_more_nonblock(bytes_to_read, out_buffer) { @io.readpartial @source_buffer_length }
|
95
|
+
end
|
96
|
+
|
108
97
|
def read_nonblock(bytes_to_read, out_buffer = nil, *options)
|
109
|
-
|
98
|
+
read_more_nonblock(bytes_to_read, out_buffer) { @io.read_nonblock(@source_buffer_length, *options) }
|
99
|
+
end
|
100
|
+
|
101
|
+
protected def read_more_nonblock(bytes_to_read, out_buffer, &_block)
|
102
|
+
Validation.validate_not_negative_integer bytes_to_read
|
103
|
+
Validation.validate_string out_buffer unless out_buffer.nil?
|
104
|
+
|
105
|
+
return ::String.new :encoding => ::Encoding::BINARY if bytes_to_read.zero?
|
106
|
+
|
107
|
+
io_provided_eof_error = false
|
110
108
|
|
111
|
-
|
109
|
+
if @buffer.bytesize < bytes_to_read
|
110
|
+
begin
|
111
|
+
append_io_data yield
|
112
|
+
rescue ::EOFError
|
113
|
+
io_provided_eof_error = true
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
flush_io_data if @buffer.bytesize < bytes_to_read
|
118
|
+
raise ::EOFError if empty? && io_provided_eof_error
|
112
119
|
|
113
120
|
read_bytes_from_buffer bytes_to_read, out_buffer
|
114
121
|
end
|
115
122
|
|
116
|
-
|
117
|
-
|
118
|
-
|
123
|
+
# -- common --
|
124
|
+
|
125
|
+
protected def append_io_data(io_data)
|
126
|
+
io_portion = @io_remainder + io_data
|
127
|
+
bytes_read = raw_wrapper :read, io_portion
|
128
|
+
@io_remainder = io_portion.byteslice bytes_read, io_portion.bytesize - bytes_read
|
129
|
+
|
130
|
+
# Even empty io data may require flush.
|
131
|
+
@need_to_flush = true
|
119
132
|
end
|
120
133
|
|
121
|
-
|
134
|
+
protected def flush_io_data
|
135
|
+
raw_wrapper :flush
|
122
136
|
|
123
|
-
|
124
|
-
|
137
|
+
@need_to_flush = false
|
138
|
+
end
|
139
|
+
|
140
|
+
protected def empty?
|
141
|
+
!@need_to_flush && @buffer.bytesize.zero?
|
125
142
|
end
|
126
143
|
|
127
144
|
protected def read_bytes_from_buffer(bytes_to_read, out_buffer)
|
@@ -136,14 +153,16 @@ module BRS
|
|
136
153
|
result
|
137
154
|
end
|
138
155
|
|
139
|
-
protected def
|
140
|
-
|
141
|
-
|
142
|
-
@
|
156
|
+
protected def read_buffer(out_buffer)
|
157
|
+
result = @buffer
|
158
|
+
reset_buffer
|
159
|
+
@pos += result.bytesize
|
143
160
|
|
144
|
-
|
145
|
-
|
146
|
-
|
161
|
+
result.force_encoding @external_encoding unless @external_encoding.nil?
|
162
|
+
result = transcode_to_internal result
|
163
|
+
|
164
|
+
result = out_buffer.replace result unless out_buffer.nil?
|
165
|
+
result
|
147
166
|
end
|
148
167
|
|
149
168
|
protected def transcode_to_internal(data)
|
data/lib/brs/stream/writer.rb
CHANGED
@@ -64,7 +64,7 @@ module BRS
|
|
64
64
|
end
|
65
65
|
|
66
66
|
protected def write_remaining_buffer
|
67
|
-
return nil if @buffer.bytesize
|
67
|
+
return nil if @buffer.bytesize.zero?
|
68
68
|
|
69
69
|
@io.write @buffer
|
70
70
|
|
@@ -77,6 +77,11 @@ module BRS
|
|
77
77
|
|
78
78
|
# -- asynchronous --
|
79
79
|
|
80
|
+
# IO write nonblock can raise wait writable error.
|
81
|
+
# After resolving this error user may provide same content again.
|
82
|
+
# It is not possible to revert accepted content after error.
|
83
|
+
# So we have to accept content after processing IO write nonblock.
|
84
|
+
# It means that first write nonblock won't call IO write nonblock.
|
80
85
|
def write_nonblock(object, *options)
|
81
86
|
return 0 unless write_remaining_buffer_nonblock(*options)
|
82
87
|
|
@@ -120,14 +125,14 @@ module BRS
|
|
120
125
|
end
|
121
126
|
|
122
127
|
protected def write_remaining_buffer_nonblock(*options)
|
123
|
-
return true if @buffer.bytesize
|
128
|
+
return true if @buffer.bytesize.zero?
|
124
129
|
|
125
130
|
bytes_written = @io.write_nonblock @buffer, *options
|
126
|
-
return false if bytes_written
|
131
|
+
return false if bytes_written.zero?
|
127
132
|
|
128
133
|
@buffer = @buffer.byteslice bytes_written, @buffer.bytesize - bytes_written
|
129
134
|
|
130
|
-
@buffer.bytesize
|
135
|
+
@buffer.bytesize.zero?
|
131
136
|
end
|
132
137
|
|
133
138
|
protected def raw_nonblock_wrapper(method_name, *args)
|
@@ -31,9 +31,10 @@ module BRS
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def putc(object, encoding: ::Encoding::BINARY)
|
34
|
-
|
34
|
+
case object
|
35
|
+
when ::Numeric
|
35
36
|
write object.chr(encoding)
|
36
|
-
|
37
|
+
when ::String
|
37
38
|
write object[0]
|
38
39
|
else
|
39
40
|
raise ValidateError, "invalid object: \"#{object}\" for putc"
|
data/lib/brs/validation.rb
CHANGED
@@ -23,7 +23,7 @@ module BRS
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.validate_positive_integer(value)
|
26
|
-
raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value
|
26
|
+
raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value.positive?
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.validate_not_negative_integer(value)
|
data/lib/brs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-brs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Aladjev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|
@@ -122,20 +122,6 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '1.5'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: rubocop-rails
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - "~>"
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '2.3'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - "~>"
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '2.3'
|
139
125
|
- !ruby/object:Gem::Dependency
|
140
126
|
name: simplecov
|
141
127
|
requirement: !ruby/object:Gem::Requirement
|