hierynomus / sshj

ssh, scp and sftp for java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullpointerException on downloadFile (starts with version 0.34)

DevMaddin opened this issue · comments

We use SSHJ to download files from a SFTP-Server and used a long time the version 0.19.
Now we upgrade to version 0.36 and become a NullpointerException on downloading a file.
I tested other versions and the version 0.33 works but with 0.34 the Nullpointer is thrown.

Here is the Stacktrace:
grafik

Any ideas what has changed in version 0.34 and not working with our code?

Update:
I found the Nullpointer in the Implementation of "InMemoryDestFile":
grafik

What is the correct Implementation for "getLength()" and "getOutputStream(boolean append)"?

The StreamingInMemoryDestFile is not part of SSHJ.

The method that contains line 172 which you indicate contains the following code in 0.34.0:

private LocalDestFile downloadFile(final StreamCopier.Listener listener,
final RemoteResourceInfo remote,
final LocalDestFile local,
final long byteOffset)
throws IOException {
final LocalDestFile adjusted = local.getTargetFile(remote.getName());
final RemoteFile rf = engine.open(remote.getPath());
try {
log.debug("Attempting to download {} with offset={}", remote.getPath(), byteOffset);
final RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16, byteOffset);
final OutputStream os = adjusted.getOutputStream(byteOffset != 0);
try {
new StreamCopier(rfis, os, engine.getLoggerFactory())
.bufSize(engine.getSubsystem().getLocalMaxPacketSize())
.keepFlushing(false)
.listener(listener)
.copy();
} finally {
rfis.close();
os.close();
}
} finally {
rf.close();
}
return adjusted;
}

This just closes the outputstream that is assigned at L163.

Not sure how that can be null if you're actually indeed passing in that StreamingInMemoryDestFile

Thanks @hierynomus .
My comment "before NULL" means that I returned NULL before. 😅
Now i set "getOutputStream()" and my question is if that is correct and what could the correct Implementation for "getLength()" and "getOutputStream(boolean append)"?

I'd say it depends, I'm not sure you you use the library. The append flag is there when you're resuming a downloaded file, so with append=true you'd expect the current implementation that returns the outputStream. With append=false you would expect to set and return a new or truncated outputstream, so that there's no previous contents.
The getLength looks fine.

Thanks. I will try it.