Skip to content

NestedByteChannel.read(ByteBuffer) incorrectly returns 0 when buffer could not be filled #51515

Closed

Description

@turbanoff

We have classpath scanner which reads all jars. It stumbles on nested jars packed by spring-boot.
AbstractDataLoader.ScanJarFileTask opens them with Files.newInputStream(), which ends up on NestedByteChannel.

E 260827 112253.294 [ForkJoinPool.commonPool-worker-13] AssignableClassesLoader - Error scan jar file: /opt/xt/gateway/lib/gateway.jar/!BOOT-INF/lib/metrics-515.jar
java.io.EOFException: Unexpected end of ZLIB input stream
        at java.base/java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:266)
        at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:175)
        at java.base/java.util.zip.ZipInputStream.read(ZipInputStream.java:412)
        at java.base/java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:171)
        at java.base/java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:149)
        at com.loaders.AbstractDataLoader$ScanJarFileTask.compute(AbstractDataLoader.java:257)
    int total = 0;
    while (dst.remaining() > 0) {
        int count = resources.getData().read(dst, position);
        if (count <= 0)
            return (total != 0) ? 0 : count;   // <- should return total
        total += count; position += count;
    }
    return total;

What scanner does:
Gathers jars and directories:

    private static Set<URL> getResources(@Nonnull ClassLoader classLoader, @Nonnull String path) throws IOException {
        Enumeration<URL> resources = classLoader.getResources(path);
        Set<URL> result = new LinkedHashSet<>();
        while (resources.hasMoreElements()) {
            result.add(resources.nextElement());
        }
        return result;
    }

Filters jar files:

    @Nonnull
    private static URI extractJarPath(@Nonnull URL jarResource) {
        // example input: 'jar:file:/opt/xt/api-209.jar!/com/api/descriptors'
        String fileResource = jarResource.getFile();
        // fileResource: 'file:/opt/api-209.jar!/com/api/descriptors'
        int jarIndex = fileResource.lastIndexOf(".jar!");
        if (jarIndex == -1) {
            throw new IllegalArgumentException("Unexpected jar resource URL with '.jar!' in it: " + jarResource);
        }
        String fileOnlyUri = fileResource.substring(0, jarIndex + 4);
        int mainJarIndex = fileResource.indexOf(".jar!");
        // uber-jar spring-boot
        if (mainJarIndex != jarIndex) {
            return URI.create("jar:" + fileOnlyUri);
        } else {
            // fileOnlyUri: 'file:/opt/xt/api-209.jar'
            return URI.create(fileOnlyUri);
        }
    }
            List<Path> dirs = new ArrayList<>();
            List<URI> jarFiles = new ArrayList<>();
            for (URL resource : resources) {
                if ("jar".equals(resource.getProtocol())) {
                    jarFiles.add(extractJarPath(resource));
                } else {
                    dirs.add(Path.of(resource.toURI()));
                }
            }

Then reads jars

            try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(Files.newInputStream(file)))) {
                for (ZipEntry ze; (ze = zis.getNextEntry()) != null; ) {

Activity

  1. added
    status: waiting-for-triage
    on Aug 31, 2026
  2. philwebb commented on Aug 31, 2026

    @philwebb
    Member

    Are you able to provide a sample application that reproduces the issue? Something that we can run and debug.

  3. added
    status: waiting-for-feedback
    on Aug 31, 2026
  4. AbarnaaSree commented on Aug 31, 2026

    @AbarnaaSree

    I’d like to investigate this. I’ll work on creating a minimal sample application that reproduces the NestedByteChannel.read(ByteBuffer) behavior with a Spring Boot nested JAR, and I’ll share it here once I can reproduce the failure.

  5. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Aug 31, 2026
  6. added
    status: waiting-for-feedback
    and removed
    status: feedback-provided
    on Aug 31, 2026
  7. snicoll commented on Aug 31, 2026

    @snicoll
    Member

    @AbarnaaSree thanks but we're asking the sample to the OP.

  8. changed the title [-]Buggy NestedByteChannel.read(ByteBuffer) prevents reading nested jar's[/-] [+]Buggy NestedByteChannel.read(ByteBuffer) prevents reading nested jars[/+] on Sep 1, 2026
  9. turbanoff commented on Sep 1, 2026

    @turbanoff
    Author

    Here is the reproducer - https://github.com/turbanoff/spring-boot-bug-51515

    java.io.EOFException: Unexpected end of ZLIB input stream
            at java.base/java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:266) ~[na:na]
            at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:175) ~[na:na]
            at java.base/java.util.zip.ZipInputStream.read(ZipInputStream.java:412) ~[na:na]
            at java.base/java.io.InputStream.readNBytes(InputStream.java:412) ~[na:na]
            at java.base/java.util.zip.ZipInputStream.readNBytes(ZipInputStream.java:274) ~[na:na]
            at java.base/java.io.InputStream.readAllBytes(InputStream.java:349) ~[na:na]
            at java.base/java.util.zip.ZipInputStream.readAllBytes(ZipInputStream.java:234) ~[na:na]
            at com.example.repro.NestedJarScanner.scanJarFile(NestedJarScanner.java:43) ~[!/:0.0.1-SNAPSHOT]
    
  10. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Sep 1, 2026
  11. wilkinsona commented on Sep 1, 2026

    @wilkinsona
    Member

    Thanks for the sample, @turbanoff. I've reproduced the problem.

    I noticed that the sample's using Spring Boot 3.5.16 and 3.5.x is no longer support. If I upgrade to 4.0.8, the problem still occurs. This means that we'll continue to investigate but please be aware that consuming any OSS fix will require an upgrade to Spring Boot 4.

  12. removed
    status: feedback-provided
    on Sep 1, 2026
  13. self-assigned this
    on Sep 1, 2026
  14. added
    type: bug
    and removed
    status: waiting-for-triage
    on Sep 1, 2026
  15. added this to the 4.0.x milestone on Sep 1, 2026
  16. wilkinsona commented on Sep 1, 2026

    @wilkinsona
    Member

    Thanks, too, for the analysis, @turbanoff. I agree that total should be returned, not 0. Doing so fixes the sample such that it can process the full uber jar.

  17. changed the title [-]Buggy NestedByteChannel.read(ByteBuffer) prevents reading nested jars[/-] [+]NestedByteChannel.read(ByteBuffer) incorrectly returns 0 from read(ByteBuffer) when buffer could not be filled[/+] on Sep 1, 2026
  18. modified the milestones: 4.0.x, 4.0.9 on Sep 1, 2026
  19. added a commit that references this issue on Sep 1, 2026
    837f44f
  20. changed the title [-]NestedByteChannel.read(ByteBuffer) incorrectly returns 0 from read(ByteBuffer) when buffer could not be filled[/-] [+]NestedByteChannel.read(ByteBuffer) incorrectly returns 0 when buffer could not be filled[/+] on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions