@@ -78,7 +78,7 @@ mutable struct GenericIOBuffer{T<:AbstractVector{UInt8}} <: IO
78
78
# which can be seeked back using `reset`, even for non-seekable buffers.
79
79
# For non-seekable buffers that can be compacted, data before the mark can be
80
80
# destroyed.
81
- # This value is always in -1 : size- offset-1
81
+ # This value is always in -1 : size - offset
82
82
mark:: Int
83
83
84
84
# Unsafe constructor which does not do any checking
@@ -128,7 +128,15 @@ function GenericIOBuffer(data::Vector{UInt8}, readable::Bool, writable::Bool, se
128
128
mem = ref. mem
129
129
len = length (data)
130
130
offset = memoryrefoffset (ref) - 1
131
- buf = GenericIOBuffer (mem, readable, writable, seekable, append, maxsize)
131
+
132
+ # The user may pass a vector of length <= maxsize, but where the underlying memory
133
+ # is larger than maxsize. Don't throw an error in that case.
134
+ mz = Int (maxsize):: Int
135
+ len = Int (length (data)):: Int
136
+ if mz < len
137
+ throw (ArgumentError (" maxsize must not be smaller than data length" ))
138
+ end
139
+ buf = GenericIOBuffer {Memory{UInt8}} (unsafe_method, mem, readable, writable, seekable, append, max (mz, length (mem)))
132
140
buf. size = len + offset
133
141
buf. ptr = offset + 1
134
142
buf. offset = offset
@@ -239,12 +247,8 @@ function IOBuffer(;
239
247
flags = open_flags (read= read, write= write, append= append, truncate= truncate)
240
248
# A common usecase of IOBuffer is to incrementally construct strings. By using StringMemory
241
249
# as the default storage, we can turn the result into a string without copying.
242
- # TODO : Do we need to zero this here?
243
- data = fill! (StringMemory (size), 0 )
244
- buf = GenericIOBuffer {Memory{UInt8}} (unsafe_method, data, flags. read, flags. write, true , flags. append, mz)
245
- if flags. truncate
246
- buf. size = buf. offset
247
- end
250
+ buf = GenericIOBuffer {Memory{UInt8}} (unsafe_method, StringMemory (size), flags. read, flags. write, true , flags. append, mz)
251
+ buf. size = 0
248
252
return buf
249
253
end
250
254
@@ -382,17 +386,6 @@ function read(from::GenericIOBuffer, T::MultiByteBitNumberType)
382
386
return x
383
387
end
384
388
385
- function read_sub (from:: GenericIOBuffer , a:: MutableDenseArrayType{T} , offs, nel) where T
386
- require_one_based_indexing (a)
387
- from. readable || _throw_not_readable ()
388
- if offs+ nel- 1 > length (a) || offs < 1 || nel < 0
389
- throw (BoundsError ())
390
- end
391
- nb = UInt (nel * sizeof (T))
392
- GC. @preserve a unsafe_read (from, pointer (a, offs), nb)
393
- return a
394
- end
395
-
396
389
@inline function read (from:: GenericIOBuffer , :: Type{UInt8} )
397
390
from. readable || _throw_not_readable ()
398
391
ptr = from. ptr
@@ -496,7 +489,7 @@ function _resize!(io::GenericIOBuffer, new_size::Int)
496
489
end
497
490
498
491
# TODO : These errors cannot be converted to LazyString, but it's wasteful to interpolate them here.
499
- function truncate (io:: GenericIOBuffer , n:: Integer )
492
+ function truncate (io:: GenericIOBuffer , n:: Integer )
500
493
io. writable || throw (ArgumentError (" truncate failed, IOBuffer is not writeable" ))
501
494
# Non-seekable buffers can only be constructed with `PipeBuffer`, which is explicitly
502
495
# documented to not be truncatable.
@@ -812,13 +805,16 @@ end
812
805
end
813
806
814
807
readbytes! (io:: GenericIOBuffer , b:: MutableDenseArrayType{UInt8} , nb= length (b)) = readbytes! (io, b, Int (nb))
808
+
815
809
function readbytes! (io:: GenericIOBuffer , b:: MutableDenseArrayType{UInt8} , nb:: Int )
816
- nr = min (nb, bytesavailable (io))
817
- if length (b) < nr
818
- resize! (b, nr)
810
+ io. readable || _throw_not_readable ()
811
+ to_read = min (nb, bytesavailable (io))
812
+ if length (b) < to_read
813
+ resize! (b, to_read)
819
814
end
820
- read_sub (io, b, 1 , nr)
821
- return nr
815
+ checkbounds (b, 1 : to_read)
816
+ GC. @preserve b unsafe_read (io, pointer (b), to_read)
817
+ to_read
822
818
end
823
819
read (io:: GenericIOBuffer ) = read! (io, StringVector (bytesavailable (io)))
824
820
0 commit comments