forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iobuffer.jl
324 lines (294 loc) · 10.5 KB
/
iobuffer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
## work with Vector{UInt8} via I/O primitives ##
# Stateful string
type IOBuffer <: IO
data::Vector{UInt8}
readable::Bool
writable::Bool
seekable::Bool # if not seekable, free to destroy (compact) past read data
append::Bool # add data at end instead of at pointer
size::Int
maxsize::Int # pre-allocated, fixed array size
ptr::Int # read (and maybe write) pointer
mark::Int
IOBuffer(data::Vector{UInt8},readable::Bool,writable::Bool,seekable::Bool,append::Bool,maxsize::Int) =
new(data,readable,writable,seekable,append,length(data),maxsize,1,-1)
end
function copy(b::IOBuffer)
ret = IOBuffer(b.writable?copy(b.data):b.data,
b.readable,b.writable,b.seekable,b.append,b.maxsize)
ret.size = b.size
ret.ptr = b.ptr
ret
end
show(io::IO, b::IOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
"readable=", b.readable, ", ",
"writable=", b.writable, ", ",
"seekable=", b.seekable, ", ",
"append=", b.append, ", ",
"size=", b.size, ", ",
"maxsize=", b.maxsize == typemax(Int) ? "Inf" : b.maxsize, ", ",
"ptr=", b.ptr, ", ",
"mark=", b.mark, ")")
# PipeBuffers behave like Unix Pipes. They are readable and writable, the act appendable, and not seekable.
PipeBuffer(data::Vector{UInt8},maxsize::Int) = IOBuffer(data,true,true,false,true,maxsize)
PipeBuffer(data::Vector{UInt8}) = PipeBuffer(data,typemax(Int))
PipeBuffer() = PipeBuffer(UInt8[])
PipeBuffer(maxsize::Int) = (x = PipeBuffer(Array(UInt8,maxsize),maxsize); x.size=0; x)
# IOBuffers behave like Files. They are readable and writable. They are seekable. (They can be appendable).
IOBuffer(data::Vector{UInt8},readable::Bool,writable::Bool,maxsize::Int) =
IOBuffer(data,readable,writable,true,false,maxsize)
IOBuffer(data::Vector{UInt8},readable::Bool,writable::Bool) = IOBuffer(data,readable,writable,typemax(Int))
IOBuffer(data::Vector{UInt8}) = IOBuffer(data, true, false)
IOBuffer(str::ByteString) = IOBuffer(str.data, true, false)
IOBuffer(readable::Bool,writable::Bool) = IOBuffer(UInt8[],readable,writable)
IOBuffer() = IOBuffer(UInt8[], true, true)
IOBuffer(maxsize::Int) = (x=IOBuffer(Array(UInt8,maxsize),true,true,maxsize); x.size=0; x)
is_maxsize_unlimited(io::IOBuffer) = (io.maxsize == typemax(Int))
maxsize(io::IOBuffer) = io.maxsize
read!(from::IOBuffer, a::Array) = read_sub(from, a, 1, length(a))
function read_sub{T}(from::IOBuffer, a::Array{T}, offs, nel)
if offs+nel-1 > length(a) || offs < 1 || nel < 0
throw(BoundsError())
end
if !isbits(T)
throw(ArgumentError("read from IOBuffer only supports bits types or arrays of bits types; got "*string(T)))
end
read!(from, pointer(a, offs), nel*sizeof(T))
return a
end
read!(from::IOBuffer, p::Ptr, nb::Integer) = read!(from, p, int(nb))
function read!(from::IOBuffer, p::Ptr, nb::Int)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
avail = nb_available(from)
adv = min(avail,nb)
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt), p, pointer(from.data,from.ptr), adv)
from.ptr += adv
if nb > avail
throw(EOFError())
end
p
end
function read(from::IOBuffer, ::Type{UInt8})
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
if from.ptr > from.size
throw(EOFError())
end
byte = from.data[from.ptr]
from.ptr += 1
return byte
end
function peek(from::IOBuffer)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
if from.ptr > from.size
throw(EOFError())
end
return from.data[from.ptr]
end
read{T}(from::IOBuffer, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, UInt))
isreadable(io::IOBuffer) = io.readable
iswritable(io::IOBuffer) = io.writable
# TODO: IOBuffer is not iterable, so doesn't really have a length.
# This should maybe be sizeof() instead.
#length(io::IOBuffer) = (io.seekable ? io.size : nb_available(io))
nb_available(io::IOBuffer) = io.size - io.ptr + 1
position(io::IOBuffer) = io.ptr-1
function skip(io::IOBuffer, n::Integer)
io.ptr = min(io.ptr + n, io.size+1)
return io
end
function seek(io::IOBuffer, n::Integer)
if !io.seekable
ismarked(io) || throw(ArgumentError("seek failed, IOBuffer is not seekable and is not marked"))
n == io.mark || throw(ArgumentError("seek failed, IOBuffer is not seekable and n != mark"))
end
io.ptr = min(n+1, io.size+1)
return io
end
function seekend(io::IOBuffer)
io.ptr = io.size+1
return io
end
function truncate(io::IOBuffer, n::Integer)
io.writable || throw(ArgumentError("truncate failed, IOBuffer is not writeable"))
io.seekable || throw(ArgumentError("truncate failed, IOBuffer is not seekable"))
n < 0 && throw(ArgumentError("truncate failed, n bytes must be ≥ 0, got $n"))
n > io.maxsize && throw(ArgumentError("truncate failed, $(n) bytes is exceeds IOBuffer maxsize $(io.maxsize)"))
if n > length(io.data)
resize!(io.data, n)
end
io.data[io.size+1:n] = 0
io.size = n
io.ptr = min(io.ptr, n+1)
ismarked(io) && io.mark > n && unmark(io)
return io
end
function compact(io::IOBuffer)
io.writable || throw(ArgumentError("compact failed, IOBuffer is not writeable"))
io.seekable && throw(ArgumentError("compact failed, IOBuffer is seekable"))
local ptr::Int, bytes_to_move::Int
if ismarked(io) && io.mark < io.ptr
if io.mark == 0 return end
ptr = io.mark
bytes_to_move = nb_available(io) + (io.ptr-io.mark)
else
ptr = io.ptr
bytes_to_move = nb_available(io)
end
ccall(:memmove, Ptr{Void}, (Ptr{Void},Ptr{Void},UInt),
io.data, pointer(io.data,ptr), bytes_to_move)
io.size -= ptr - 1
io.ptr -= ptr - 1
io.mark -= ptr - 1
return io
end
function ensureroom(io::IOBuffer, nshort::Int)
io.writable || throw(ArgumentError("ensureroom failed, IOBuffer is not writeable"))
if !io.seekable
nshort >= 0 || throw(ArgumentError("ensureroom failed, requested number of bytes must be ≥ 0, got $nshort"))
if !ismarked(io) && io.ptr > 1 && io.size <= io.ptr - 1
io.ptr = 1
io.size = 0
else
datastart = ismarked(io) ? io.mark : io.ptr
if (io.size+nshort > io.maxsize) ||
(datastart > 4096 && datastart > io.size - io.ptr) ||
(datastart > 262144)
# apply somewhat arbitrary heuristics to decide when to destroy
# old, read data to make more room for new data
compact(io)
end
end
end
n = min(nshort + (io.append ? io.size : io.ptr-1), io.maxsize)
if n > length(io.data)
resize!(io.data, n)
end
return io
end
eof(io::IOBuffer) = (io.ptr-1 == io.size)
function close(io::IOBuffer)
if io.writable
resize!(io.data, 0)
else
io.data = UInt8[]
end
io.readable = false
io.writable = false
io.seekable = false
io.size = 0
io.maxsize = 0
io.ptr = 1
io.mark = -1
nothing
end
isopen(io::IOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0
function bytestring(io::IOBuffer)
io.readable || throw(ArgumentError("bytestring read failed, IOBuffer is not readable"))
io.seekable || throw(ArgumentError("bytestring read failed, IOBuffer is not seekable"))
bytestring(pointer(io.data), io.size)
end
function takebuf_array(io::IOBuffer)
ismarked(io) && unmark(io)
if io.seekable
data = io.data
if io.writable
maxsize = (io.maxsize == typemax(Int) ? 0 : io.maxsize)
io.data = Array(UInt8,maxsize)
else
data = copy(data)
end
resize!(data,io.size)
else
nbytes = nb_available(io)
a = Array(UInt8, nbytes)
data = read!(io, a)
end
if io.writable
io.ptr = 1
io.size = 0
end
data
end
takebuf_string(io::IOBuffer) = bytestring(takebuf_array(io))
function write(to::IOBuffer, from::IOBuffer)
write(to, pointer(from.data,from.ptr), nb_available(from))
from.ptr += nb_available(from)
end
write(to::IOBuffer, p::Ptr, nb::Integer) = write(to, p, int(nb))
function write(to::IOBuffer, p::Ptr, nb::Int)
to.writable || throw(ArgumentError("write failed, IOBuffer is not writeable"))
ensureroom(to, nb)
ptr = (to.append ? to.size+1 : to.ptr)
nb = min(nb, length(to.data) - ptr + 1)
ccall(:memcpy, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt), pointer(to.data,ptr), p, nb)
to.size = max(to.size, ptr - 1 + nb)
if !to.append to.ptr += nb end
nb
end
function write_sub{T}(to::IOBuffer, a::Array{T}, offs, nel)
if offs+nel-1 > length(a) || offs < 1 || nel < 0
throw(BoundsError())
end
if isbits(T)
write(to, pointer(a,offs), nel*sizeof(T))
else
nb = 0
for i = offs:offs+nel-1
nb += write(to, a[i])
end
nb
end
end
write(to::IOBuffer, a::Array) = write_sub(to, a, 1, length(a))
function write(to::IOBuffer, a::UInt8)
to.writable || throw(ArgumentError("write failed, IOBuffer is not writeable"))
ensureroom(to, 1)
ptr = (to.append ? to.size+1 : to.ptr)
if ptr > to.maxsize
return 0
else
to.data[ptr] = a
end
to.size = max(to.size, ptr)
if !to.append to.ptr += 1 end
sizeof(UInt8)
end
write(to::IOBuffer, p::Ptr) = write(to, convert(UInt, p))
function readbytes!(io::IOBuffer, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(io))
if length(b) < nr
resize!(b, nr)
end
read_sub(io, b, 1, nr)
return nr
end
readbytes(io::IOBuffer) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::IOBuffer, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))
function search(buf::IOBuffer, delim)
p = pointer(buf.data, buf.ptr)
q = ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,nb_available(buf))
nb = (q == C_NULL ? 0 : q-p+1)
end
function readuntil(io::IOBuffer, delim::UInt8)
lb = 70
A = Array(UInt8, lb)
n = 0
data = io.data
for i = io.ptr : io.size
n += 1
if n > lb
lb = n*2
resize!(A, lb)
end
@inbounds b = data[i]
@inbounds A[n] = b
if b == delim
break
end
end
io.ptr += n
if lb != n
resize!(A, n)
end
A
end