ruby-zstds 1.0.1 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +90 -159
- data/ext/extconf.rb +13 -0
- data/ext/zstds_ext/common.h +3 -0
- data/ext/zstds_ext/dictionary.c +1 -4
- data/ext/zstds_ext/error.c +0 -1
- data/ext/zstds_ext/io.c +37 -40
- data/ext/zstds_ext/main.c +0 -1
- data/ext/zstds_ext/option.c +0 -4
- data/ext/zstds_ext/option.h +3 -5
- data/ext/zstds_ext/stream/compressor.c +6 -9
- data/ext/zstds_ext/stream/compressor.h +6 -7
- data/ext/zstds_ext/stream/decompressor.c +6 -9
- data/ext/zstds_ext/stream/decompressor.h +6 -7
- data/ext/zstds_ext/string.c +2 -5
- data/lib/zstds/file.rb +4 -0
- data/lib/zstds/option.rb +2 -3
- data/lib/zstds/stream/abstract.rb +9 -12
- data/lib/zstds/stream/raw/abstract.rb +6 -2
- data/lib/zstds/stream/raw/compressor.rb +5 -7
- data/lib/zstds/stream/raw/decompressor.rb +1 -5
- data/lib/zstds/stream/reader.rb +71 -52
- data/lib/zstds/stream/reader_helpers.rb +2 -0
- data/lib/zstds/stream/writer.rb +10 -5
- data/lib/zstds/stream/writer_helpers.rb +8 -10
- data/lib/zstds/validation.rb +15 -2
- data/lib/zstds/version.rb +1 -1
- metadata +41 -13
data/ext/zstds_ext/main.c
CHANGED
data/ext/zstds_ext/option.c
CHANGED
data/ext/zstds_ext/option.h
CHANGED
@@ -5,8 +5,6 @@
|
|
5
5
|
#define ZSTDS_EXT_OPTIONS_H
|
6
6
|
|
7
7
|
#include <stdbool.h>
|
8
|
-
#include <stdint.h>
|
9
|
-
#include <stdlib.h>
|
10
8
|
#include <zstd.h>
|
11
9
|
|
12
10
|
#include "ruby.h"
|
@@ -22,9 +20,9 @@ enum {
|
|
22
20
|
ZSTDS_EXT_OPTION_TYPE_STRATEGY
|
23
21
|
};
|
24
22
|
|
25
|
-
typedef
|
26
|
-
typedef int
|
27
|
-
typedef unsigned long long
|
23
|
+
typedef zstds_ext_byte_fast_t zstds_ext_option_type_t;
|
24
|
+
typedef int zstds_ext_option_value_t;
|
25
|
+
typedef unsigned long long zstds_ext_ull_option_value_t;
|
28
26
|
|
29
27
|
typedef struct {
|
30
28
|
bool has_value;
|
@@ -3,12 +3,9 @@
|
|
3
3
|
|
4
4
|
#include "zstds_ext/stream/compressor.h"
|
5
5
|
|
6
|
-
#include <stdint.h>
|
7
|
-
#include <stdlib.h>
|
8
6
|
#include <zstd.h>
|
9
7
|
|
10
8
|
#include "ruby.h"
|
11
|
-
#include "zstds_ext/common.h"
|
12
9
|
#include "zstds_ext/error.h"
|
13
10
|
#include "zstds_ext/option.h"
|
14
11
|
|
@@ -19,7 +16,7 @@ static void free_compressor(zstds_ext_compressor_t* compressor_ptr)
|
|
19
16
|
ZSTD_freeCCtx(ctx);
|
20
17
|
}
|
21
18
|
|
22
|
-
|
19
|
+
zstds_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
23
20
|
if (destination_buffer != NULL) {
|
24
21
|
free(destination_buffer);
|
25
22
|
}
|
@@ -68,7 +65,7 @@ VALUE zstds_ext_initialize_compressor(VALUE self, VALUE options)
|
|
68
65
|
destination_buffer_length = ZSTD_CStreamOutSize();
|
69
66
|
}
|
70
67
|
|
71
|
-
|
68
|
+
zstds_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
72
69
|
if (destination_buffer == NULL) {
|
73
70
|
ZSTD_freeCCtx(ctx);
|
74
71
|
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -185,9 +182,9 @@ VALUE zstds_ext_compressor_read_result(VALUE self)
|
|
185
182
|
GET_COMPRESSOR(self);
|
186
183
|
DO_NOT_USE_AFTER_CLOSE(compressor_ptr);
|
187
184
|
|
188
|
-
|
189
|
-
size_t
|
190
|
-
size_t
|
185
|
+
zstds_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
186
|
+
size_t destination_buffer_length = compressor_ptr->destination_buffer_length;
|
187
|
+
size_t remaining_destination_buffer_length = compressor_ptr->remaining_destination_buffer_length;
|
191
188
|
|
192
189
|
const char* result = (const char*)destination_buffer;
|
193
190
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
@@ -212,7 +209,7 @@ VALUE zstds_ext_compressor_close(VALUE self)
|
|
212
209
|
compressor_ptr->ctx = NULL;
|
213
210
|
}
|
214
211
|
|
215
|
-
|
212
|
+
zstds_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
216
213
|
if (destination_buffer != NULL) {
|
217
214
|
free(destination_buffer);
|
218
215
|
|
@@ -4,18 +4,17 @@
|
|
4
4
|
#if !defined(ZSTDS_EXT_STREAM_COMPRESSOR_H)
|
5
5
|
#define ZSTDS_EXT_STREAM_COMPRESSOR_H
|
6
6
|
|
7
|
-
#include <stdint.h>
|
8
|
-
#include <stdlib.h>
|
9
7
|
#include <zstd.h>
|
10
8
|
|
11
9
|
#include "ruby.h"
|
10
|
+
#include "zstds_ext/common.h"
|
12
11
|
|
13
12
|
typedef struct {
|
14
|
-
ZSTD_CCtx*
|
15
|
-
|
16
|
-
size_t
|
17
|
-
|
18
|
-
size_t
|
13
|
+
ZSTD_CCtx* ctx;
|
14
|
+
zstds_ext_byte_t* destination_buffer;
|
15
|
+
size_t destination_buffer_length;
|
16
|
+
zstds_ext_byte_t* remaining_destination_buffer;
|
17
|
+
size_t remaining_destination_buffer_length;
|
19
18
|
} zstds_ext_compressor_t;
|
20
19
|
|
21
20
|
VALUE zstds_ext_allocate_compressor(VALUE klass);
|
@@ -3,12 +3,9 @@
|
|
3
3
|
|
4
4
|
#include "zstds_ext/stream/decompressor.h"
|
5
5
|
|
6
|
-
#include <stdint.h>
|
7
|
-
#include <stdlib.h>
|
8
6
|
#include <zstd.h>
|
9
7
|
|
10
8
|
#include "ruby.h"
|
11
|
-
#include "zstds_ext/common.h"
|
12
9
|
#include "zstds_ext/error.h"
|
13
10
|
#include "zstds_ext/option.h"
|
14
11
|
|
@@ -19,7 +16,7 @@ static void free_decompressor(zstds_ext_decompressor_t* decompressor_ptr)
|
|
19
16
|
ZSTD_freeDCtx(ctx);
|
20
17
|
}
|
21
18
|
|
22
|
-
|
19
|
+
zstds_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
23
20
|
if (destination_buffer != NULL) {
|
24
21
|
free(destination_buffer);
|
25
22
|
}
|
@@ -68,7 +65,7 @@ VALUE zstds_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
68
65
|
destination_buffer_length = ZSTD_DStreamOutSize();
|
69
66
|
}
|
70
67
|
|
71
|
-
|
68
|
+
zstds_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
72
69
|
if (destination_buffer == NULL) {
|
73
70
|
ZSTD_freeDCtx(ctx);
|
74
71
|
zstds_ext_raise_error(ZSTDS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -129,9 +126,9 @@ VALUE zstds_ext_decompressor_read_result(VALUE self)
|
|
129
126
|
GET_DECOMPRESSOR(self);
|
130
127
|
DO_NOT_USE_AFTER_CLOSE(decompressor_ptr);
|
131
128
|
|
132
|
-
|
133
|
-
size_t
|
134
|
-
size_t
|
129
|
+
zstds_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
130
|
+
size_t destination_buffer_length = decompressor_ptr->destination_buffer_length;
|
131
|
+
size_t remaining_destination_buffer_length = decompressor_ptr->remaining_destination_buffer_length;
|
135
132
|
|
136
133
|
const char* result = (const char*)destination_buffer;
|
137
134
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
@@ -156,7 +153,7 @@ VALUE zstds_ext_decompressor_close(VALUE self)
|
|
156
153
|
decompressor_ptr->ctx = NULL;
|
157
154
|
}
|
158
155
|
|
159
|
-
|
156
|
+
zstds_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
160
157
|
if (destination_buffer != NULL) {
|
161
158
|
free(destination_buffer);
|
162
159
|
|
@@ -4,18 +4,17 @@
|
|
4
4
|
#if !defined(ZSTDS_EXT_STREAM_DECOMPRESSOR_H)
|
5
5
|
#define ZSTDS_EXT_STREAM_DECOMPRESSOR_H
|
6
6
|
|
7
|
-
#include <stdint.h>
|
8
|
-
#include <stdlib.h>
|
9
7
|
#include <zstd.h>
|
10
8
|
|
11
9
|
#include "ruby.h"
|
10
|
+
#include "zstds_ext/common.h"
|
12
11
|
|
13
12
|
typedef struct {
|
14
|
-
ZSTD_DCtx*
|
15
|
-
|
16
|
-
size_t
|
17
|
-
|
18
|
-
size_t
|
13
|
+
ZSTD_DCtx* ctx;
|
14
|
+
zstds_ext_byte_t* destination_buffer;
|
15
|
+
size_t destination_buffer_length;
|
16
|
+
zstds_ext_byte_t* remaining_destination_buffer;
|
17
|
+
size_t remaining_destination_buffer_length;
|
19
18
|
} zstds_ext_decompressor_t;
|
20
19
|
|
21
20
|
VALUE zstds_ext_allocate_decompressor(VALUE klass);
|
data/ext/zstds_ext/string.c
CHANGED
@@ -3,13 +3,10 @@
|
|
3
3
|
|
4
4
|
#include "zstds_ext/string.h"
|
5
5
|
|
6
|
-
#include <stdint.h>
|
7
|
-
#include <stdlib.h>
|
8
6
|
#include <zstd.h>
|
9
7
|
|
10
8
|
#include "ruby.h"
|
11
9
|
#include "zstds_ext/buffer.h"
|
12
|
-
#include "zstds_ext/common.h"
|
13
10
|
#include "zstds_ext/error.h"
|
14
11
|
#include "zstds_ext/macro.h"
|
15
12
|
#include "zstds_ext/option.h"
|
@@ -65,7 +62,7 @@ static inline zstds_ext_result_t compress(
|
|
65
62
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
66
63
|
|
67
64
|
while (true) {
|
68
|
-
out_buffer.dst = (
|
65
|
+
out_buffer.dst = (zstds_ext_byte_t*)RSTRING_PTR(destination_value) + destination_length;
|
69
66
|
out_buffer.size = remaining_destination_buffer_length;
|
70
67
|
out_buffer.pos = 0;
|
71
68
|
|
@@ -166,7 +163,7 @@ static inline zstds_ext_result_t decompress(
|
|
166
163
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
167
164
|
|
168
165
|
while (true) {
|
169
|
-
out_buffer.dst = (
|
166
|
+
out_buffer.dst = (zstds_ext_byte_t*)RSTRING_PTR(destination_value) + destination_length;
|
170
167
|
out_buffer.size = remaining_destination_buffer_length;
|
171
168
|
out_buffer.pos = 0;
|
172
169
|
|
data/lib/zstds/file.rb
CHANGED
@@ -22,6 +22,8 @@ module ZSTDS
|
|
22
22
|
open_files(source, destination) do |source_io, destination_io|
|
23
23
|
ZSTDS._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 ZSTDS
|
|
33
35
|
open_files(source, destination) do |source_io, destination_io|
|
34
36
|
ZSTDS._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/zstds/option.rb
CHANGED
@@ -166,9 +166,8 @@ module ZSTDS
|
|
166
166
|
end
|
167
167
|
|
168
168
|
dictionary = options[:dictionary]
|
169
|
-
|
170
|
-
|
171
|
-
end
|
169
|
+
raise ValidateError, "invalid dictionary" unless
|
170
|
+
dictionary.nil? || dictionary.is_a?(Dictionary)
|
172
171
|
|
173
172
|
options
|
174
173
|
end
|
@@ -17,23 +17,19 @@ module ZSTDS
|
|
17
17
|
|
18
18
|
include Delegates
|
19
19
|
|
20
|
-
attr_reader :io
|
21
|
-
|
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
|
21
|
+
|
26
22
|
alias tell pos
|
27
23
|
|
28
|
-
def initialize(io,
|
24
|
+
def initialize(io, options = {})
|
29
25
|
@raw_stream = create_raw_stream
|
30
26
|
|
31
27
|
Validation.validate_io io
|
32
28
|
@io = io
|
33
29
|
|
34
|
-
@stat = Stat.new @io.stat
|
30
|
+
@stat = Stat.new @io.stat if @io.respond_to? :stat
|
35
31
|
|
36
|
-
set_encoding external_encoding, internal_encoding, transcode_options
|
32
|
+
set_encoding options[:external_encoding], options[:internal_encoding], options[:transcode_options]
|
37
33
|
reset_buffer
|
38
34
|
reset_io_advise
|
39
35
|
|
@@ -50,8 +46,8 @@ module ZSTDS
|
|
50
46
|
|
51
47
|
protected def reset_io_advise
|
52
48
|
# Both compressor and decompressor need sequential io access.
|
53
|
-
@io.advise :sequential
|
54
|
-
rescue ::Errno::ESPIPE
|
49
|
+
@io.advise :sequential if @io.respond_to? :advise
|
50
|
+
rescue ::Errno::ESPIPE
|
55
51
|
# ok
|
56
52
|
end
|
57
53
|
|
@@ -126,7 +122,8 @@ module ZSTDS
|
|
126
122
|
def rewind
|
127
123
|
@raw_stream = create_raw_stream
|
128
124
|
|
129
|
-
@io.rewind
|
125
|
+
@io.rewind if @io.respond_to? :rewind
|
126
|
+
|
130
127
|
reset_buffer
|
131
128
|
reset_io_advise
|
132
129
|
|
@@ -19,11 +19,13 @@ module ZSTDS
|
|
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 ZSTDS
|
|
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,13 +39,15 @@ module ZSTDS
|
|
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
|
|
46
46
|
unless bytes_written == source.bytesize
|
47
|
+
# :nocov:
|
47
48
|
# Compressor write should eat all provided "source" without remainder.
|
48
49
|
raise UnexpectedError, "unexpected error"
|
50
|
+
# :nocov:
|
49
51
|
end
|
50
52
|
|
51
53
|
break
|
@@ -63,7 +65,7 @@ module ZSTDS
|
|
63
65
|
need_more_destination = @native_stream.flush
|
64
66
|
|
65
67
|
if need_more_destination
|
66
|
-
|
68
|
+
more_destination(&writer)
|
67
69
|
next
|
68
70
|
end
|
69
71
|
|
@@ -71,8 +73,6 @@ module ZSTDS
|
|
71
73
|
end
|
72
74
|
|
73
75
|
super
|
74
|
-
|
75
|
-
nil
|
76
76
|
end
|
77
77
|
|
78
78
|
def close(&writer)
|
@@ -84,7 +84,7 @@ module ZSTDS
|
|
84
84
|
need_more_destination = @native_stream.finish
|
85
85
|
|
86
86
|
if need_more_destination
|
87
|
-
|
87
|
+
more_destination(&writer)
|
88
88
|
next
|
89
89
|
end
|
90
90
|
|
@@ -92,8 +92,6 @@ module ZSTDS
|
|
92
92
|
end
|
93
93
|
|
94
94
|
super
|
95
|
-
|
96
|
-
nil
|
97
95
|
end
|
98
96
|
end
|
99
97
|
end
|
@@ -34,7 +34,7 @@ module ZSTDS
|
|
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 ZSTDS
|
|
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 ZSTDS
|
|
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/zstds/stream/reader.rb
CHANGED
@@ -20,79 +20,60 @@ module ZSTDS
|
|
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_from_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_from_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_from_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 ZSTDS
|
|
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,18 +153,20 @@ module ZSTDS
|
|
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)
|
150
|
-
data = data.encode @internal_encoding,
|
169
|
+
data = data.encode @internal_encoding, **@transcode_options unless @internal_encoding.nil?
|
151
170
|
data
|
152
171
|
end
|
153
172
|
|