user608020
user608020

Reputation: 313

Compute checksum of remote file in Java

I am using commons VFS to transfer files over sftp. Once copying is done, i want to compare the checksum of source and destination file.

How can i find the checksum of remote file in java ? For local file,i am using FileUtils.checksum().

Upvotes: 2

Views: 2114

Answers (1)

ethanfar
ethanfar

Reputation: 3778

It depends on which check sum implementation you're using (e.g. CRC32, etc.).

Let's take CRC32 as an example. You create an instance of CRC32, and update it with more and more bytes. This is a perfect fit for the task you're trying to do, since you can update whenever you have more bytes available in the input stream.

Here's a half-baked exanple:

public long checksumRemote(InputStream inputStream) {
    CRC32 checksum = new CRC32();
    boolean finished = false;
    byte[] buffer = new byte[4096];

    while (!finished) {
        int bytesRead = inputStream.read(buffer);
        if (bytesRead < 0) {
            finished = true;
        } else if (bytesRead > 0) {
            checksum.update(buffer, 0, bytesRead);
        }
    }

    return checksum.getValue();
}

Upvotes: 1

Related Questions