Reputation: 5504
Checking whether a file exists is easy with JCIFS:
import jcifs.smb.*;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
SmbFile file = new SmbFile("smb://server/filepath", auth);
if( file.exists() ) { ... }
Now suppose you want to put all of this in a loop and check whether various files exist. I assume that in such scenario new SmbFile("smb://server/filepath", auth);
will set up a new connection for each file. Am I correct?
So my question is: what is the least resource intensive way of testing for file existence over SMB?
I was thinking about letting file
point to the SMB root, and then use file.listFiles(java.lang.String wildcard)
to check for file existence. But the problem is that listFiles
doesn't work recursively. I can wrap it and make it recursive, but that wouldn't be clean.
Upvotes: 2
Views: 5131
Reputation: 94584
In response to:
new SmbFile("smb://server/filepath", auth);
will set up a new connection for each file
The connection is not established until you actually try to do something against the server (e.g. check existence). However, it uses the same connection to talk to the server for the operations. The SmbTransport
class is the thing that deals with this. You can use wireshark or any other monitoring tool to see that the operations use the same connection to the server.
So, in response to 'what is the least resource intensive way of checking the existence of a file', you check that it exists.
If you're checking for multiple files, and you already know their names, then just checking existence is still the most efficient way.
If you don't know the names of the files, and you use listFiles
(and child directory descent), then you're going to be asking for significantly more data from the server e.g. per-entry attributes for items if they're a directory or not, directories with hundreds, perhaps thousands of entries, which can amount to a lot of data to cache while walking the tree.
Upvotes: 2