Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/TranscodingStreams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export

const ByteData = Union{Vector{UInt8},Base.CodeUnits{UInt8}}

# in-place variant of transcode when output buffer size is known
function transcode! end

include("memory.jl")
include("buffer.jl")
include("error.jl")
Expand Down
40 changes: 40 additions & 0 deletions src/transcode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,43 @@ function initial_output_size(codec::Codec, input::Memory)
8, # just in case where both minoutsize and expectedsize are foolish
)
end
# Mutates the provided output buffer. Useful for cases when output size is known.
function transcode!(codec::Codec, data::ByteData,output::Buffer)
input = Buffer(data)
error = Error()
code = startproc(codec, :write, error)
if code === :error
@goto error
end
@label process
Δin, Δout, code = process(codec, buffermem(input), marginmem(output), error)
@debug(
"called process()",
code = code,
input_size = buffersize(input),
output_size = marginsize(output),
input_delta = Δin,
output_delta = Δout,
)
consumed!(input, Δin)
supplied!(output, Δout)
if code === :error
@goto error
elseif code === :end
if buffersize(input) > 0
if startproc(codec, :write, error) === :error
@goto error
end
@goto process
end
resize!(output.data, output.marginpos - 1)
return output.data
else
@goto process
end
@label error
if !haserror(error)
set_default_error!(error)
end
throw(error[])
end