|
| 1 | +# Codec Interfaces |
| 2 | +# ================ |
| 3 | + |
| 4 | +abstract type Codec end |
| 5 | + |
| 6 | +abstract type Mode end |
| 7 | +struct ReadMode <: Mode end |
| 8 | +struct WriteMode <: Mode end |
| 9 | + |
| 10 | +primitive type ProcCode 8 end |
| 11 | +const PROC_INIT = reinterpret(ProcCode, 0x00) |
| 12 | +const PROC_OK = reinterpret(ProcCode, 0x01) |
| 13 | +const PROC_FINISH = reinterpret(ProcCode, 0x02) |
| 14 | + |
| 15 | + |
| 16 | +# Start |
| 17 | +# ----- |
| 18 | + |
| 19 | +""" |
| 20 | + start(::Type{ReadMode}, codec::Codec, source::IO)::Void |
| 21 | +
|
| 22 | +Start transcoding using `codec` with read mode. |
| 23 | +
|
| 24 | +This method will be called only once before calling `process` first time. |
| 25 | +
|
| 26 | +Implementing this method is optional; there is a default method that does nothing. |
| 27 | +""" |
| 28 | +function start(::Type{ReadMode}, ::Codec, ::IO) |
| 29 | + return nothing |
| 30 | +end |
| 31 | + |
| 32 | +""" |
| 33 | + start(::Type{WriteMode}, codec::Codec, sink::IO)::Void |
| 34 | +
|
| 35 | +Start transcoding using `codec` with write mode. |
| 36 | +
|
| 37 | +This method will be called only once before calling `process` first time. |
| 38 | +
|
| 39 | +Implementing this method is optional; there is a default method that does nothing. |
| 40 | +""" |
| 41 | +function start(::Type{WriteMode}, ::Codec, ::IO) |
| 42 | + return nothing |
| 43 | +end |
| 44 | + |
| 45 | + |
| 46 | +# Process |
| 47 | +# ------- |
| 48 | + |
| 49 | +""" |
| 50 | + process(::Type{ReadMode}, codec::Codec, source::IO, output::Ptr{UInt8}, nbytes::Int)::Tuple{Int,ProcCode} |
| 51 | +
|
| 52 | +Transcode data using `codec` with read mode. |
| 53 | +
|
| 54 | + <> >-----(process)-----> [........] |
| 55 | + source codec output |
| 56 | +
|
| 57 | +This method reads some data from `source` and write the transcoded data to |
| 58 | +`output` at most `nbytes`, and then returns the number of written bytes and an |
| 59 | +appropriate return code. It will be called repeatedly to incrementally |
| 60 | +transcode input data from `source`. It can assume `output` points to a valid |
| 61 | +memory position and `nbytes` are positive. The intermediate result of |
| 62 | +transcoding is indicated by the return value of it. If processing works |
| 63 | +properly and there are still data to write, it must return the number of written |
| 64 | +bytes and `PROC_OK`. If processing finishes and there are no remaining data to |
| 65 | +write, it must return the number of written bytes and `PROC_FINISH`. Therefore, |
| 66 | +after finishing reading data from `source` and all (possibly buffered) data are |
| 67 | +written to `output`, it is expected to return `(0, PROC_FINISH)` forever. |
| 68 | +
|
| 69 | +This method may throw an exception when transcoding fails. For example, when |
| 70 | +`codec` is a decompression algorithm, it may throw an exception if it reads |
| 71 | +malfomed data from `source`. However, this method must not throw `EOFError` |
| 72 | +when reading data from `source`. Also note that it is responsible for this |
| 73 | +method to release resources allocated by `codec` when an exception happens. |
| 74 | +""" |
| 75 | +function process(::Type{ReadMode}, codec::Codec, stream::IO, data::Ptr{UInt8}, nbytes::Int) |
| 76 | + error("codec $(codec) does not implement read mode") |
| 77 | +end |
| 78 | + |
| 79 | +""" |
| 80 | + process(::Type{WriteMode}, codec::Codec, sink::IO, input::Ptr{UInt8}, nbytes::Int)::Tuple{Int,ProcCode} |
| 81 | +
|
| 82 | +Transcode data using `codec` with write mode. |
| 83 | +
|
| 84 | + <> <-----(transcode)-----< [.......] |
| 85 | + sink codec input |
| 86 | +
|
| 87 | +This method reads some data from `input` and write the transcoded bytes to `sink`. |
| 88 | +""" |
| 89 | +function process(::Type{WriteMode}, codec::Codec, stream::IO, data::Ptr{UInt8}, nbytes::Int) |
| 90 | + error("codec $(codec) does not implement write mode") |
| 91 | +end |
| 92 | + |
| 93 | + |
| 94 | +# Finish |
| 95 | +# ------ |
| 96 | + |
| 97 | +""" |
| 98 | + finish(::Type{ReadMode}, codec::Codec, source::IO)::Void |
| 99 | +
|
| 100 | +Finish transcoding using `codec` with read mode. |
| 101 | +
|
| 102 | +This method will be called only once after calling `process` last time. |
| 103 | +
|
| 104 | +Implementing this method is optional; there is a default method that does nothing. |
| 105 | +""" |
| 106 | +function finish(::Type{ReadMode}, ::Codec, ::IO) |
| 107 | + return nothing |
| 108 | +end |
| 109 | + |
| 110 | +""" |
| 111 | + finish(::Type{WriteMode}, codec::Codec, sink::IO)::Void |
| 112 | +
|
| 113 | +Finish transcoding using `codec` with write mode. |
| 114 | +
|
| 115 | +This method will be called only once after calling `process` last time. |
| 116 | +
|
| 117 | +Implementing this method is optional; there is a default method that does nothing. |
| 118 | +""" |
| 119 | +function finish(::Type{WriteMode}, ::Codec, ::IO) |
| 120 | + return nothing |
| 121 | +end |
0 commit comments