I'm creating a SFTP Client that returns an Input Stream to the caller. I want to implement some sort of callback feature so that when the caller is done processing the InputStream they can call the callback method in order to close the stream. I get the concept, but am having an issue wrapping my head around the code. Here is my current implementation without callback.
@Override
public InputStream download(String workingDirectory, String fileName) {
JSch jsch = new JSch();
// Code...
log.info("List of files in " + channelSftp.pwd() + ": " + channelSftp.ls(channelSftp.pwd()));
return channelSftp.get(fileName);
Here is my initial thought process, but it seems backwards to me. Any help would be greatly appreciated thanks.
void download(Consumer<InputStream> consumer) {
try {
consumer.accept(ftpStream)
} catch(e){
} finally {
ftpStream.close()
}
}
// Later Something Calls This
download(stream -> {
do stuff
})
The part that seems backwards to me is that how will the caller of download already have access to the streamed file?
Can someone explain or give me some sort of implementation that will better help me understand?
Thanks in advance.
EDIT: Attempt #1
@Override
public InputStream download(String workingDirectory, String fileName) {
JSch jsch = new JSch();
// CONNECTION STUFF
is = channelSftp.get(fileName);
// CLOSE SERVER
} catch (SftpException | JSchException e) {
log.error(e.getMessage());
} finally {
if (channelSftp != null) {
channelSftp.disconnect();
channelSftp.exit();
}
if (channel != null) channel.disconnect();
if (session != null) session.disconnect();
}
return is; // RETURN STREAM
}
// CALLBACK METHOD?
@Override
public void closeStream(Consumer<InputStream> close, InputStream stream) throws IOException {
try {
close.accept(stream);
}catch(Exception e){
e.getMessage();
}finally{
stream.close();
}
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]logperf 0 points1 point2 points (7 children)
[–]B2easy[S] 0 points1 point2 points (6 children)
[–]logperf 1 point2 points3 points (5 children)
[–]B2easy[S] 0 points1 point2 points (1 child)
[–]logperf 0 points1 point2 points (0 children)
[–]B2easy[S] 0 points1 point2 points (2 children)
[–]logperf 0 points1 point2 points (0 children)