Skip to content

Commit c3658c5

Browse files
committed
add docs
1 parent e34b797 commit c3658c5

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TranscodingStreams.jl - IO streams with transcoding
77
[![codecov.io][codecov-img]][codecov-url]
88

99
TranscodingStreams.jl exports a type `TranscodingStream{C<:Codec,S<:IO}<:IO`
10-
which wraps `S<:IO` using `C<:Codec`. Codecs are exported from other packages
10+
which wraps `S<:IO` using `C<:Codec`. Codecs are defined in other packages
1111
listed below:
1212

1313
- [CodecZlib.jl](https://github.com/bicycle1885/CodecZlib.jl)
@@ -24,10 +24,10 @@ listed below:
2424
- `Bzip2Compression` (`Bzip2CompressionStream`)
2525
- `Bzip2Decompression` (`Bzip2DecompressionStream`)
2626

27-
The following snippet is an example of using CodecZlib.jl, which exports
28-
`GzipDecompressionStream` as an alias of
27+
By convention, codec types have names that match `.*(De)?Compression` and I/O
28+
types have names with `Stream` suffix. The following snippet is an example of
29+
using CodecZlib.jl, which exports `GzipDecompressionStream` as an alias of
2930
`TranscodingStream{GzipDecompression,S}` where `S<:IO`:
30-
3131
```julia
3232
# Read lines from a gzip-compressed file.
3333
using CodecZlib
@@ -36,17 +36,34 @@ for line in eachline(stream)
3636
# do something...
3737
end
3838
close(stream)
39+
```
3940

41+
When writing data, the end of a data stream is indicated by calling `close`,
42+
which may write an epilogue if necessary and flush all buffered data to the
43+
underlying I/O stream. If you want to explicitly specify the end position of a
44+
stream for some reason, you can write `TranscodingStreams.TOKEN_END` to the
45+
transcoding stream as follows:
46+
```julia
4047
# Compress a string using zstd.
4148
using CodecZstd
4249
using TranscodingStreams
4350
buf = IOBuffer()
4451
stream = ZstdCompressionStream(buf)
4552
write(stream, "foobarbaz"^100, TranscodingStreams.TOKEN_END)
53+
flush(stream)
4654
compressed = take!(buf)
4755
close(stream)
4856
```
4957

58+
TranscodingStreams.jl extends the `transcode` function to transcode a data
59+
in one shot. `transcode` takes a codec object as its first argument and a data
60+
vector as its second argument:
61+
```julia
62+
using CodecZlib
63+
decompressed = transcode(ZlibDecompression(), b"x\x9cKL*JLNLI\x04R\x00\x19\xf2\x04U")
64+
String(decompressed)
65+
```
66+
5067
[travisci-img]: https://travis-ci.org/bicycle1885/TranscodingStreams.jl.svg?branch=master
5168
[travisci-url]: https://travis-ci.org/bicycle1885/TranscodingStreams.jl
5269
[codecov-img]: http://codecov.io/github/bicycle1885/TranscodingStreams.jl/coverage.svg?branch=master

src/stream.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ end
278278
transcode(codec::Codec, data::Vector{UInt8})::Vector{UInt8}
279279
280280
Transcode `data` by applying `codec`.
281+
282+
Examples
283+
--------
284+
285+
```jldoctest
286+
julia> using CodecZlib
287+
288+
julia> data = Vector{UInt8}("abracadabra");
289+
290+
julia> compressed = transcode(ZlibCompression(), data);
291+
292+
julia> decompressed = transcode(ZlibDecompression(), compressed);
293+
294+
julia> String(decompressed)
295+
"abracadabra"
296+
297+
```
281298
"""
282299
function Base.transcode(codec::Codec, data::Vector{UInt8})
283300
buffer2 = Buffer(length(data))

0 commit comments

Comments
 (0)