io-stream 0.8.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f73e60fdaf4001c88fcfaa4ccb29bfc8ee90d2fc3ffc81408c8673b414038ae
4
- data.tar.gz: 933183cda89e7d555239ba15a752dadccc938628d6197675b3bedb8cfeb60f43
3
+ metadata.gz: 108295b1d28ce05e7c9e9e3f7b3d26427f564a85527a9f23a24ad5b47a261dea
4
+ data.tar.gz: 2aa1c2f6d600d38667e5030122aecd1faa97f81be44695edf6776dda541d5a89
5
5
  SHA512:
6
- metadata.gz: 732318c46b27af3ecabb0c99361b60de5ae3c2733013261ae3089fbaa0f4f2f542e70ee10c410227262a8d137a59d5f1e36ba9419307e801fefd0cfebb412ce5
7
- data.tar.gz: 5ae80afa0d558011183648ce9d34c02dc52e2b8f0cf24bde53a319d1cba3dd188fa8705a477baa8c7fcd5afeb6ee487087e72c1a75d39c80566ea9c378d4e7b1
6
+ metadata.gz: c6c9fc15b84c0ef6d58e2400aeebaac0a36fd67f656dcf10e2f4a1dde2176434dc8f9464809f2b5c05af2563c8bcb1a74ef1e243b7263348d36d24f00df2af7c
7
+ data.tar.gz: 92c3a90776a6f6308ab7d0fa25d35b7df65021f70cb8aadf01e7363961c818392c2ce3fe861b20b38965acebcca851e7419708ad18b2536ae892496b9ea39ade
checksums.yaml.gz.sig CHANGED
Binary file
@@ -58,9 +58,18 @@ module IO::Stream
58
58
 
59
59
  # Read data from the stream.
60
60
  # @parameter size [Integer | Nil] The number of bytes to read. If nil, read until end of stream.
61
- # @returns [String] The data read from the stream.
62
- def read(size = nil)
63
- return String.new(encoding: Encoding::BINARY) if size == 0
61
+ # @parameter buffer [String | Nil] An optional buffer to fill with data instead of allocating a new string.
62
+ # @returns [String] The data read from the stream, or the provided buffer filled with data.
63
+ def read(size = nil, buffer = nil)
64
+ if size == 0
65
+ if buffer
66
+ buffer.clear
67
+ buffer.force_encoding(Encoding::BINARY)
68
+ return buffer
69
+ else
70
+ return String.new(encoding: Encoding::BINARY)
71
+ end
72
+ end
64
73
 
65
74
  if size
66
75
  until @done or @read_buffer.bytesize >= size
@@ -76,26 +85,37 @@ module IO::Stream
76
85
  end
77
86
  end
78
87
 
79
- return consume_read_buffer(size)
88
+ return consume_read_buffer(size, buffer)
80
89
  end
81
90
 
82
91
  # Read at most `size` bytes from the stream. Will avoid reading from the underlying stream if possible.
83
- def read_partial(size = nil)
84
- return String.new(encoding: Encoding::BINARY) if size == 0
92
+ # @parameter size [Integer | Nil] The number of bytes to read. If nil, read all available data.
93
+ # @parameter buffer [String | Nil] An optional buffer to fill with data instead of allocating a new string.
94
+ # @returns [String] The data read from the stream, or the provided buffer filled with data.
95
+ def read_partial(size = nil, buffer = nil)
96
+ if size == 0
97
+ if buffer
98
+ buffer.clear
99
+ buffer.force_encoding(Encoding::BINARY)
100
+ return buffer
101
+ else
102
+ return String.new(encoding: Encoding::BINARY)
103
+ end
104
+ end
85
105
 
86
106
  if !@done and @read_buffer.empty?
87
107
  fill_read_buffer
88
108
  end
89
109
 
90
- return consume_read_buffer(size)
110
+ return consume_read_buffer(size, buffer)
91
111
  end
92
112
 
93
113
  # Read exactly the specified number of bytes.
94
114
  # @parameter size [Integer] The number of bytes to read.
95
115
  # @parameter exception [Class] The exception to raise if not enough data is available.
96
116
  # @returns [String] The data read from the stream.
97
- def read_exactly(size, exception: EOFError)
98
- if buffer = read(size)
117
+ def read_exactly(size, buffer = nil, exception: EOFError)
118
+ if buffer = read(size, buffer)
99
119
  if buffer.bytesize != size
100
120
  raise exception, "Could not read enough data!"
101
121
  end
@@ -107,8 +127,11 @@ module IO::Stream
107
127
  end
