### CI Link

https://gradle-enterprise.elastic.co/s/tolzhhuvjvobo/tests/:server:test/org.elasticsearch.gateway.PersistedClusterStateServiceTests/testHandlesShuffledDocuments%20%7Bseed=%5B6281127015198EC1:C2E270963902654%5D%7D?top-execution=1

### Repro line

./gradlew ':server:test' --tests "org.elasticsearch.gateway.PersistedClusterStateServiceTests.testHandlesShuffledDocuments {seed=[6281127015198EC1:C2E270963902654]}" -Dtests.seed=6281127015198EC1 -Dtests.locale=ca -Dtests.timezone=Africa/Asmera -Druntime.java=18

### Does it reproduce?

No

### Applicable branches

master, maybe others

### Failure history

_No response_

### Failure excerpt

```
2> junit.framework.AssertionFailedError: Unexpected exception type, expected CorruptStateException but got java.lang.IndexOutOfBoundsException: Range [4, 4 + -3) out of bounds for length 1 |  
-- | --
  | at __randomizedtesting.SeedInfo.seed([6281127015198EC1:C2E270963902654]:0) |  
  | at org.apache.lucene.tests.util.LuceneTestCase.expectThrows(LuceneTestCase.java:2850) |  
  | at org.apache.lucene.tests.util.LuceneTestCase.expectThrows(LuceneTestCase.java:2831) |  
  | at org.elasticsearch.gateway.PersistedClusterStateServiceTests.testHandlesShuffledDocuments(PersistedClusterStateServiceTests.java:1149) |  
  |   |  
  | Caused by: |  
  | java.lang.IndexOutOfBoundsException: Range [4, 4 + -3) out of bounds for length 1 |  
  | at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100) |  
  | at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:118) |  
  | at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:397) |  
  | at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) |  
  | at org.elasticsearch.common.bytes.BytesArray.slice(BytesArray.java:95) |  
  | at org.elasticsearch.common.compress.DeflateCompressor.uncompress(DeflateCompressor.java:208) |  
  | at org.elasticsearch.gateway.PersistedClusterStateService.uncompress(PersistedClusterStateService.java:660) |  
  | at org.elasticsearch.gateway.PersistedClusterStateService.consumeFromType(PersistedClusterStateService.java:622) |  
  | at org.elasticsearch.gateway.PersistedClusterStateService.loadOnDiskState(PersistedClusterStateService.java:551) |  
  | at org.elasticsearch.gateway.PersistedClusterStateService.loadBestOnDiskState(PersistedClusterStateService.java:458) |  
  | at org.elasticsearch.gateway.PersistedClusterStateServiceTests.loadPersistedClusterState(PersistedClusterStateServiceTests.java:1633) |  
  | at org.elasticsearch.gateway.PersistedClusterStateServiceTests.lambda$testHandlesShuffledDocuments$30(PersistedClusterStateServiceTests.java:1149) |  
  | at org.apache.lucene.tests.util.LuceneTestCase._expectThrows(LuceneTestCase.java:3003) |  
  | at org.apache.lucene.tests.util.LuceneTestCase.expectThrows(LuceneTestCase.java:2840) |  
  | ... 2 more
```

I haven't been able to get a reliable reproduction line, but usually running the test 10,000 times seems to provoke it:

```
for i in {0..100}; do ./gradlew ':server:test' -Dtests.iters=100 --tests "org.elasticsearch.gateway.PersistedClusterStateServiceTests.testHandlesShuffledDocuments"; done
```
server/src/main/java/org/elasticsearch/common/compress/DeflateCompressor.java
}
    }
    @Override
    public InputStream threadLocalInputStream(InputStream in) throws IOException {
        return inputStream(in, true);
    }
    /**
     * Creates a new input stream that decompresses the contents read from the provided input stream.
     * Closing the returned stream will close the provided input stream.
     * Optionally uses thread-local, pooled resources to save off-heap allocations if the stream is guaranteed to not escape the current
     * thread.
     *
     * @param in           input stream to wrap
     * @param threadLocal  whether this stream will only be used on the current thread or not
     * @return             decompressing stream
     */
    public static InputStream inputStream(InputStream in, boolean threadLocal) throws IOException {
        final byte[] headerBytes = new byte[HEADER.length];
        final int len = Streams.readFully(in, headerBytes);
        if (len != HEADER.length || Arrays.equals(headerBytes, HEADER) == false) {
            throw new IllegalArgumentException("Input stream is not compressed with DEFLATE!");
        }
        final Releasable releasable;
        final Inflater inflater;
        if (threadLocal) {
            final ReleasableReference<Inflater> current = inflaterForStreamRef.get();
            if (current.inUse) {
                
                inflater = new Inflater(true);
                releasable = inflater::end;