/
buffered.cr
212 lines (172 loc) · 4.65 KB
/
buffered.cr
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
# The `IO::Buffered` mixin enhances an `IO` with input/output buffering.
#
# The buffering behaviour can be turned on/off with the `#sync=` and
# `#read_buffering=` methods.
#
# Additionally, several methods, like `#gets`, are implemented in a more
# efficient way.
module IO::Buffered
BUFFER_SIZE = 8192
@in_buffer_rem = Bytes.empty
@out_count = 0
@sync = false
@read_buffering = true
# Reads at most *slice.size* bytes from the wrapped `IO` into *slice*.
# Returns the number of bytes read.
abstract def unbuffered_read(slice : Bytes)
# Writes at most *slice.size* bytes from *slice* into the wrapped `IO`.
# Returns the number of bytes written.
abstract def unbuffered_write(slice : Bytes)
# Flushes the wrapped `IO`.
abstract def unbuffered_flush
# Closes the wrapped `IO`.
abstract def unbuffered_close
# Rewinds the wrapped `IO`.
abstract def unbuffered_rewind
# :nodoc:
def read_byte : UInt8?
check_open
fill_buffer if read_buffering? && @in_buffer_rem.empty?
if @in_buffer_rem.empty?
return nil if read_buffering?
byte = uninitialized UInt8
if read(Slice.new(pointerof(byte), 1)) == 1
byte
else
nil
end
else
b = @in_buffer_rem[0]
@in_buffer_rem += 1
b
end
end
# Buffered implementation of `IO#read(slice)`.
def read(slice : Bytes)
check_open
count = slice.size
return 0 if count == 0
if @in_buffer_rem.empty?
# If we are asked to read more than half the buffer's size,
# read directly into the slice, as it's not worth the extra
# memory copy.
if !read_buffering? || count >= BUFFER_SIZE // 2
return unbuffered_read(slice[0, count]).to_i
else
fill_buffer
return 0 if @in_buffer_rem.empty?
end
end
to_read = Math.min(count, @in_buffer_rem.size)
slice.copy_from(@in_buffer_rem.to_unsafe, to_read)
@in_buffer_rem += to_read
to_read
end
# Returns the bytes hold in the read buffer.
#
# This method only performs a read to return
# peek data if the current buffer is empty:
# otherwise no read is performed and whatever
# is in the buffer is returned.
def peek : Bytes?
check_open
if @in_buffer_rem.empty?
fill_buffer
if @in_buffer_rem.empty?
return Bytes.empty # EOF
end
end
@in_buffer_rem
end
# :nodoc:
def skip(bytes_count) : Nil
check_open
if bytes_count <= @in_buffer_rem.size
@in_buffer_rem += bytes_count
return
end
bytes_count -= @in_buffer_rem.size
@in_buffer_rem = Bytes.empty
super(bytes_count)
end
# Buffered implementation of `IO#write(slice)`.
def write(slice : Bytes)
check_open
return if slice.empty?
count = slice.size
if sync?
return unbuffered_write(slice)
end
if count >= BUFFER_SIZE
flush
return unbuffered_write slice[0, count]
end
if count > BUFFER_SIZE - @out_count
flush
end
slice.copy_to(out_buffer + @out_count, count)
@out_count += count
nil
end
# :nodoc:
def write_byte(byte : UInt8)
check_open
if sync?
return super
end
if @out_count >= BUFFER_SIZE
flush
end
out_buffer[@out_count] = byte
@out_count += 1
end
# Turns on/off `IO` **write** buffering. When *sync* is set to `true`, no buffering
# will be done (that is, writing to this `IO` is immediately synced to the
# underlying `IO`).
def sync=(sync)
flush if sync && !@sync
@sync = !!sync
end
# Determines if this `IO` does write buffering. If `true`, no buffering is done.
def sync?
@sync
end
# Turns on/off `IO` **read** buffering.
def read_buffering=(read_buffering)
@read_buffering = !!read_buffering
end
# Determines whether this `IO` buffers reads.
def read_buffering?
@read_buffering
end
# Flushes any buffered data and the underlying `IO`. Returns `self`.
def flush
unbuffered_write(Slice.new(out_buffer, @out_count)) if @out_count > 0
unbuffered_flush
@out_count = 0
self
end
# Flushes and closes the underlying `IO`.
def close : Nil
flush if @out_count > 0
ensure
unbuffered_close
end
# Rewinds the underlying `IO`. Returns `self`.
def rewind
unbuffered_rewind
@in_buffer_rem = Bytes.empty
self
end
private def fill_buffer
in_buffer = in_buffer()
size = unbuffered_read(Slice.new(in_buffer, BUFFER_SIZE)).to_i
@in_buffer_rem = Slice.new(in_buffer, size)
end
private def in_buffer
@in_buffer ||= GC.malloc_atomic(BUFFER_SIZE.to_u32).as(UInt8*)
end
private def out_buffer
@out_buffer ||= GC.malloc_atomic(BUFFER_SIZE.to_u32).as(UInt8*)
end
end