05nelsonm / encoding

A Kotlin Multiplatform library for configurable, streamable, efficient and extensible Encoding/Decoding with support for base16/32/64.
Apache License 2.0
33 stars 5 forks source link

Add ability to `flush` an `Encoder.Feed` #108

Closed 05nelsonm closed 1 year ago

05nelsonm commented 1 year ago

When using an Encoder.Feed directly, it'd be nice to be able to call doFinal w/o closing the feed so that multiple things can be encoded without having to instantiate a new feed. Having the ability to call flush will open up the possibility to perform multiple encodings within the .use lambda.

val sb = StringBuilder()
Base64().newEncoderFeed { encodedChar ->
    sb.append(encodedChar)
}.use { feed ->
    messageBytes.forEach { b -> feed.consume(b) }

    feed.flush()

    sb.append(':')

    signatureBytes.forEach { b -> feed.consume(b) }
}
05nelsonm commented 1 year ago

flush() should be available for both Encoder.Feed and Decoder.Feed.

Because of some of the implementations that hold on to some stateful data, this should be implemented in the following manner:

Implementors of EncoderDecoder.Feed were required to OptIn to the ExperimentalEncodingApi, so this is a "non-breaking" change

05nelsonm commented 1 year ago

Actually, this can be done w/o the need to break the current API