Removed ReaderWriter::readAllBytes (#6070)

As planned for Jersey 4.0 (see notes in the removed code): Getting rid of a worarkound for a bug in one specific application server product (not a bug of Jersey, not a bug of OpenJDK). The vendor had several years of time to fix his own code. Keeping copies of OpenJDK-internals within libraries and applications is a no-go prevents JRE-internal optimizations.

Disclaimer: The contributor is part of the core-libs team at OpenJDK.
diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java
index 35a7ee1..333bbbb 100644
--- a/core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java
+++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2026 Oracle and/or its affiliates. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v. 2.0, which is available at
@@ -158,7 +158,7 @@
      * @throws IOException if there is an error reading from the input stream.
      */
     public static String readFromAsString(InputStream in, MediaType type) throws IOException {
-        return new String(readAllBytes(in), getCharset(type));
+        return new String(in.readAllBytes(), getCharset(type));
     }
 
     /**
@@ -188,81 +188,7 @@
      * @since 2.47
      */
     public static byte[] readFromAsBytes(InputStream in) throws IOException {
-        return readAllBytes(in);
-    }
-    /**
-     * The maximum size of an array to allocate.
-     * Some VMs reserve some header words in an array.
-     * Attempts to allocate larger arrays may result in
-     * OutOfMemoryError: Requested array size exceeds the VM limit
-     */
-    private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
-
-    /**
-     * Java 9+ InputStream::readAllBytes
-     * TODO Replace in Jersey 4.0, as the sole difference to OpenJDK is working around a bug in the input stream.
-     */
-    private static byte[] readAllBytes(InputStream inputStream) throws IOException {
-        List<byte[]> bufs = null;
-        byte[] result = null;
-        int total = 0;
-        int remaining = Integer.MAX_VALUE;
-        int n;
-        do {
-            byte[] buf = new byte[Math.min(remaining, BUFFER_SIZE)];
-            int nread = 0;
-
-            // read to EOF which may read more or less than buffer size
-            while ((n = inputStream.read(buf, nread,
-                    Math.min(buf.length - nread, remaining))) > 0) {
-                nread += n;
-                remaining -= n;
-
-                if (nread == BUFFER_SIZE) { // This differs from JDK version
-                    break;                  // prevents a bug (See ReaderWriterTest)
-                }
-            }
-
-            if (nread > 0) {
-                if (MAX_BUFFER_SIZE - total < nread) {
-                    throw new OutOfMemoryError("Required array size too large");
-                }
-                if (nread < buf.length) {
-                    buf = Arrays.copyOfRange(buf, 0, nread);
-                }
-                total += nread;
-                if (result == null) {
-                    result = buf;
-                } else {
-                    if (bufs == null) {
-                        bufs = new ArrayList<>();
-                        bufs.add(result);
-                    }
-                    bufs.add(buf);
-                }
-            }
-            // if the last call to read returned -1 or the number of bytes
-            // requested have been read then break
-        } while (n >= 0 && remaining > 0);
-
-        if (bufs == null) {
-            if (result == null) {
-                return new byte[0];
-            }
-            return result.length == total ? result : Arrays.copyOf(result, total);
-        }
-
-        result = new byte[total];
-        int offset = 0;
-        remaining = total;
-        for (byte[] b : bufs) {
-            int count = Math.min(b.length, remaining);
-            System.arraycopy(b, 0, result, offset, count);
-            offset += count;
-            remaining -= count;
-        }
-
-        return result;
+        return in.readAllBytes();
     }
 
     /**
diff --git a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/ReaderWriterTest.java b/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/ReaderWriterTest.java
deleted file mode 100644
index 59edddd..0000000
--- a/tests/e2e-core-common/src/test/java/org/glassfish/jersey/tests/e2e/common/message/internal/ReaderWriterTest.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v. 2.0, which is available at
- * http://www.eclipse.org/legal/epl-2.0.
- *
- * This Source Code may also be made available under the following Secondary
- * Licenses when the conditions for such availability set forth in the
- * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
- * version 2 with the GNU Classpath Exception, which is available at
- * https://www.gnu.org/software/classpath/license.html.
- *
- * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
- */
-
-package org.glassfish.jersey.tests.e2e.common.message.internal;
-
-import org.glassfish.jersey.message.internal.ReaderWriter;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-import jakarta.ws.rs.core.MediaType;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-
-public class ReaderWriterTest {
-    @Test
-    public void testToNeverAskToReadZeroBytes() throws IOException {
-        // Unnamed app server bug test
-        int size = ((ReaderWriter.BUFFER_SIZE + 1000) / 10) * 10;
-        StringBuilder sb = new StringBuilder(size);
-        String shortMsg = "0123456789";
-        while (sb.length() < size) {
-            sb.append(shortMsg);
-        }
-
-        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)) {
-            @Override
-            public synchronized int read(byte[] b, int off, int len) {
-                if (len == 0) {
-                    return -1; // simulate the bug
-                }
-                return super.read(b, off, len);
-            }
-        };
-
-        String read = ReaderWriter.readFromAsString(byteArrayInputStream, MediaType.TEXT_HTML_TYPE);
-        Assertions.assertEquals(size, read.length());
-    }
-}