A Javery journey – part one
I recently had the misfortune (or so I thought) of needing to write a Java applet to achieve a bit functionality in a browser that really wouldn’t be possible using anything else.
It didn’t sound particularly hard in principal, but these things never do (before you start out), and besides there’d be loads of Googly help out there.
But help-a-plenty and understanding it enough to apply it to a rudimentary understanding of a new language in some way negates its helpfulness.
In every language there a things that are unexpected and cause confusion just because they are unexpected.
Step one – find the file
The first step was to get it to read a file from any currently “mounted” drive on the client machine – which was supposed to be easy, and in theory it is:
File.listRoots();
This returns an object with all the drives contained therein, which could then be munched through and the file easily tested for existence – sounds like a plan – hence:
String file;
for (File drive : File.listRoots()) {
String filepath = drive + "\\" + FILE_NAME;
if (file == null) {
Boolean exists = new File(filepath).exists();
if (exists) {
file = filepath;
}
} else {
break;
}
}
For Windows, that turned out to be fine, but not using it I could only test it on a VM. What about nix based systems (Mac, Linux etc)? The listRoots() above returned a single forward slash – what about other mounts points.
The only reference I could find was a post mentioning that you’re left with exec’ing a system command. That meant regex’s – arrrrrrrg, Java regex’s.
The following is the best I could come up with – exec a df -a, read through the returned text, pattern match, then test for the files existence. Easy – if only Java regex’s worked like everything else.
String line;
String file;
try {
Pattern pattern = Pattern.compile("/[a-zA-Z0-9/]+$",Pattern.CASE_INSENSITIVE);
Process process = Runtime.getRuntime().exec("df -a");
InputStream input = process.getInputStream();
BufferedReader buffRead = new BufferedReader(new InputStreamReader(input));
readline:
while ((line = buffRead.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
Boolean result = matcher.find();
while (result) {
String mountPoint = matcher.group(0);
if (mountPoint.equals("/dev") || mountPoint.equals("/net")) continue readline;
String filepath = mountPoint + "/" + FILE_NAME;
if (file == null) {
Boolean exists = new File(filepath).exists();
if (exists) {
file = filepath;
}
} else {
break;
}
result = matcher.find();
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
That was the first hurdle – damn frustrating. I’m not sure why the Pattern object works like it does, there’s probably plenty of good reasons. I guess in some way, having a matching object that can then be instructed step through each match is the same as having an array of matched objects that would need to be stepped through anyway.
This will definitely continue full of all the other Javery joys I journeyed through.
The next part is here.
Pages





