Thursday, October 16, 2008

Getting updates in a file over network

Faster than getting the actual diff every time:


URL logfile;
try {
logfile = new URL("url-to-file");
URLConnection uc = logfile.openConnection();
uc.setUseCaches(false);
uc.setConnectTimeout(10000);
uc.setRequestProperty("Cache-Control","no-cache");
uc.setRequestProperty("Keep-Alive", "5000");
String userPassword = "user:pswd";
Base64Converter b64c = new Base64Converter();
String encoding = b64c.encode(userPassword.getBytes());
uc.setRequestProperty("Authorization","Basic "+encoding);
uc.setRequestProperty("Content-Type","application/octet-stream");

int contentlength = uc.getContentLength();

//Do something that updates the file or wait till some other process updates it

URLConnection uc2 = logfile.openConnection();
uc2.setRequestProperty("Authorization","Basic "+encoding);
uc2.setRequestProperty("Content-Type","application/octet-stream");
InputStream reis = uc2.getInputStream();
BufferedReader newbr = new BufferedReader(new InputStreamReader(reis));
String temp_str = "";
String new_file = "";
newbr.skip(contentlength);
while((temp_str = newbr.readLine()) != null){
new_file += temp_str + "\n";
}
System.out.printl("Update:");
System.out.println(new_file);

Monday, October 13, 2008

diffing files over network in Java (J2EE)

Here's a way to diff files over network in Java.
It uses java.net.URLConection - twice.

Its basically a block diff - you won't get a line to line diff
This is more useful if you are checking the same file after sometime and getting the update in it. only - see to it that you don't get the cached version of the same file! or you wont get any diff!

The total time for execution depends on the size of files you're diffing.



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class file_diff {
/**
* @param args
*/
public String getFileData(String file_url, String uname, String pswd){
try{
URL file = new URL(file_url);

URLConnection uc = file.openConnection();

uc.setUseCaches(false);
uc.setConnectTimeout(10000);
uc.setRequestProperty("Cache-Control","no-cache");

String userPassword = uname+":"+pswd;
Base64Converter b64c = new Base64Converter();
String encoding = b64c.encode(userPassword.getBytes());
uc.setRequestProperty("Authorization","Basic "+encoding);

uc.setRequestProperty("Content-Type","application/octet-stream");

InputStream is = uc.getInputStream();
String data_file = "", temp_str;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
System.out.println(uc.getHeaderFields().toString());
System.out.println("Readig file...");
while((temp_str = br.readLine()) != null){
data_file += temp_str;
}
System.out.println("Closing file");
is.close();
br.close();
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (data_file);

}
public static void main(String[] args) {
file_diff obj = new file_diff();
String data_file1 = obj.getFileData("");
String data_file2 = obj.getFileData("");
System.out.println("Difference: ");
if(data_file2.contains(data_file1)){
System.out.println(data_file2.substring(data_file1.length()));

}

}