Skip to content

Commit 3bd4bb8

Browse files
authored
Remove checked EncodingException from encode extension functions (#218)
1 parent e2d6a62 commit 3bd4bb8

3 files changed

Lines changed: 74 additions & 37 deletions

File tree

library/core/src/commonMain/kotlin/io/matthewnelson/encoding/core/Decoder.kt

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
7777
* which will call [doFinal] (or [close] if there was an error with
7878
* decoding) for you.
7979
*
80-
* @see [newDecoderFeed]
8180
* @see [use]
81+
* @see [newDecoderFeed]
8282
* @see [EncoderDecoder.Feed]
8383
* @see [EncoderDecoder.Feed.doFinal]
8484
* */
@@ -106,8 +106,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
106106
/**
107107
* Updates the [Decoder.Feed] with a new character to decode.
108108
*
109-
* @throws [EncodingException] if [isClosed] is true, or if
110-
* there was an error decoding.
109+
* @throws [EncodingException] If [isClosed] is `true`, or if there was an
110+
* error decoding.
111111
* */
112112
@Throws(EncodingException::class)
113113
public fun consume(input: Char) {
@@ -146,12 +146,13 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
146146
}
147147

148148
/**
149-
* Flushes the buffered input and performs any final decoding
150-
* operations without closing the [Feed].
149+
* Flushes the buffered input and performs final decoding operations without
150+
* closing the [Feed].
151151
*
152152
* @see [EncoderDecoder.Feed.flush]
153-
* @throws [EncodingException] if [isClosed] is true, or if
154-
* there was an error decoding.
153+
*
154+
* @throws [EncodingException] If [isClosed] is `true`, or if there was an
155+
* error decoding.
155156
* */
156157
@Throws(EncodingException::class)
157158
public final override fun flush() {
@@ -224,7 +225,9 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
224225
* @see [CharSequence.decodeBuffered]
225226
* @see [CharSequence.decodeBufferedAsync]
226227
*
227-
* @throws [EncodingException] If decoding failed.
228+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
229+
* an invalid character or sequence.
230+
* @throws [EncodingSizeException] If the decoded output would exceed [Int.MAX_VALUE].
228231
* */
229232
@JvmStatic
230233
@Throws(EncodingException::class)
@@ -263,7 +266,9 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
263266
* @see [CharArray.decodeBuffered]
264267
* @see [CharArray.decodeBufferedAsync]
265268
*
266-
* @throws [EncodingException] If decoding failed.
269+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
270+
* an invalid character or sequence.
271+
* @throws [EncodingSizeException] If the decoded output would exceed [Int.MAX_VALUE].
267272
* */
268273
@JvmStatic
269274
@Throws(EncodingException::class)
@@ -340,7 +345,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
340345
* @see [CharSequence.decodeToByteArrayOrNull]
341346
* @see [CharSequence.decodeBufferedAsync]
342347
*
343-
* @throws [EncodingException] If decoding failed.
348+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
349+
* an invalid character or sequence.
344350
* */
345351
@JvmStatic
346352
@Throws(EncodingException::class)
@@ -400,7 +406,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
400406
* @see [CharSequence.decodeToByteArrayOrNull]
401407
* @see [CharSequence.decodeBufferedAsync]
402408
*
403-
* @throws [EncodingException] If decoding failed.
409+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
410+
* an invalid character or sequence.
404411
* @throws [IllegalArgumentException] If [maxBufSize] is less than or equal to
405412
* [EncoderDecoder.Config.maxDecodeEmit].
406413
* */
@@ -477,7 +484,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
477484
* @see [CharSequence.decodeToByteArrayOrNull]
478485
* @see [CharSequence.decodeBufferedAsync]
479486
*
480-
* @throws [EncodingException] If decoding failed.
487+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
488+
* an invalid character or sequence.
481489
* @throws [IllegalArgumentException] If [buf] size is less than or equal to
482490
* [EncoderDecoder.Config.maxDecodeEmit].
483491
* */
@@ -545,7 +553,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
545553
* @see [CharSequence.decodeBuffered]
546554
*
547555
* @throws [CancellationException]
548-
* @throws [EncodingException] If decoding failed.
556+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
557+
* an invalid character or sequence.
549558
* */
550559
@JvmStatic
551560
@Throws(CancellationException::class, EncodingException::class)
@@ -607,7 +616,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
607616
* @see [CharSequence.decodeBuffered]
608617
*
609618
* @throws [CancellationException]
610-
* @throws [EncodingException] If decoding failed.
619+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
620+
* an invalid character or sequence.
611621
* @throws [IllegalArgumentException] If [maxBufSize] is less than or equal to
612622
* [EncoderDecoder.Config.maxDecodeEmit].
613623
* */
@@ -686,7 +696,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
686696
* @see [CharSequence.decodeBuffered]
687697
*
688698
* @throws [CancellationException]
689-
* @throws [EncodingException] If decoding failed.
699+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
700+
* an invalid character or sequence.
690701
* @throws [IllegalArgumentException] If [buf] size is less than or equal to
691702
* [EncoderDecoder.Config.maxDecodeEmit].
692703
* */
@@ -755,7 +766,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
755766
* @see [CharArray.decodeToByteArrayOrNull]
756767
* @see [CharArray.decodeBufferedAsync]
757768
*
758-
* @throws [EncodingException] If decoding failed.
769+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
770+
* an invalid character or sequence.
759771
* */
760772
@JvmStatic
761773
@Throws(EncodingException::class)
@@ -817,7 +829,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
817829
* @see [CharArray.decodeToByteArrayOrNull]
818830
* @see [CharArray.decodeBufferedAsync]
819831
*
820-
* @throws [EncodingException] If decoding failed.
832+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
833+
* an invalid character or sequence.
821834
* @throws [IllegalArgumentException] If [maxBufSize] is less than or equal to
822835
* [EncoderDecoder.Config.maxDecodeEmit].
823836
* */
@@ -898,7 +911,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
898911
* @see [CharArray.decodeToByteArrayOrNull]
899912
* @see [CharArray.decodeBufferedAsync]
900913
*
901-
* @throws [EncodingException] If decoding failed.
914+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
915+
* an invalid character or sequence.
902916
* @throws [IllegalArgumentException] If [buf] size is less than or equal to
903917
* [EncoderDecoder.Config.maxDecodeEmit].
904918
* */
@@ -967,7 +981,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
967981
* @see [CharArray.decodeBuffered]
968982
*
969983
* @throws [CancellationException]
970-
* @throws [EncodingException] If decoding failed.
984+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
985+
* an invalid character or sequence.
971986
* */
972987
@JvmStatic
973988
@Throws(CancellationException::class, EncodingException::class)
@@ -1030,7 +1045,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
10301045
* @see [CharArray.decodeBuffered]
10311046
*
10321047
* @throws [CancellationException]
1033-
* @throws [EncodingException] If decoding failed.
1048+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
1049+
* an invalid character or sequence.
10341050
* @throws [IllegalArgumentException] If [maxBufSize] is less than or equal to
10351051
* [EncoderDecoder.Config.maxDecodeEmit].
10361052
* */
@@ -1110,7 +1126,8 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
11101126
* @see [CharArray.decodeBuffered]
11111127
*
11121128
* @throws [CancellationException]
1113-
* @throws [EncodingException] If decoding failed.
1129+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
1130+
* an invalid character or sequence.
11141131
* @throws [IllegalArgumentException] If [buf] size is less than or equal to
11151132
* [EncoderDecoder.Config.maxDecodeEmit].
11161133
* */
@@ -1130,6 +1147,9 @@ public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {
11301147

11311148
/**
11321149
* DEPRECATED since `2.3.0`
1150+
* @throws [EncodingException] If decoding failed, such as the [decoder] rejecting
1151+
* an invalid character or sequence.
1152+
* @throws [EncodingSizeException] If the decoded output would exceed [Int.MAX_VALUE].
11331153
* @suppress
11341154
* */
11351155
@JvmStatic

library/core/src/commonMain/kotlin/io/matthewnelson/encoding/core/Encoder.kt

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
8484
* which will call [doFinal] (or [close] if there was an error with
8585
* encoding) for you.
8686
*
87-
* @see [newEncoderFeed]
8887
* @see [use]
88+
* @see [newEncoderFeed]
8989
* @see [EncoderDecoder.Feed]
9090
* @see [EncoderDecoder.Feed.doFinal]
9191
* */
@@ -116,7 +116,9 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
116116
/**
117117
* Updates the [Encoder.Feed] with a new byte to encode.
118118
*
119-
* @throws [EncodingException] if [isClosed] is true.
119+
* @throws [EncodingException] If [isClosed] is `true`, or the [Feed] is configured
120+
* to reject something, such as `UTF-8` byte to text transformations rejecting
121+
* invalid byte sequences.
120122
* */
121123
@Throws(EncodingException::class)
122124
public fun consume(input: Byte) {
@@ -135,7 +137,10 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
135137
* operations without closing the [Feed].
136138
*
137139
* @see [EncoderDecoder.Feed.flush]
138-
* @throws [EncodingException] if [isClosed] is true.
140+
*
141+
* @throws [EncodingException] If [isClosed] is `true`, or the [Feed] is configured
142+
* to reject something, such as `UTF-8` byte to text transformations rejecting
143+
* invalid byte sequences.
139144
* */
140145
@Throws(EncodingException::class)
141146
public final override fun flush() {
@@ -199,14 +204,19 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
199204
public companion object {
200205

201206
/**
202-
* Encodes a [ByteArray] for the provided [encoder] and
203-
* returns the encoded data in the form of a [String].
207+
* Encode a [ByteArray].
208+
*
209+
* @param [encoder] The [Encoder] to use.
210+
*
211+
* @return The [String] of encoded data.
204212
*
205-
* @throws [EncodingSizeException] if the encoded output
206-
* exceeds [Int.MAX_VALUE].
213+
* @see [encodeToCharArray]
214+
*
215+
* @throws [EncodingException] If the [encoder] is configured to reject something,
216+
* such as `UTF-8` byte to text transformations rejecting invalid byte sequences.
217+
* @throws [EncodingSizeException] If the encoded output would exceed [Int.MAX_VALUE].
207218
* */
208219
@JvmStatic
209-
@Throws(EncodingSizeException::class)
210220
public fun ByteArray.encodeToString(encoder: Encoder<*>): String {
211221
return encoder.encodeOutMaxSizeOrFail(size) { maxSize ->
212222
val sb = StringBuilder(maxSize)
@@ -224,14 +234,19 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
224234
}
225235

226236
/**
227-
* Encodes a [ByteArray] for the provided [encoder] and
228-
* returns the encoded data in the form of a [CharArray].
237+
* Encode a [ByteArray].
238+
*
239+
* @param [encoder] The [Encoder] to use.
240+
*
241+
* @return The [CharArray] of encoded data.
242+
*
243+
* @see [encodeToString]
229244
*
230-
* @throws [EncodingSizeException] if the encoded output
231-
* exceeds [Int.MAX_VALUE].
245+
* @throws [EncodingException] If the [encoder] is configured to reject something,
246+
* such as `UTF-8` byte to text transformations rejecting invalid byte sequences.
247+
* @throws [EncodingSizeException] If the encoded output exceeds [Int.MAX_VALUE].
232248
* */
233249
@JvmStatic
234-
@Throws(EncodingSizeException::class)
235250
public fun ByteArray.encodeToCharArray(encoder: Encoder<*>): CharArray {
236251
return encoder.encodeOutMaxSizeOrFail(size) block@ { maxSize ->
237252
var i = 0
@@ -248,10 +263,12 @@ public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder<C>(con
248263

249264
/**
250265
* DEPRECATED since `2.3.0`
266+
* @throws [EncodingException] If the [encoder] is configured to reject something,
267+
* such as `UTF-8` byte to text transformations rejecting invalid byte sequences.
268+
* @throws [EncodingSizeException] if the encoded output exceeds [Int.MAX_VALUE].
251269
* @suppress
252270
* */
253271
@JvmStatic
254-
@Throws(EncodingSizeException::class)
255272
@Deprecated(
256273
message = "Should not utilize. Underlying Char to Byte conversion can produce incorrect results",
257274
level = DeprecationLevel.ERROR,

library/core/src/commonMain/kotlin/io/matthewnelson/encoding/core/EncoderDecoder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public abstract class EncoderDecoder<C: EncoderDecoder.Config>(config: C): Encod
546546
* [Feed]s are meant to be single use disposables for the
547547
* given encoding/decoding operation.
548548
*
549-
* TLDR; [Feed]s only care about [Byte]s and [Char]s, not the medium
549+
* TL;DR [Feed]s only care about [Byte]s and [Char]s, not the medium
550550
* for which they come from or are going to. Use the [use] extension
551551
* function.
552552
*
@@ -635,7 +635,7 @@ public abstract class EncoderDecoder<C: EncoderDecoder.Config>(config: C): Encod
635635
* will not be considered an error if already closed.
636636
*
637637
* After [close] has been called, any invocation of
638-
* [Decoder.Feed.consume], [Encoder.Feed.consume],
638+
* [Decoder.Feed.consume], [Encoder.Feed.consume], [flush]
639639
* or [doFinal] will be considered an error and throw an
640640
* [EncodingException].
641641
*

0 commit comments

Comments
 (0)