Skip to content

Commit b745f95

Browse files
authored
implement new protocol (#6)
1 parent c453134 commit b745f95

File tree

14 files changed

+382
-231
lines changed

14 files changed

+382
-231
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
julia 0.6.0-rc1
1+
julia 0.6.0

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ makedocs(
55
format=:html,
66
sitename="TranscodingStreams.jl",
77
modules=[TranscodingStreams],
8+
pages=["index.md", "examples.md", "references.md"],
89
assets=["assets/custom.css"])
910

1011
deploydocs(

docs/src/examples.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
Examples
2+
========
3+
4+
Read lines from a gzip-compressed file
5+
--------------------------------------
6+
7+
The following snippet is an example of using CodecZlib.jl, which exports
8+
`GzipDecompressionStream{S}` as an alias of
9+
`TranscodingStream{GzipDecompression,S} where S<:IO`:
10+
```julia
11+
using CodecZlib
12+
stream = GzipDecompressionStream(open("data.txt.gz"))
13+
for line in eachline(stream)
14+
# do something...
15+
end
16+
close(stream)
17+
```
18+
19+
Note that the last `close` call will close the file as well. Alternatively,
20+
`open(<stream type>, <filepath>) do ... end` syntax will close the file at the
21+
end:
22+
```julia
23+
using CodecZlib
24+
open(GzipDecompressionStream, "data.txt.gz") do stream
25+
for line in eachline(stream)
26+
# do something...
27+
end
28+
end
29+
```
30+
31+
Save a data matrix with Zstd compression
32+
----------------------------------------
33+
34+
Writing compressed data is easy. One thing you need to keep in mind is to call
35+
`close` after writing data; otherwise, the output file will be incomplete:
36+
```julia
37+
using CodecZstd
38+
mat = randn(100, 100)
39+
stream = ZstdCompressionStream(open("data.mat.zst", "w"))
40+
writedlm(stream, mat)
41+
close(stream)
42+
```
43+
44+
Of course, `open(<stream type>, ...) do ... end` works well:
45+
```julia
46+
using CodecZstd
47+
mat = randn(100, 100)
48+
open(ZstdCompressionStream, "data.mat.zst", "w") do stream
49+
writedlm(stream, mat)
50+
end
51+
```
52+
53+
Explicitly finish transcoding by writing `TOKEN_END`
54+
----------------------------------------------------
55+
56+
When writing data, the end of a data stream is indicated by calling `close`,
57+
which may write an epilogue if necessary and flush all buffered data to the
58+
underlying I/O stream. If you want to explicitly specify the end position of a
59+
stream for some reason, you can write `TranscodingStreams.TOKEN_END` to the
60+
transcoding stream as follows:
61+
```julia
62+
using CodecZstd
63+
using TranscodingStreams
64+
buf = IOBuffer()
65+
stream = ZstdCompressionStream(buf)
66+
write(stream, "foobarbaz"^100, TranscodingStreams.TOKEN_END)
67+
flush(stream)
68+
compressed = take!(buf)
69+
close(stream)
70+
```
71+
72+
Use a noop codec
73+
----------------
74+
75+
Sometimes, the `Noop` codec, which does nothing, may be useful. The following
76+
example creates a decompression stream based on the extension of a filepath:
77+
```julia
78+
using CodecZlib
79+
using CodecBzip2
80+
using TranscodingStreams
81+
82+
function makestream(filepath)
83+
if endswith(filepath, ".gz")
84+
codec = GzipDecompression()
85+
elseif endswith(filepath, ".bz2")
86+
codec = Bzip2Decompression()
87+
else
88+
codec = Noop()
89+
end
90+
return TranscodingStream(codec, open(filepath))
91+
end
92+
93+
makestream("data.txt.gz")
94+
makestream("data.txt.bz2")
95+
makestream("data.txt")
96+
```
97+
98+
Transcode data in one shot
99+
--------------------------
100+
101+
TranscodingStreams.jl extends the `transcode` function to transcode a data
102+
in one shot. `transcode` takes a codec object as its first argument and a data
103+
vector as its second argument:
104+
```julia
105+
using CodecZlib
106+
decompressed = transcode(ZlibDecompression(), b"x\x9cKL*JLNLI\x04R\x00\x19\xf2\x04U")
107+
String(decompressed)
108+
```

docs/src/index.md

Lines changed: 20 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
TranscodingStreams.jl
22
=====================
33

4+
Overview
5+
--------
6+
47
TranscodingStreams.jl is a package for transcoding (e.g. compression) data
5-
streams. This package exports a type `TranscodingStream`, which
6-
is a subtype of `IO` and supports various I/O operations like other usual I/O
7-
streams in the standard library.
8+
streams. It exports a type `TranscodingStream`, which is a subtype of `IO` and
9+
supports various I/O operations like other usual I/O streams in the standard
10+
library. Operations are quick, simple, and consistent.
11+
12+
In this page, we intorduce the basic concepts of TranscodingStreams.jl and
13+
available packages. The [Examples](@ref) page demonstrates common usage. The
14+
[References](@ref) page offers a comprehensive API document.
815

916

1017
Introduction
@@ -119,146 +126,14 @@ feasible like these packages. TranscodingStreams.jl requests a codec to
119126
implement some interface functions which will be described later.
120127

121128

122-
Examples
123-
--------
124-
125-
### Read lines from a gzip-compressed file
126-
127-
The following snippet is an example of using CodecZlib.jl, which exports
128-
`GzipDecompressionStream{S}` as an alias of
129-
`TranscodingStream{GzipDecompression,S} where S<:IO`:
130-
```julia
131-
using CodecZlib
132-
stream = GzipDecompressionStream(open("data.txt.gz"))
133-
for line in eachline(stream)
134-
# do something...
135-
end
136-
close(stream)
137-
```
138-
139-
Note that the last `close` call will close the file as well. Alternatively,
140-
`open(<stream type>, <filepath>) do ... end` syntax will close the file at the
141-
end:
142-
```julia
143-
using CodecZlib
144-
open(GzipDecompressionStream, "data.txt.gz") do stream
145-
for line in eachline(stream)
146-
# do something...
147-
end
148-
end
149-
```
150-
151-
### Save a data matrix with Zstd compression
152-
153-
Writing compressed data is easy. One thing you need to keep in mind is to call
154-
`close` after writing data; otherwise, the output file will be incomplete:
155-
```julia
156-
using CodecZstd
157-
mat = randn(100, 100)
158-
stream = ZstdCompressionStream(open("data.mat.zst", "w"))
159-
writedlm(stream, mat)
160-
close(stream)
161-
```
162-
163-
Of course, `open(<stream type>, ...) do ... end` works well:
164-
```julia
165-
using CodecZstd
166-
mat = randn(100, 100)
167-
open(ZstdCompressionStream, "data.mat.zst", "w") do stream
168-
writedlm(stream, mat)
169-
end
170-
```
171-
172-
### Explicitly finish transcoding by writing `TOKEN_END`
173-
174-
When writing data, the end of a data stream is indicated by calling `close`,
175-
which may write an epilogue if necessary and flush all buffered data to the
176-
underlying I/O stream. If you want to explicitly specify the end position of a
177-
stream for some reason, you can write `TranscodingStreams.TOKEN_END` to the
178-
transcoding stream as follows:
179-
```julia
180-
using CodecZstd
181-
using TranscodingStreams
182-
buf = IOBuffer()
183-
stream = ZstdCompressionStream(buf)
184-
write(stream, "foobarbaz"^100, TranscodingStreams.TOKEN_END)
185-
flush(stream)
186-
compressed = take!(buf)
187-
close(stream)
188-
```
189-
190-
### Use a noop codec
191-
192-
Sometimes, the `Noop` codec, which does nothing, may be useful. The following
193-
example creates a decompression stream based on the extension of a filepath:
194-
```julia
195-
using CodecZlib
196-
using CodecBzip2
197-
using TranscodingStreams
198-
199-
function makestream(filepath)
200-
if endswith(filepath, ".gz")
201-
codec = GzipDecompression()
202-
elseif endswith(filepath, ".bz2")
203-
codec = Bzip2Decompression()
204-
else
205-
codec = Noop()
206-
end
207-
return TranscodingStream(codec, open(filepath))
208-
end
129+
Error handling
130+
--------------
209131

210-
makestream("data.txt.gz")
211-
makestream("data.txt.bz2")
212-
makestream("data.txt")
213-
```
214-
215-
### Transcode data in one shot
216-
217-
TranscodingStreams.jl extends the `transcode` function to transcode a data
218-
in one shot. `transcode` takes a codec object as its first argument and a data
219-
vector as its second argument:
220-
```julia
221-
using CodecZlib
222-
decompressed = transcode(ZlibDecompression(), b"x\x9cKL*JLNLI\x04R\x00\x19\xf2\x04U")
223-
String(decompressed)
224-
```
225-
226-
227-
API
228-
---
229-
230-
```@meta
231-
CurrentModule = TranscodingStreams
232-
```
233-
234-
```@docs
235-
TranscodingStream(codec::Codec, stream::IO)
236-
transcode(codec::Codec, data::Vector{UInt8})
237-
TranscodingStreams.TOKEN_END
238-
```
239-
240-
```@docs
241-
TranscodingStreams.Noop
242-
TranscodingStreams.NoopStream
243-
```
244-
245-
```@docs
246-
TranscodingStreams.CodecIdentity.Identity
247-
TranscodingStreams.CodecIdentity.IdentityStream
248-
```
249-
250-
251-
Defining a new codec
252-
--------------------
253-
254-
```@docs
255-
TranscodingStreams.Codec
256-
TranscodingStreams.initialize
257-
TranscodingStreams.finalize
258-
TranscodingStreams.startproc
259-
TranscodingStreams.process
260-
```
261-
262-
```@docs
263-
TranscodingStreams.Memory
264-
```
132+
You may encounter an error while processing data with this package. For example,
133+
your compressed data may be corrupted or truncated and the decompression codec
134+
cannot handle it properly. In this case, the codec informs the stream of the
135+
error and the stream goes to an unrecoverable state. In this state, the only
136+
possible operations are `isopen` and `close`. Other operations, such as `read`
137+
or `write`, will result in an argument error exception. Resources allocated in
138+
the codec will be released by the stream and hence you must not call the
139+
finalizer of a codec that is once passed to a transcoding stream object.

docs/src/references.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
References
2+
==========
3+
4+
```@meta
5+
CurrentModule = TranscodingStreams
6+
```
7+
8+
TranscodingStream
9+
-----------------
10+
11+
```@docs
12+
TranscodingStream(codec::Codec, stream::IO)
13+
transcode(codec::Codec, data::Vector{UInt8})
14+
TranscodingStreams.TOKEN_END
15+
TranscodingStreams.unsafe_read
16+
```
17+
18+
Codec
19+
-----
20+
21+
```@docs
22+
TranscodingStreams.Noop
23+
TranscodingStreams.NoopStream
24+
```
25+
26+
**This type is deprecated**. Use [`Noop`](@ref) instead.
27+
```@docs
28+
TranscodingStreams.CodecIdentity.Identity
29+
TranscodingStreams.CodecIdentity.IdentityStream
30+
```
31+
32+
```@docs
33+
TranscodingStreams.Codec
34+
TranscodingStreams.initialize
35+
TranscodingStreams.finalize
36+
TranscodingStreams.startproc
37+
TranscodingStreams.process
38+
```
39+
40+
Internal types
41+
--------------
42+
43+
```@docs
44+
TranscodingStreams.Memory
45+
TranscodingStreams.Error
46+
TranscodingStreams.State
47+
```

src/TranscodingStreams.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export
99

1010
include("memory.jl")
1111
include("buffer.jl")
12+
include("error.jl")
1213
include("codec.jl")
1314
include("state.jl")
1415
include("stream.jl")

0 commit comments

Comments
 (0)