NioZipEncoding throwing IllegalStateException on Android

Maurice Lam <[email protected]> Fri, 1 Aug 2025 16:37:06 -0700
Newsgroups gmane.comp.jakarta.commons.user
Message-ID <CAP+CcduYr=_+L-XQPrGoKJNQ76ho6MRRfzAjL7vmc_jwj+wvWQ@mail.gmail.com>
--000000000000e7d439063b563de2
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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/o=
jluni/src/main/java/java/nio/charset/CharsetEncoder.java;l=3D952;drc=3D5498=
505951a9e607d809fc88da616f4249eb414e>
).

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 operation
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 Sun
implementation overrides canEncode
<https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/sun=
/nio/cs/US_ASCII.java#L138>,
whereas on Android they use an alternative implementation that uses the
base implementation in CharsetEncoder.

Maurice

--000000000000e7d439063b563de2--