From f5b64a6a5d9796378c36a14913711e1762afa890 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Wed, 15 Mar 2023 10:35:20 -0400 Subject: [PATCH 01/25] allow users to optionally provide an output buffer when calling transcode --- src/transcode.jl | 140 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 44 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index 6043853d..f62cfe79 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -2,7 +2,7 @@ # ========= """ - transcode(::Type{C}, data::Vector{UInt8})::Vector{UInt8} where C<:Codec + transcode(::Type{C}, data::ByteData)::ByteData where {C<:Codec} Transcode `data` by applying a codec `C()`. @@ -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} codec = C() initialize(codec) try @@ -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( + codec::Codec, + input::Buffer, + output::Buffer = Buffer(initial_output_size(codec, buffermem(input))), +) error = Error() code = startproc(codec, :write, error) if code === :error @@ -121,6 +84,95 @@ function Base.transcode(codec::Codec, data::ByteData) throw(error[]) end +""" + 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) = + transcode(codec, Buffer(data), output) + +""" + transcode(codec::Codec, data::ByteData, output::ByteData)::ByteData + +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) = + transcode(codec, data, Buffer(output)) + # Return the initial output buffer size. function initial_output_size(codec::Codec, input::Memory) return max( From 9e5bb40999ff51de93a03985c11b1c86035c826a Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Thu, 23 Mar 2023 15:58:43 -0400 Subject: [PATCH 02/25] add tests and refactor docstring --- src/noop.jl | 7 +-- src/transcode.jl | 128 ++++++++++++++++------------------------------ test/codecnoop.jl | 17 ++++++ 3 files changed, 66 insertions(+), 86 deletions(-) diff --git a/src/noop.jl b/src/noop.jl index d5b511c7..190814d6 100644 --- a/src/noop.jl +++ b/src/noop.jl @@ -120,10 +120,11 @@ function Base.transcode(::Type{Noop}, data::ByteData) return transcode(Noop(), data) end -function Base.transcode(::Noop, data::ByteData) - # Copy data because the caller may expect the return object is not the same +function Base.transcode(codec::Noop, input::Buffer, output::Buffer = Buffer(Vector{UInt8}())) + # copy data because the caller may expect the return object is not the same # as from the input. - return Vector{UInt8}(data) + copydata!(output, bufferptr(input), length(input)) + return output.data end diff --git a/src/transcode.jl b/src/transcode.jl index f62cfe79..ba6ce043 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -37,6 +37,49 @@ function Base.transcode(::Type{C}, data::ByteData) where {C<:Codec} end end +""" + transcode(codec::Codec, data::Union{ByteData, Buffer}[, output::Union{ByteData, Buffer}])::ByteData + +Transcode `data` by applying `codec`. + +If `output` is unspecified, then this method will allocate it. + +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 = 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" + +``` +""" function Base.transcode( codec::Codec, input::Buffer, @@ -84,95 +127,14 @@ function Base.transcode( throw(error[]) end -""" - 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, output::ByteData) = + transcode(codec, data, Buffer(output)) -``` -""" Base.transcode(codec::Codec, data::ByteData) = transcode(codec, Buffer(data)) Base.transcode(codec::Codec, data::ByteData, output::Buffer) = transcode(codec, Buffer(data), output) -""" - transcode(codec::Codec, data::ByteData, output::ByteData)::ByteData - -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) = - transcode(codec, data, Buffer(output)) - # Return the initial output buffer size. function initial_output_size(codec::Codec, input::Memory) return max( diff --git a/test/codecnoop.jl b/test/codecnoop.jl index b95e9a79..22e6e93c 100644 --- a/test/codecnoop.jl +++ b/test/codecnoop.jl @@ -192,9 +192,26 @@ data = b"" @test transcode(Noop(), data) == data @test transcode(Noop(), data) !== data + @test transcode(Noop(), data, Vector{UInt8}()) == data + @test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) == data + @test transcode(Noop(), data, Vector{UInt8}()) !== data + @test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) !== data + output = Vector{UInt8}() + @test transcode(Noop(), data, output) === output + output = TranscodingStreams.Buffer(Vector{UInt8}()) + @test transcode(Noop(), data, output) === output.data + data = b"foo" @test transcode(Noop(), data) == data @test transcode(Noop(), data) !== data + @test transcode(Noop(), data, Vector{UInt8}()) == data + @test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) == data + @test transcode(Noop(), data, Vector{UInt8}()) !== data + @test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) !== data + output = Vector{UInt8}() + @test transcode(Noop(), data, output) === output + output = TranscodingStreams.Buffer(Vector{UInt8}()) + @test transcode(Noop(), data, output) === output.data TranscodingStreams.test_roundtrip_transcode(Noop, Noop) TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream) From 090cb9c9a27a9fa98fc6291648997972e5976e68 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Sat, 25 Mar 2023 06:52:59 -0400 Subject: [PATCH 03/25] nicer formating Co-authored-by: Mark Kittisopikul --- src/transcode.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index ba6ce043..27a21c4f 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -80,10 +80,17 @@ julia> String(decompressed) ``` """ +_default_output_buffer(codec, input) = Buffer( + initial_output_size( + codec, + buffermem(input) + ) +) + function Base.transcode( codec::Codec, input::Buffer, - output::Buffer = Buffer(initial_output_size(codec, buffermem(input))), + output::Buffer = _default_output_buffer(codec, input) , ) error = Error() code = startproc(codec, :write, error) From b6d5f9df26db543233bdbab37cb44d48df0a4f13 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Sat, 25 Mar 2023 06:59:55 -0400 Subject: [PATCH 04/25] new copydata! method --- src/buffer.jl | 5 +++++ src/noop.jl | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/buffer.jl b/src/buffer.jl index 93700bf0..779e9c12 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -199,6 +199,11 @@ function copydata!(buf::Buffer, data::Ptr{UInt8}, nbytes::Integer) return buf end +# Copy data from `data` to `buf`. +function copydata!(buf::Buffer, data::Buffer, nbytes::Integer = length(data)) + return copydata!(buf, bufferptr(data), nbytes) +end + # Copy data from `buf` to `data`. function copydata!(data::Ptr{UInt8}, buf::Buffer, nbytes::Integer) # NOTE: It's caller's responsibility to ensure that the buffer has at least diff --git a/src/noop.jl b/src/noop.jl index 190814d6..588d85ef 100644 --- a/src/noop.jl +++ b/src/noop.jl @@ -121,9 +121,7 @@ function Base.transcode(::Type{Noop}, data::ByteData) end function Base.transcode(codec::Noop, input::Buffer, output::Buffer = Buffer(Vector{UInt8}())) - # copy data because the caller may expect the return object is not the same - # as from the input. - copydata!(output, bufferptr(input), length(input)) + copydata!(output, input) return output.data end From 2ce1efb10e5ea9164585ac135978f8f464bcbe55 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Wed, 29 Mar 2023 07:17:22 -0400 Subject: [PATCH 05/25] reassociate docstring with correct method --- src/transcode.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index 27a21c4f..a3e36d9e 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -37,6 +37,13 @@ function Base.transcode(::Type{C}, data::ByteData) where {C<:Codec} end end +_default_output_buffer(codec, input) = Buffer( + initial_output_size( + codec, + buffermem(input) + ) +) + """ transcode(codec::Codec, data::Union{ByteData, Buffer}[, output::Union{ByteData, Buffer}])::ByteData @@ -80,13 +87,6 @@ julia> String(decompressed) ``` """ -_default_output_buffer(codec, input) = Buffer( - initial_output_size( - codec, - buffermem(input) - ) -) - function Base.transcode( codec::Codec, input::Buffer, From 49b5b822fe23db2040a75d39765ab20bf01c2302 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:42:49 -0400 Subject: [PATCH 06/25] Expand ByteData Co-authored-by: Mark Kittisopikul --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index a3e36d9e..84608f44 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -2,7 +2,7 @@ # ========= """ - transcode(::Type{C}, data::ByteData)::ByteData where {C<:Codec} + transcode(::Type{C}, data::Union{Vector{UInt8},Base.CodeUnits{UInt8}})::ByteData where {C<:Codec} Transcode `data` by applying a codec `C()`. From 78d2b3bc835422de9689696bbf8208c1d246f6c2 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:42:59 -0400 Subject: [PATCH 07/25] Expand ByteData Co-authored-by: Mark Kittisopikul --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index 84608f44..290cd488 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -27,7 +27,7 @@ julia> String(decompressed) ``` """ -function Base.transcode(::Type{C}, data::ByteData) where {C<:Codec} +function Base.transcode(::Type{C}, data::Union{Vector{UInt8},Base.CodeUnits{UInt8}}) where {C<:Codec} codec = C() initialize(codec) try From 20e82d95b1afbd85cc262afd77a727bd8e6086ba Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:43:13 -0400 Subject: [PATCH 08/25] Expand ByteData Co-authored-by: Mark Kittisopikul --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index 290cd488..4decd6f5 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -45,7 +45,7 @@ _default_output_buffer(codec, input) = Buffer( ) """ - transcode(codec::Codec, data::Union{ByteData, Buffer}[, output::Union{ByteData, Buffer}])::ByteData + transcode(codec::Codec, data::Union{ByteData, Buffer}[, output::Union{Vector{UInt8},Base.CodeUnits{UInt8}, Buffer}])::ByteData Transcode `data` by applying `codec`. From c01098346107d144b14f741266f1818ba0e7baf7 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:46:58 -0400 Subject: [PATCH 09/25] clarify formatting --- src/transcode.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index 4decd6f5..23b97b98 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -2,7 +2,10 @@ # ========= """ - transcode(::Type{C}, data::Union{Vector{UInt8},Base.CodeUnits{UInt8}})::ByteData where {C<:Codec} + transcode( + ::Type{C}, + data::Union{Vector{UInt8},Base.CodeUnits{UInt8}} + )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} where {C<:Codec} Transcode `data` by applying a codec `C()`. @@ -45,7 +48,11 @@ _default_output_buffer(codec, input) = Buffer( ) """ - transcode(codec::Codec, data::Union{ByteData, Buffer}[, output::Union{Vector{UInt8},Base.CodeUnits{UInt8}, Buffer}])::ByteData + transcode( + codec::Codec, + data::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}, + [output::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}] + )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} Transcode `data` by applying `codec`. From 829c655848a6d44392e63d9db425459a83879955 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 09:48:08 -0400 Subject: [PATCH 10/25] formatting --- src/transcode.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index 23b97b98..392ab6af 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -4,7 +4,7 @@ """ transcode( ::Type{C}, - data::Union{Vector{UInt8},Base.CodeUnits{UInt8}} + data::Union{Vector{UInt8},Base.CodeUnits{UInt8}}, )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} where {C<:Codec} Transcode `data` by applying a codec `C()`. @@ -51,7 +51,7 @@ _default_output_buffer(codec, input) = Buffer( transcode( codec::Codec, data::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}, - [output::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}] + [output::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}], )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} Transcode `data` by applying `codec`. From 3d17ac87f908f0dfa48bf10bc049551343dc5a7e Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:23:07 -0400 Subject: [PATCH 11/25] Generic args Co-authored-by: Joao Aparicio --- src/transcode.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index 392ab6af..e606105e 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -30,7 +30,15 @@ julia> String(decompressed) ``` """ -function Base.transcode(::Type{C}, data::Union{Vector{UInt8},Base.CodeUnits{UInt8}}) where {C<:Codec} +function Base.transcode(::Type{C}, args...) + codec = C() + initialize(codec) + try + return transcode(codec, args...) + finally + finalize(codec) + end +end``` codec = C() initialize(codec) try From 1bf636dfd168a5dfedd59d44f5f3635b27930e27 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:23:48 -0400 Subject: [PATCH 12/25] Generic args Co-authored-by: Joao Aparicio --- src/transcode.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index e606105e..4509cffd 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -149,13 +149,11 @@ function Base.transcode( throw(error[]) end -Base.transcode(codec::Codec, data::ByteData, output::ByteData) = +Base.transcode(codec::Codec, data::Buffer, output::ByteData) = transcode(codec, data, Buffer(output)) -Base.transcode(codec::Codec, data::ByteData) = transcode(codec, Buffer(data)) - -Base.transcode(codec::Codec, data::ByteData, output::Buffer) = - transcode(codec, Buffer(data), output) +Base.transcode(codec::Codec, data::ByteData, args...) = + transcode(codec, Buffer(data), args...) # Return the initial output buffer size. function initial_output_size(codec::Codec, input::Memory) From cd753f668b228656ee45bc1083bc360d03af858b Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:34:46 -0400 Subject: [PATCH 13/25] small fixes --- src/buffer.jl | 8 ++++---- src/transcode.jl | 10 +--------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/buffer.jl b/src/buffer.jl index 779e9c12..6c9dadbc 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -26,10 +26,6 @@ mutable struct Buffer # the total number of transcoded bytes transcoded::Int64 - function Buffer(size::Integer) - return new(Vector{UInt8}(undef, size), 0, 1, 1, 0) - end - function Buffer(data::Vector{UInt8}) return new(data, 0, 1, length(data)+1, 0) end @@ -39,6 +35,10 @@ function Buffer(data::Base.CodeUnits{UInt8}) return Buffer(Vector{UInt8}(data)) end +function Buffer(size::Integer = 0) + return Buffer(Vector{UInt8}(undef, size)) +end + function Base.length(buf::Buffer) return length(buf.data) end diff --git a/src/transcode.jl b/src/transcode.jl index 4509cffd..f4e2f1d1 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -30,7 +30,7 @@ julia> String(decompressed) ``` """ -function Base.transcode(::Type{C}, args...) +function Base.transcode(::Type{C}, args...) where {C<:Codec} codec = C() initialize(codec) try @@ -38,14 +38,6 @@ function Base.transcode(::Type{C}, args...) finally finalize(codec) end -end``` - codec = C() - initialize(codec) - try - return transcode(codec, data) - finally - finalize(codec) - end end _default_output_buffer(codec, input) = Buffer( From 0d1e93439c1b6f4bc063136e16566300daf08f65 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:35:38 -0400 Subject: [PATCH 14/25] simplify --- src/noop.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/noop.jl b/src/noop.jl index 588d85ef..2ebc44c2 100644 --- a/src/noop.jl +++ b/src/noop.jl @@ -120,7 +120,7 @@ function Base.transcode(::Type{Noop}, data::ByteData) return transcode(Noop(), data) end -function Base.transcode(codec::Noop, input::Buffer, output::Buffer = Buffer(Vector{UInt8}())) +function Base.transcode(codec::Noop, input::Buffer, output::Buffer = Buffer()) copydata!(output, input) return output.data end From 3b90dd6bf5fdda43bde3c124116fcb4ec9678260 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:43:33 -0400 Subject: [PATCH 15/25] fix buffer --- src/buffer.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/buffer.jl b/src/buffer.jl index 6c9dadbc..1b37aee5 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -26,6 +26,10 @@ mutable struct Buffer # the total number of transcoded bytes transcoded::Int64 + function Buffer(size::Integer = 0) + return Buffer(Vector{UInt8}(undef, size), 0, 1, 1, 0) + end + function Buffer(data::Vector{UInt8}) return new(data, 0, 1, length(data)+1, 0) end @@ -35,10 +39,6 @@ function Buffer(data::Base.CodeUnits{UInt8}) return Buffer(Vector{UInt8}(data)) end -function Buffer(size::Integer = 0) - return Buffer(Vector{UInt8}(undef, size)) -end - function Base.length(buf::Buffer) return length(buf.data) end From 5ec6bf3cfca3c119fb6e44171ddcaaf55e8cde2c Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:43:51 -0400 Subject: [PATCH 16/25] fix --- src/buffer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buffer.jl b/src/buffer.jl index 1b37aee5..8fe703e1 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -27,7 +27,7 @@ mutable struct Buffer transcoded::Int64 function Buffer(size::Integer = 0) - return Buffer(Vector{UInt8}(undef, size), 0, 1, 1, 0) + return new(Vector{UInt8}(undef, size), 0, 1, 1, 0) end function Buffer(data::Vector{UInt8}) From a11043e86f99926ec0f8aa566fc4495cb5567142 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:44:39 -0400 Subject: [PATCH 17/25] formatting --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index f4e2f1d1..4465bcd3 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -97,7 +97,7 @@ julia> String(decompressed) function Base.transcode( codec::Codec, input::Buffer, - output::Buffer = _default_output_buffer(codec, input) , + output::Buffer = _default_output_buffer(codec, input), ) error = Error() code = startproc(codec, :write, error) From e926f4faa7941c979416d195abff81fe52f644b8 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:59:57 -0400 Subject: [PATCH 18/25] fix buffer --- src/buffer.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/buffer.jl b/src/buffer.jl index 8fe703e1..5af749ea 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -26,17 +26,17 @@ mutable struct Buffer # the total number of transcoded bytes transcoded::Int64 - function Buffer(size::Integer = 0) - return new(Vector{UInt8}(undef, size), 0, 1, 1, 0) + function Buffer(data::Vector{UInt8}, keepbytes::Integer=length(data)) + return new(data, 0, 1, keepbytes+1, 0) end +end - function Buffer(data::Vector{UInt8}) - return new(data, 0, 1, length(data)+1, 0) - end +function Buffer(size::Integer = 0) + return Buffer(Vector{UInt8}(undef, size), 0) end -function Buffer(data::Base.CodeUnits{UInt8}) - return Buffer(Vector{UInt8}(data)) +function Buffer(data::Base.CodeUnits{UInt8}, keepbytes::Integer=length(data)) + return Buffer(Vector{UInt8}(data), keepbytes) end function Base.length(buf::Buffer) From c5cd74356122e010e85ca02ffd6d3861a2fac182 Mon Sep 17 00:00:00 2001 From: Joao Aparicio Date: Fri, 7 Apr 2023 16:37:08 -0500 Subject: [PATCH 19/25] Address PR comment https://github.com/JuliaIO/TranscodingStreams.jl/pull/136#discussion_r1156670839 --- src/buffer.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/buffer.jl b/src/buffer.jl index 5af749ea..89f46eb1 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -27,6 +27,7 @@ mutable struct Buffer transcoded::Int64 function Buffer(data::Vector{UInt8}, keepbytes::Integer=length(data)) + 0 <= keepbytes <= length(data) || throw(ArgumentError("invalid keepbytes: keepbytes must be 0 ≤ keepbytes ≤ length(data), got $keepbytes and length(data)=$(length(data))")) return new(data, 0, 1, keepbytes+1, 0) end end From 8931cb468fc443a69818ed9718e48d781b1fbd17 Mon Sep 17 00:00:00 2001 From: Joao Aparicio Date: Fri, 7 Apr 2023 16:48:24 -0500 Subject: [PATCH 20/25] Address PR comment https://github.com/JuliaIO/TranscodingStreams.jl/pull/136#discussion_r1156681122 --- src/transcode.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transcode.jl b/src/transcode.jl index 4465bcd3..bf74e0e0 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -5,7 +5,7 @@ transcode( ::Type{C}, data::Union{Vector{UInt8},Base.CodeUnits{UInt8}}, - )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} where {C<:Codec} + )::Vector{UInt8} where {C<:Codec} Transcode `data` by applying a codec `C()`. @@ -52,7 +52,7 @@ _default_output_buffer(codec, input) = Buffer( codec::Codec, data::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}, [output::Union{Vector{UInt8},Base.CodeUnits{UInt8},Buffer}], - )::Union{Vector{UInt8},Base.CodeUnits{UInt8}} + )::Vector{UInt8} Transcode `data` by applying `codec`. From b743bcd31da917216ff26af32167325dd565d66d Mon Sep 17 00:00:00 2001 From: Joao Aparicio Date: Fri, 7 Apr 2023 16:52:07 -0500 Subject: [PATCH 21/25] Address PR review https://github.com/JuliaIO/TranscodingStreams.jl/pull/136#issuecomment-1495984605 https://github.com/JuliaIO/TranscodingStreams.jl/pull/136#issuecomment-1496483176 --- src/transcode.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index bf74e0e0..279630a7 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -97,7 +97,16 @@ julia> String(decompressed) function Base.transcode( codec::Codec, input::Buffer, - output::Buffer = _default_output_buffer(codec, input), + output::Union{Buffer,Nothing} = nothing, +) + output = (isnothing(output) ? _default_output_buffer(codec, input) : initbufer!(output)) + transcode!(output, codec, input) +end + +function transcode!( + output::Buffer, + codec::Codec, + input::Buffer, ) error = Error() code = startproc(codec, :write, error) From 8134ab19369df60115b34fb5398f8b7b24d9bb34 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Fri, 7 Apr 2023 21:46:40 -0400 Subject: [PATCH 22/25] dont use isnothing --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index 279630a7..00ca7e90 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -99,7 +99,7 @@ function Base.transcode( input::Buffer, output::Union{Buffer,Nothing} = nothing, ) - output = (isnothing(output) ? _default_output_buffer(codec, input) : initbufer!(output)) + output = (output === nothing ? _default_output_buffer(codec, input) : initbufer!(output)) transcode!(output, codec, input) end From 93c28d42d4789bcb232a5614746d285533034bdf Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Fri, 7 Apr 2023 22:02:18 -0400 Subject: [PATCH 23/25] add doc-string --- src/transcode.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/transcode.jl b/src/transcode.jl index 00ca7e90..8ebfb269 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -103,6 +103,15 @@ function Base.transcode( transcode!(output, codec, input) end +""" + transcode!(output::Buffer, codec::Codec, input::Buffer) + +Transcode `input` by applying `codec` and storing 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. +""" function transcode!( output::Buffer, codec::Codec, From b3f444bcc78740cbe79aac888dcf66ca99b9f23a Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Fri, 7 Apr 2023 22:58:22 -0400 Subject: [PATCH 24/25] replace keepbytes with marginpos --- src/buffer.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/buffer.jl b/src/buffer.jl index 89f46eb1..b490c523 100644 --- a/src/buffer.jl +++ b/src/buffer.jl @@ -26,18 +26,18 @@ mutable struct Buffer # the total number of transcoded bytes transcoded::Int64 - function Buffer(data::Vector{UInt8}, keepbytes::Integer=length(data)) - 0 <= keepbytes <= length(data) || throw(ArgumentError("invalid keepbytes: keepbytes must be 0 ≤ keepbytes ≤ length(data), got $keepbytes and length(data)=$(length(data))")) - return new(data, 0, 1, keepbytes+1, 0) + function Buffer(data::Vector{UInt8}, marginpos::Integer=length(data)+1) + @assert 1 <= marginpos <= length(data)+1 + return new(data, 0, 1, marginpos, 0) end end function Buffer(size::Integer = 0) - return Buffer(Vector{UInt8}(undef, size), 0) + return Buffer(Vector{UInt8}(undef, size), 1) end -function Buffer(data::Base.CodeUnits{UInt8}, keepbytes::Integer=length(data)) - return Buffer(Vector{UInt8}(data), keepbytes) +function Buffer(data::Base.CodeUnits{UInt8}, args...) + return Buffer(Vector{UInt8}(data), args...) end function Base.length(buf::Buffer) From 9a4b8675ded58925d11cc2b492a0ee1da0004b54 Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Fri, 7 Apr 2023 23:07:57 -0400 Subject: [PATCH 25/25] fix typeo --- src/transcode.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transcode.jl b/src/transcode.jl index 8ebfb269..b1b03876 100644 --- a/src/transcode.jl +++ b/src/transcode.jl @@ -99,7 +99,7 @@ function Base.transcode( input::Buffer, output::Union{Buffer,Nothing} = nothing, ) - output = (output === nothing ? _default_output_buffer(codec, input) : initbufer!(output)) + output = (output === nothing ? _default_output_buffer(codec, input) : initbuffer!(output)) transcode!(output, codec, input) end