Skip to content

Commit c929a97

Browse files
committed
Add some sugar in the generic stream reader API
We added a parameter to bypass the underlying stream read limit when reading directly int he buffer from a generic stream. This is simply sugar to set underlyingStreamReadSizeLimit to nil before the read and back to its value after the read.
1 parent 89b33e4 commit c929a97

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/StreamReader/Implementations/GenericStreamReader.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public final class GenericStreamReader : StreamReader {
127127
returns the number of bytes read.
128128

129129
The `readSizeLimit` and `underlyingStreamReadSizeLimit` variables are
130-
respected when using this method.
130+
respected when using this method, unless instructed otherwise in the
131+
parameters.
131132

132133
- Important: If the buffer is big enough, might read _more_ bytes than asked.
133134
Can also read less (if the end of the stream is reached, or if only one read
@@ -137,8 +138,15 @@ public final class GenericStreamReader : StreamReader {
137138
from the stream.
138139
- Parameter allowMoreThanOneRead: If `true`, the method will read from the
139140
stream until the asked size is read or the end of stream is reached.
141+
- Parameter bypassUnderlyingStreamReadSizeLimit: A simple convenience to set
142+
`underlyingStreamReadSizeLimit` to `nil` before reading from the stream and
143+
set it back to whatever its value was after the read.
140144
- Returns: The number of bytes acutally read from the stream. */
141-
public func readStreamInBuffer(size: Int, allowMoreThanOneRead: Bool = false) throws -> Int {
145+
public func readStreamInBuffer(size: Int, allowMoreThanOneRead: Bool = false, bypassUnderlyingStreamReadSizeLimit: Bool = false) throws -> Int {
146+
let previousUnderlyingStreamReadSizeLimit = underlyingStreamReadSizeLimit
147+
if bypassUnderlyingStreamReadSizeLimit {underlyingStreamReadSizeLimit = nil}
148+
defer {underlyingStreamReadSizeLimit = previousUnderlyingStreamReadSizeLimit}
149+
142150
let previousBufferValidLenth = bufferValidLength
143151
_ = try readDataNoCurrentPosIncrement(size: (bufferValidLength + size), readContraints: allowMoreThanOneRead ? .readUntilSizeOrStreamEnd : .readFromStreamMaxOnce)
144152
let ret = (bufferValidLength - previousBufferValidLenth)

0 commit comments

Comments
 (0)