Skip to content

Commit 8d7c3f7

Browse files
committed
move Identity to CodecIdentity
1 parent ace32c3 commit 8d7c3f7

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

docs/src/index.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,33 @@ compressed = take!(buf)
185185
close(stream)
186186
```
187187

188+
### Use an identity (no-op) codec
189+
190+
Sometimes, the `Identity` codec, which does nothing, may be useful. The
191+
following example creates a decompression stream based on the extension of a
192+
filepath:
193+
```julia
194+
using CodecZlib
195+
using CodecBzip2
196+
using TranscodingStreams
197+
using TranscodingStreams.CodecIdentity
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 = Identity()
206+
end
207+
return TranscodingStream(codec, open(filepath))
208+
end
209+
210+
makestream("data.txt.gz")
211+
makestream("data.txt.bz2")
212+
makestream("data.txt")
213+
```
214+
188215
### Transcode data in one shot
189216

190217
TranscodingStreams.jl extends the `transcode` function to transcode a data

src/identity.jl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
# Identity Codec
22
# ==============
33

4-
struct Identity <: Codec end
4+
module CodecIdentity
55

6-
function process(::Identity, input::Memory, output::Memory)
6+
export
7+
Identity,
8+
IdentityStream
9+
10+
import TranscodingStreams:
11+
TranscodingStreams,
12+
TranscodingStream,
13+
Memory
14+
15+
"""
16+
Identity()
17+
18+
Create an identity (no-op) codec.
19+
"""
20+
struct Identity <: TranscodingStreams.Codec end
21+
22+
const IdentityStream{S} = TranscodingStream{Identity,S}
23+
24+
"""
25+
IdentityStream(stream::IO)
26+
27+
Create an identity (no-op) codec.
28+
"""
29+
function IdentityStream(stream::IO)
30+
return TranscodingStream(Identity(), stream)
31+
end
32+
33+
function TranscodingStreams.process(::Identity, input::Memory, output::Memory)
734
n = Int(min(input.size, output.size))
835
unsafe_copy!(output.ptr, input.ptr, n)
936
return n, n, ifelse(input.size == 0, :end, :ok)
1037
end
38+
39+
end

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using TranscodingStreams
2+
using TranscodingStreams.CodecIdentity
23
using Base.Test
34

45
@testset "Identity Codec" begin
5-
Identity = TranscodingStreams.Identity
6-
76
source = IOBuffer("")
87
stream = TranscodingStream(Identity(), source)
98
@test eof(stream)
@@ -84,6 +83,9 @@ using Base.Test
8483
@test transcode(Identity(), b"") == b""
8584
@test transcode(Identity(), b"foo") == b"foo"
8685
TranscodingStreams.test_roundtrip_transcode(Identity, Identity)
86+
87+
TranscodingStreams.test_roundtrip_read(IdentityStream, IdentityStream)
88+
TranscodingStreams.test_roundtrip_write(IdentityStream, IdentityStream)
8789
end
8890

8991
installed = keys(Pkg.installed())

0 commit comments

Comments
 (0)