Streaming deleted files to file objects

If anyone knows a quick way to get a remote file that directly streams content to a file object, so there is no need to store the file temporarily on a computer, it will be greatly appreciated! So far, I am copying the file from the remote ios device as follows (using net.schmizz.sshj):

SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(fingerprint);
ssh.connect(ip);

try {
    ssh.authPassword("username", "userpassword".toCharArray());
    ssh.newSCPFileTransfer().download(fileRemote, new FileSystemFile(fileLocal));
} catch (IOException ioe) {
    ioe.printStackTrace();
} finally {
    ssh.disconnect();
}

If anyone is interested in the solution code:

As mentioned in his answer as Nutlike, it is better to use InMemoryDestFile. So create the following class:

class MyInMemoryDestFile extends InMemoryDestFile {
    public ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    @Override
    public ByteArrayOutputStream getOutputStream() throws IOException {
        return this.outputStream;
    }
}

... in your method where you perform the load operation, instantiate a new class:

MyInMemoryDestFile a = new StreamingInMemoryDestFile();

and access the output stream:

ssh.newSCPFileTransfer().download(remoteFile, a);
a.getOutputStream().toByteArray();

Regards

+5
source share
1 answer
+3

All Articles