Repository: apache/mina-sshd
Issue: SftpInputStreamAsync#read(byte[], int, int) fails on calls with len = 0 

### Version

2.9.2

### Bug description

When passing len = 0 on a second read call, after eofIndicator has already be set to true, the read method returns -1. This can lead to an incomplete read when using e.g. java.io.InputStream#readAllBytes or java.io.InputStream#readNBytes(int). 

A possible solution would be an early return of 0 in case of len = 0




### Actual behavior

First reading offset 0, len 8192,
then offset 8192, len 0
then offset 8192, len 8192

Return in sum at most 8192 bytes (if available)

### Expected behavior

First reading offset 0, len 8192,
then offset 8192, len 0
then offset 8192, len 8192

Return in sum more 8192 bytes (if available)

### Relevant log output

_No response_

### Other information

_No response_

Changed files:
sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpTest.java
sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java

diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
index 68e45f363..7979dcf0b 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/impl/SftpInputStreamAsync.java
@@ -136,7 +136,7 @@ public class SftpInputStreamAsync extends InputStreamWithChannel implements Sftp
             int l = buf.available();
             buf.getRawBytes(b, offset.getAndAdd(l), l);
         });
-        if (res == 0 && eofIndicator) {
+        if (res == 0 && eofIndicator && hasNoData()) {
             res = -1;
         }
         return res;
diff --git a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpTest.java b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpTest.java
index a0b408d1b..d2f9ec3fc 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpTest.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpTest.java
@@ -284,6 +284,51 @@ public class SftpTest extends AbstractSftpClientTestSupport {
         }
     }
 
+    @Test // see SSHD-1287
+    public void testZeroRead() throws Exception {
+        Path targetPath = detectTargetFolder();
+        Path parentPath = targetPath.getParent();
+        Path lclSftp = CommonTestSupportUtils.resolve(targetPath, SftpConstants.SFTP_SUBSYSTEM_NAME, getClass().getSimpleName(),
+                getCurrentTestName());
+        Path testFile = assertHierarchyTargetFolderExists(lclSftp).resolve("file.bin");
+        byte[] expected = new byte[4000];
+
+        Factory<? extends Random> factory = sshd.getRandomFactory();
+        Random rnd = factory.create();
+        rnd.fill(expected);
+        Files.write(testFile, expected);
+
+        String file = CommonTestSupportUtils.resolveRelativeRemotePath(parentPath, testFile);
+        try (SftpClient sftp = createSingleSessionClient()) {
+            byte[] actual = new byte[expected.length];
+            try (InputStream in = sftp.read(file)) {
+                int off = 0;
+                int n = 0;
+                while (off < actual.length) {
+                    n = in.read(actual, off, 100);
+                    if (n < 0) {
+                        break;
+                    }
+                    off += n;
+                    if (in.read(actual, off, 0) < 0) {
+                        break;
+                    }
+                }
+                assertEquals("Short read", actual.length, off);
+                if (n >= 0) {
+                    n = in.read();
+                    assertTrue("Stream not at eof", n < 0);
+                }
+            }
+            for (int index = 0; index < actual.length; index++) {
+                byte expByte = expected[index];
+                byte actByte = actual[index];
+                assertEquals("Mismatched values at index=" + index, Integer.toHexString(expByte & 0xFF),
+                        Integer.toHexString(actByte & 0xFF));
+            }
+        }
+    }
+
     @Test // see SSHD-1288
     public void testReadWriteDownload() throws Exception {
         Assume.assumeTrue("Not sure appending to a file opened for reading works on Windows",