Skip to content

Commit 6bad5d3

Browse files
committed
add TOKEN_END
1 parent 486c073 commit 6bad5d3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ The following snippet is an example of using CodecZlib.jl, which exports
2626
`TranscodingStream{GzipDecompression,S}` where `S<:IO`:
2727

2828
```julia
29+
# Read lines from a gzip-compressed file.
2930
using CodecZlib
3031
stream = GzipDecompressionStream(open("data.gzip"))
3132
for line in eachline(stream)
3233
# do something...
3334
end
3435
close(stream)
36+
37+
# Compress a string using zstd.
38+
using CodecZstd
39+
using TranscodingStreams
40+
buf = IOBuffer()
41+
stream = ZstdCompressionStream(buf)
42+
write(stream, "foobarbaz"^100, TranscodingStreams.TOKEN_END)
43+
compressed = take!(buf)
44+
close(stream)
3545
```
3646

3747
[travisci-img]: https://travis-ci.org/bicycle1885/TranscodingStreams.jl.svg?branch=master

src/stream.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ function Base.flush(stream::TranscodingStream)
239239
flush(stream.stream)
240240
end
241241

242+
# A singleton type of end token.
243+
struct EndToken end
244+
245+
"""
246+
A special token indicating the end of data.
247+
248+
`TOKEN_END` may be written to a transcoding stream like `write(stream,
249+
TOKEN_END)`, which will terminate the current transcoding block.
250+
"""
251+
const TOKEN_END = EndToken()
252+
253+
function Base.write(stream::TranscodingStream, ::EndToken)
254+
changestate!(stream, :write)
255+
processall(stream)
256+
return 0
257+
end
258+
242259

243260
# Buffering
244261
# ---------

0 commit comments

Comments
 (0)