108
128
 
109
129
  # This is a compatibility shim for existing code that uses `readpartial`.
110
- def readpartial(size = nil)
111
- read_partial(size) or raise EOFError, "Encountered done while reading data!"
130
+ # @parameter size [Integer | Nil] The number of bytes to read.
131
+ # @parameter buffer [String | Nil] An optional buffer to fill with data instead of allocating a new string.
132
+ # @returns [String] The data read from the stream.
133
+ def readpartial(size = nil, buffer = nil)
134
+ read_partial(size, buffer) or raise EOFError, "Encountered done while reading data!"
112
135
  end
113
136
 
114
137
  # Find the index of a pattern in the read buffer, reading more data if needed.
@@ -330,25 +353,43 @@ module IO::Stream
330
353
 
331
354
  # Consumes at most `size` bytes from the buffer.
332
355
  # @parameter size [Integer | Nil] The amount of data to consume. If nil, consume entire buffer.
333
- def consume_read_buffer(size = nil)
356
+ # @parameter buffer [String | Nil] An optional buffer to fill with data instead of allocating a new string.
357
+ # @returns [String | Nil] The consumed data, or nil if no data available.
358
+ def consume_read_buffer(size = nil, buffer = nil)
334
359
  # If we are at done, and the read buffer is empty, we can't consume anything.
335
- return nil if @done && @read_buffer.empty?
360
+ if @done && @read_buffer.empty?
361
+ # Clear the buffer even when returning nil
362
+ if buffer
363
+ buffer.clear
364
+ buffer.force_encoding(Encoding::BINARY)
365
+ end
366
+ return nil
367
+ end
336
368
 
337
369
  result = nil
338
370
 
339
371
  if size.nil? or size >= @read_buffer.bytesize
340
372
  # Consume the entire read buffer:
341
- result = @read_buffer
373
+ if buffer
374
+ buffer.clear
375
+ buffer << @read_buffer
376
+ result = buffer
377
+ else
378
+ result = @read_buffer
379
+ end
342
380
  @read_buffer = StringBuffer.new
343
381
  else
344
- # This approach uses more memory.
345
- # result = @read_buffer.slice!(0, size)
346
-
347
382
  # We know that we are not going to reuse the original buffer.
348
383
  # But byteslice will generate a hidden copy. So let's freeze it first:
349
384
  @read_buffer.freeze
350
385
 
351
- result = @read_buffer.byteslice(0, size)
386
+ if buffer
387
+ # Use replace instead of clear + << for better performance
388
+ buffer.replace(@read_buffer.byteslice(0, size))
389
+ result = buffer
390
+ else
391
+ result = @read_buffer.byteslice(0, size)
392
+ end
352
393
  @read_buffer = @read_buffer.byteslice(size, @read_buffer.bytesize)
353
394
  end
354
395
 
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2023-2024, by Samuel Williams.
5
5
 
6
6
  module IO::Stream
7
- VERSION = "0.8.0"
7
+ VERSION = "0.9.0"
8
8
  end
data/readme.md CHANGED
@@ -12,6 +12,10 @@ Please see the [project documentation](https://socketry.github.io/io-stream) for
12
12
 
13
13
  Please see the [project releases](https://socketry.github.io/io-streamreleases/index) for all releases.
14
14
 
15
+ ### v0.9.0
16
+
17
+ - Add support for `buffer` parameter in `read`, `read_exactly`, and `read_partial` methods to allow reading into a provided buffer.
18
+
15
19
  ### v0.8.0
16
20
 
17
21
  - On Ruby v3.3+, use `IO#write` directly instead of `IO#write_nonblock`, for better performance.
@@ -58,10 +62,6 @@ Please see the [project releases](https://socketry.github.io/io-streamreleases/i
58
62
 
59
63
  - Add convenient `IO.Stream()` constructor method for creating buffered streams.
60
64
 
61
- ### v0.3.0
62
-
63
- - Add support for timeouts with compatibility shims for various IO types.
64
-
65
65
  ## See Also
66
66
 
67
67
  - [async-io](https://github.com/socketry/async-io) — Where this implementation originally came from.
data/releases.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Releases
2
2
 
3
+ ## v0.9.0
4
+
5
+ - Add support for `buffer` parameter in `read`, `read_exactly`, and `read_partial` methods to allow reading into a provided buffer.
6
+
3
7
  ## v0.8.0
4
8
 
5
9
  - On Ruby v3.3+, use `IO#write` directly instead of `IO#write_nonblock`, for better performance.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file