Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 96 additions & 44 deletions src/transcode.jl
Comment thread
baumgold marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# =========

"""
transcode(::Type{C}, data::Vector{UInt8})::Vector{UInt8} where C<:Codec
transcode(::Type{C}, data::ByteData)::ByteData where {C<:Codec}
Comment thread
baumgold marked this conversation as resolved.
Outdated

Transcode `data` by applying a codec `C()`.

Expand All @@ -27,7 +27,7 @@ julia> String(decompressed)

```
"""
function Base.transcode(::Type{C}, data::ByteData) where C<:Codec
function Base.transcode(::Type{C}, data::ByteData) where {C<:Codec}
Comment thread
baumgold marked this conversation as resolved.
Outdated
codec = C()
initialize(codec)
try
Expand All @@ -37,48 +37,11 @@ function Base.transcode(::Type{C}, data::ByteData) where C<:Codec
end
end

"""
transcode(codec::Codec, data::Vector{UInt8})::Vector{UInt8}

Transcode `data` by applying `codec`.

Note that this method does not initialize or finalize `codec`. This is
efficient when you transcode a number of pieces of data, but you need to call
[`TranscodingStreams.initialize`](@ref) and
[`TranscodingStreams.finalize`](@ref) explicitly.

Examples
--------

```julia
julia> using CodecZlib

julia> data = b"abracadabra";

julia> codec = ZlibCompressor();

julia> TranscodingStreams.initialize(codec)

julia> compressed = transcode(codec, data);

julia> TranscodingStreams.finalize(codec)

julia> codec = ZlibDecompressor();

julia> TranscodingStreams.initialize(codec)

julia> decompressed = transcode(codec, compressed);

julia> TranscodingStreams.finalize(codec)

julia> String(decompressed)
"abracadabra"

```
"""
function Base.transcode(codec::Codec, data::ByteData)
input = Buffer(data)
output = Buffer(initial_output_size(codec, buffermem(input)))
function Base.transcode(
Comment thread
baumgold marked this conversation as resolved.
codec::Codec,
input::Buffer,
output::Buffer = Buffer(initial_output_size(codec, buffermem(input))),
)
Comment thread
baumgold marked this conversation as resolved.
Comment thread
baumgold marked this conversation as resolved.
error = Error()
code = startproc(codec, :write, error)
if code === :error
Expand Down Expand Up @@ -121,6 +84,95 @@ function Base.transcode(codec::Codec, data::ByteData)
throw(error[])
end

"""
Comment thread
baumgold marked this conversation as resolved.
transcode(codec::Codec, data::ByteData)::ByteData

Transcode `data` by applying `codec`.

Note that this method does not initialize or finalize `codec`. This is
efficient when you transcode a number of pieces of data, but you need to call
[`TranscodingStreams.initialize`](@ref) and
[`TranscodingStreams.finalize`](@ref) explicitly.

Examples
--------

```julia
julia> using CodecZlib

julia> data = b"abracadabra";

julia> codec = ZlibCompressor();

julia> TranscodingStreams.initialize(codec)

julia> compressed = transcode(codec, data);

julia> TranscodingStreams.finalize(codec)

julia> codec = ZlibDecompressor();

julia> TranscodingStreams.initialize(codec)

julia> decompressed = transcode(codec, compressed);

julia> TranscodingStreams.finalize(codec)

julia> String(decompressed)
"abracadabra"

```
"""
Base.transcode(codec::Codec, data::ByteData) = transcode(codec, Buffer(data))

Base.transcode(codec::Codec, data::ByteData, output::Buffer) =
Comment thread
baumgold marked this conversation as resolved.
Outdated
transcode(codec, Buffer(data), output)
Comment thread
baumgold marked this conversation as resolved.
Outdated

"""
transcode(codec::Codec, data::ByteData, output::ByteData)::ByteData
Comment thread
baumgold marked this conversation as resolved.
Outdated

Transcode `data` by applying `codec` and store the results in `output`.

Note that this method does not initialize or finalize `codec`. This is
efficient when you transcode a number of pieces of data, but you need to call
[`TranscodingStreams.initialize`](@ref) and
[`TranscodingStreams.finalize`](@ref) explicitly.

Examples
--------

```julia
julia> using CodecZlib

julia> data julia> data = b"abracadabra";
= b"abracadabra";

julia> codec = ZlibCompressor();

julia> TranscodingStreams.initialize(codec)

julia> compressed = Vector{UInt8}()

julia> transcode(codec, data, compressed);

julia> TranscodingStreams.finalize(codec)

julia> codec = ZlibDecompressor();

julia> TranscodingStreams.initialize(codec)

julia> decompressed = transcode(codec, compressed);

julia> TranscodingStreams.finalize(codec)

julia> String(decompressed)
"abracadabra"

```
"""
Base.transcode(codec::Codec, data::ByteData, output::ByteData) =
Comment thread
baumgold marked this conversation as resolved.
Outdated
transcode(codec, data, Buffer(output))

# Return the initial output buffer size.
function initial_output_size(codec::Codec, input::Memory)
return max(
Expand Down