3
3
# # work with AbstractVector{UInt8} via I/O primitives ##
4
4
5
5
# Stateful string
6
- type IOBuffer {T<: AbstractVector{UInt8} } <: IO
6
+ type AbstractIOBuffer {T<: AbstractVector{UInt8} } <: IO
7
7
data:: T # T should support: getindex, setindex!, length, copy!, resize!, and T()
8
8
readable:: Bool
9
9
writable:: Bool
@@ -14,22 +14,35 @@ type IOBuffer{T<:AbstractVector{UInt8}} <: IO
14
14
ptr:: Int # read (and maybe write) pointer
15
15
mark:: Int # reset mark location for ptr (or <0 for no mark)
16
16
17
- IOBuffer (data:: T ,readable:: Bool ,writable:: Bool ,seekable:: Bool ,append:: Bool ,maxsize:: Int ) =
17
+ AbstractIOBuffer (data:: T ,readable:: Bool ,writable:: Bool ,seekable:: Bool ,append:: Bool ,maxsize:: Int ) =
18
18
new (data,readable,writable,seekable,append,length (data),maxsize,1 ,- 1 )
19
19
end
20
- typealias SimpleIOBuffer IOBuffer{Vector{UInt8}}
21
- IOBuffer {T<:AbstractVector{UInt8}} (data:: T , readable:: Bool , writable:: Bool , seekable:: Bool , append:: Bool , maxsize:: Int ) =
22
- IOBuffer {T} (data, readable, writable, seekable, append, maxsize)
20
+ typealias IOBuffer AbstractIOBuffer{Vector{UInt8}}
23
21
24
- function copy (b:: IOBuffer )
22
+ AbstractIOBuffer {T<:AbstractVector{UInt8}} (data:: T , readable:: Bool , writable:: Bool , seekable:: Bool , append:: Bool , maxsize:: Int ) =
23
+ AbstractIOBuffer {T} (data, readable, writable, seekable, append, maxsize)
24
+
25
+ # IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).
26
+ IOBuffer (data:: AbstractVector{UInt8} , readable:: Bool = true , writable:: Bool = false , maxsize:: Int = typemax (Int)) =
27
+ AbstractIOBuffer (data, readable, writable, true , false , maxsize)
28
+ IOBuffer (readable:: Bool , writable:: Bool ) = IOBuffer (UInt8[], readable, writable)
29
+ IOBuffer () = IOBuffer (true , true )
30
+ IOBuffer (maxsize:: Int ) = (x= IOBuffer (Array (UInt8,maxsize), true , true , maxsize); x. size= 0 ; x)
31
+
32
+ # PipeBuffers behave like Unix Pipes. They are typically readable and writable, they act appendable, and are not seekable.
33
+ PipeBuffer (data:: Vector{UInt8} = UInt8[], maxsize:: Int = typemax (Int)) =
34
+ AbstractIOBuffer (data,true ,true ,false ,true ,maxsize)
35
+ PipeBuffer (maxsize:: Int ) = (x = PipeBuffer (Array (UInt8,maxsize),maxsize); x. size= 0 ; x)
36
+
37
+ function copy (b:: AbstractIOBuffer )
25
38
ret = typeof (b)(b. writable ? copy (b. data) : b. data,
26
39
b. readable, b. writable, b. seekable, b. append, b. maxsize)
27
40
ret. size = b. size
28
41
ret. ptr = b. ptr
29
42
ret
30
43
end
31
44
32
- show (io:: IO , b:: IOBuffer ) = print (io, " IOBuffer(data=UInt8[...], " ,
45
+ show (io:: IO , b:: AbstractIOBuffer ) = print (io, " IOBuffer(data=UInt8[...], " ,
33
46
" readable=" , b. readable, " , " ,
34
47
" writable=" , b. writable, " , " ,
35
48
" seekable=" , b. seekable, " , " ,
@@ -39,23 +52,12 @@ show(io::IO, b::IOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
39
52
" ptr=" , b. ptr, " , " ,
40
53
" mark=" , b. mark, " )" )
41
54
42
- # PipeBuffers behave like Unix Pipes. They are typically readable and writable, the act appendable, and not seekable.
43
- PipeBuffer (data:: Vector{UInt8} = UInt8[], maxsize:: Int = typemax (Int)) = IOBuffer (data,true ,true ,false ,true ,maxsize)
44
- PipeBuffer (maxsize:: Int ) = (x = PipeBuffer (Array (UInt8,maxsize),maxsize); x. size= 0 ; x)
55
+ is_maxsize_unlimited (io:: AbstractIOBuffer ) = (io. maxsize == typemax (Int))
56
+ maxsize (io:: AbstractIOBuffer ) = io. maxsize
45
57
46
- # IOBuffers behave like Files. They are typically readable and writable. They are seekable. (They can be appendable).
47
- IOBuffer (data:: AbstractVector{UInt8} , readable:: Bool = true , writable:: Bool = false , maxsize:: Int = typemax (Int)) =
48
- IOBuffer (data, readable, writable, true , false , maxsize)
49
- IOBuffer (readable:: Bool , writable:: Bool ) = IOBuffer (UInt8[], readable, writable)
50
- IOBuffer () = IOBuffer (true , true )
51
- IOBuffer (maxsize:: Int ) = (x= IOBuffer (Array (UInt8,maxsize), true , true , maxsize); x. size= 0 ; x)
52
-
53
- is_maxsize_unlimited (io:: IOBuffer ) = (io. maxsize == typemax (Int))
54
- maxsize (io:: IOBuffer ) = io. maxsize
58
+ read! (from:: AbstractIOBuffer , a:: Array ) = read_sub (from, a, 1 , length (a))
55
59
56
- read! (from:: IOBuffer , a:: Array ) = read_sub (from, a, 1 , length (a))
57
-
58
- function read_sub {T} (from:: IOBuffer , a:: AbstractArray{T} , offs, nel)
60
+ function read_sub {T} (from:: AbstractIOBuffer , a:: AbstractArray{T} , offs, nel)
59
61
from. readable || throw (ArgumentError (" read failed, IOBuffer is not readable" ))
60
62
if offs+ nel- 1 > length (a) || offs < 1 || nel < 0
61
63
throw (BoundsError ())
@@ -81,7 +83,7 @@ function read_sub{T}(from::IOBuffer, a::AbstractArray{T}, offs, nel)
81
83
return a
82
84
end
83
85
84
- function read (from:: IOBuffer , :: Type{UInt8} )
86
+ function read (from:: AbstractIOBuffer , :: Type{UInt8} )
85
87
from. readable || throw (ArgumentError (" read failed, IOBuffer is not readable" ))
86
88
if from. ptr > from. size
87
89
throw (EOFError ())
@@ -91,51 +93,51 @@ function read(from::IOBuffer, ::Type{UInt8})
91
93
return byte
92
94
end
93
95
94
- function peek (from:: IOBuffer )
96
+ function peek (from:: AbstractIOBuffer )
95
97
from. readable || throw (ArgumentError (" read failed, IOBuffer is not readable" ))
96
98
if from. ptr > from. size
97
99
throw (EOFError ())
98
100
end
99
101
return from. data[from. ptr]
100
102
end
101
103
102
- read {T} (from:: IOBuffer , :: Type{Ptr{T}} ) = convert (Ptr{T}, read (from, UInt))
104
+ read {T} (from:: AbstractIOBuffer , :: Type{Ptr{T}} ) = convert (Ptr{T}, read (from, UInt))
103
105
104
- isreadable (io:: IOBuffer ) = io. readable
105
- iswritable (io:: IOBuffer ) = io. writable
106
+ isreadable (io:: AbstractIOBuffer ) = io. readable
107
+ iswritable (io:: AbstractIOBuffer ) = io. writable
106
108
107
- # TODO : IOBuffer is not iterable, so doesn't really have a length.
109
+ # TODO : AbstractIOBuffer is not iterable, so doesn't really have a length.
108
110
# This should maybe be sizeof() instead.
109
- # length(io::IOBuffer ) = (io.seekable ? io.size : nb_available(io))
110
- nb_available (io:: IOBuffer ) = io. size - io. ptr + 1
111
- position (io:: IOBuffer ) = io. ptr- 1
111
+ # length(io::AbstractIOBuffer ) = (io.seekable ? io.size : nb_available(io))
112
+ nb_available (io:: AbstractIOBuffer ) = io. size - io. ptr + 1
113
+ position (io:: AbstractIOBuffer ) = io. ptr- 1
112
114
113
- function skip (io:: IOBuffer , n:: Integer )
115
+ function skip (io:: AbstractIOBuffer , n:: Integer )
114
116
seekto = io. ptr + n
115
117
n < 0 && return seek (io, seekto- 1 ) # Does error checking
116
118
io. ptr = min (seekto, io. size+ 1 )
117
119
return io
118
120
end
119
121
120
- function seek (io:: IOBuffer , n:: Integer )
122
+ function seek (io:: AbstractIOBuffer , n:: Integer )
121
123
if ! io. seekable
122
124
ismarked (io) || throw (ArgumentError (" seek failed, IOBuffer is not seekable and is not marked" ))
123
125
n == io. mark || throw (ArgumentError (" seek failed, IOBuffer is not seekable and n != mark" ))
124
126
end
125
127
# TODO : REPL.jl relies on the fact that this does not throw (by seeking past the beginning or end
126
- # of an IOBuffer ), so that would need to be fixed in order to throw an error here
128
+ # of an AbstractIOBuffer ), so that would need to be fixed in order to throw an error here
127
129
# (n < 0 || n > io.size) && throw(ArgumentError("Attempted to seek outside IOBuffer boundaries."))
128
130
# io.ptr = n+1
129
131
io. ptr = max (min (n+ 1 , io. size+ 1 ), 1 )
130
132
return io
131
133
end
132
134
133
- function seekend (io:: IOBuffer )
135
+ function seekend (io:: AbstractIOBuffer )
134
136
io. ptr = io. size+ 1
135
137
return io
136
138
end
137
139
138
- function truncate (io:: IOBuffer , n:: Integer )
140
+ function truncate (io:: AbstractIOBuffer , n:: Integer )
139
141
io. writable || throw (ArgumentError (" truncate failed, IOBuffer is not writeable" ))
140
142
io. seekable || throw (ArgumentError (" truncate failed, IOBuffer is not seekable" ))
141
143
n < 0 && throw (ArgumentError (" truncate failed, n bytes must be ≥ 0, got $n " ))
@@ -150,7 +152,7 @@ function truncate(io::IOBuffer, n::Integer)
150
152
return io
151
153
end
152
154
153
- function compact (io:: IOBuffer )
155
+ function compact (io:: AbstractIOBuffer )
154
156
io. writable || throw (ArgumentError (" compact failed, IOBuffer is not writeable" ))
155
157
io. seekable && throw (ArgumentError (" compact failed, IOBuffer is seekable" ))
156
158
local ptr:: Int , bytes_to_move:: Int
@@ -169,7 +171,7 @@ function compact(io::IOBuffer)
169
171
return io
170
172
end
171
173
172
- function ensureroom (io:: IOBuffer , nshort:: Int )
174
+ function ensureroom (io:: AbstractIOBuffer , nshort:: Int )
173
175
io. writable || throw (ArgumentError (" ensureroom failed, IOBuffer is not writeable" ))
174
176
if ! io. seekable
175
177
nshort >= 0 || throw (ArgumentError (" ensureroom failed, requested number of bytes must be ≥ 0, got $nshort " ))
@@ -194,9 +196,9 @@ function ensureroom(io::IOBuffer, nshort::Int)
194
196
return io
195
197
end
196
198
197
- eof (io:: IOBuffer ) = (io. ptr- 1 == io. size)
199
+ eof (io:: AbstractIOBuffer ) = (io. ptr- 1 == io. size)
198
200
199
- function close {T} (io:: IOBuffer {T} )
201
+ function close {T} (io:: AbstractIOBuffer {T} )
200
202
io. readable = false
201
203
io. writable = false
202
204
io. seekable = false
@@ -212,16 +214,16 @@ function close{T}(io::IOBuffer{T})
212
214
nothing
213
215
end
214
216
215
- isopen (io:: IOBuffer ) = io. readable || io. writable || io. seekable || nb_available (io) > 0
217
+ isopen (io:: AbstractIOBuffer ) = io. readable || io. writable || io. seekable || nb_available (io) > 0
216
218
217
- function bytestring (io:: IOBuffer )
219
+ function bytestring (io:: AbstractIOBuffer )
218
220
io. readable || throw (ArgumentError (" bytestring read failed, IOBuffer is not readable" ))
219
221
io. seekable || throw (ArgumentError (" bytestring read failed, IOBuffer is not seekable" ))
220
222
b = copy! (Array (UInt8, io. size), 1 , io. data, 1 , io. size)
221
223
return isvalid (ASCIIString, b) ? ASCIIString (b) : UTF8String (b)
222
224
end
223
225
224
- function takebuf_array (io:: IOBuffer )
226
+ function takebuf_array (io:: AbstractIOBuffer )
225
227
ismarked (io) && unmark (io)
226
228
if io. seekable
227
229
nbytes = io. size
@@ -236,7 +238,7 @@ function takebuf_array(io::IOBuffer)
236
238
end
237
239
data
238
240
end
239
- function takebuf_array (io:: SimpleIOBuffer )
241
+ function takebuf_array (io:: IOBuffer )
240
242
ismarked (io) && unmark (io)
241
243
if io. seekable
242
244
data = io. data
@@ -258,12 +260,12 @@ function takebuf_array(io::SimpleIOBuffer)
258
260
end
259
261
data
260
262
end
261
- function takebuf_string (io:: IOBuffer )
263
+ function takebuf_string (io:: AbstractIOBuffer )
262
264
b = takebuf_array (io)
263
265
return isvalid (ASCIIString, b) ? ASCIIString (b) : UTF8String (b)
264
266
end
265
267
266
- function write (to:: IOBuffer , from:: IOBuffer )
268
+ function write (to:: AbstractIOBuffer , from:: AbstractIOBuffer )
267
269
if to === from
268
270
from. ptr = from. size + 1
269
271
return 0
@@ -273,8 +275,8 @@ function write(to::IOBuffer, from::IOBuffer)
273
275
written
274
276
end
275
277
276
- write (to:: IOBuffer , p:: Ptr , nb:: Integer ) = write (to, p, Int (nb))
277
- function write (to:: IOBuffer , p:: Ptr , nb:: Int )
278
+ write (to:: AbstractIOBuffer , p:: Ptr , nb:: Integer ) = write (to, p, Int (nb))
279
+ function write (to:: AbstractIOBuffer , p:: Ptr , nb:: Int )
278
280
ensureroom (to, nb)
279
281
ptr = (to. append ? to. size+ 1 : to. ptr)
280
282
written = min (nb, length (to. data) - ptr + 1 )
@@ -287,7 +289,7 @@ function write(to::IOBuffer, p::Ptr, nb::Int)
287
289
written
288
290
end
289
291
290
- function write_sub {T} (to:: IOBuffer , a:: AbstractArray{T} , offs, nel)
292
+ function write_sub {T} (to:: AbstractIOBuffer , a:: AbstractArray{T} , offs, nel)
291
293
if offs+ nel- 1 > length (a) || offs < 1 || nel < 0
292
294
throw (BoundsError ())
293
295
end
@@ -313,9 +315,9 @@ function write_sub{T}(to::IOBuffer, a::AbstractArray{T}, offs, nel)
313
315
written
314
316
end
315
317
316
- write (to:: IOBuffer , a:: Array ) = write_sub (to, a, 1 , length (a))
318
+ write (to:: AbstractIOBuffer , a:: Array ) = write_sub (to, a, 1 , length (a))
317
319
318
- function write (to:: IOBuffer , a:: UInt8 )
320
+ function write (to:: AbstractIOBuffer , a:: UInt8 )
319
321
ensureroom (to, 1 )
320
322
ptr = (to. append ? to. size+ 1 : to. ptr)
321
323
if ptr > to. maxsize
@@ -328,26 +330,26 @@ function write(to::IOBuffer, a::UInt8)
328
330
sizeof (UInt8)
329
331
end
330
332
331
- write (to:: IOBuffer , p:: Ptr ) = write (to, convert (UInt, p))
333
+ write (to:: AbstractIOBuffer , p:: Ptr ) = write (to, convert (UInt, p))
332
334
333
- function readbytes! (io:: IOBuffer , b:: Array{UInt8} , nb= length (b))
335
+ function readbytes! (io:: AbstractIOBuffer , b:: Array{UInt8} , nb= length (b))
334
336
nr = min (nb, nb_available (io))
335
337
if length (b) < nr
336
338
resize! (b, nr)
337
339
end
338
340
read_sub (io, b, 1 , nr)
339
341
return nr
340
342
end
341
- readbytes (io:: IOBuffer ) = read! (io, Array (UInt8, nb_available (io)))
342
- readbytes (io:: IOBuffer , nb) = read! (io, Array (UInt8, min (nb, nb_available (io))))
343
+ readbytes (io:: AbstractIOBuffer ) = read! (io, Array (UInt8, nb_available (io)))
344
+ readbytes (io:: AbstractIOBuffer , nb) = read! (io, Array (UInt8, min (nb, nb_available (io))))
343
345
344
- function search (buf:: SimpleIOBuffer , delim:: UInt8 )
346
+ function search (buf:: IOBuffer , delim:: UInt8 )
345
347
p = pointer (buf. data, buf. ptr)
346
348
q = ccall (:memchr ,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,nb_available (buf))
347
349
nb = (q == C_NULL ? 0 : q- p+ 1 )
348
350
end
349
351
350
- function search (buf:: IOBuffer , delim:: UInt8 )
352
+ function search (buf:: AbstractIOBuffer , delim:: UInt8 )
351
353
data = buf. data
352
354
for i = buf. ptr : buf. size
353
355
@inbounds b = data[i]
@@ -358,7 +360,7 @@ function search(buf::IOBuffer, delim::UInt8)
358
360
return 0
359
361
end
360
362
361
- function readuntil (io:: IOBuffer , delim:: UInt8 )
363
+ function readuntil (io:: AbstractIOBuffer , delim:: UInt8 )
362
364
lb = 70
363
365
A = Array (UInt8, lb)
364
366
n = 0
0 commit comments