Re: NioZipEncoding throwing IllegalStateException on Android

Gary Gregory <[email protected]> Fri, 1 Aug 2025 20:54:16 -0400
Newsgroups gmane.comp.jakarta.commons.user
Message-ID <CACZkXPz5GAw1LN+uC+AS5ErNCTVOviPUTquwC0J99dGmiBYuhw@mail.gmail.com>
--0000000000000e4c18063b5751fd
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hello Maurice,

Do you have a stack trace?

Gary

On Fri, Aug 1, 2025 at 7:52=E2=80=AFPM Maurice Lam <[email protected]> w=
rote:

> I recently ran into an issue using NioZipEncoding (via
> CpioArchiveOutputStream).
>
> The minimal reproducing code snippet looks like this:
>
> ```
> val encoding =3D ZipEncodingHelper.getZipEncoding(
> StandardCharsets.US_ASCII.name())
> encoding.encode("=C3=B7")
> ```
>
> This crashes on Android with "IllegalStateException: Current state =3D
> CODING, new state =3D CODING" (code
> <
> https://cs.android.com/android/platform/superproject/main/+/main:libcore/=
ojluni/src/main/java/java/nio/charset/CharsetEncoder.java;l=3D952;drc=3D549=
8505951a9e607d809fc88da616f4249eb414e
> >
> ).
>
> My first inclination was to file this bug to Android, but reading the
> documentation for CharsetEncoder.canEncode
> <
> https://docs.oracle.com/javase/8/docs/api/java/nio/charset/CharsetEncoder=
.html
> >,
> it mentions that IllegalStateException is thrown "If an encoding operatio=
n
> is already in progress". Looks like the code in NioZipEncoding is doing
> exactly that.
>
> I believe the reason that it doesn't throw on desktop JVMs is that the Su=
n
> implementation overrides canEncode
> <
> https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/su=
n/nio/cs/US_ASCII.java#L138
> >,
> whereas on Android they use an alternative implementation that uses the
> base implementation in CharsetEncoder.
>
> Maurice
>

--0000000000000e4c18063b5751fd--