Friday, October 21, 2011

Work-Offline Firefox plugin

As I was going on vacation where there is a limited internet connectivity, I thought it would be great if I could get read some of the webpages offline. But I did not want to save the pages on local manually. I found a good Firefox plugin for it - work offline.

https://addons.mozilla.org/en-US/firefox/addon/work-offline/

Its really useful - when in offline mode, it serves the page if its available in its cache - similar to what is offered by IE's Work Offline feature.
So good for the Firefox lovers.

Friday, October 14, 2011

Compressing and Decompressing String in Java using GZIP streams

Following code can be used to compress or decompress any data in Java using the GZIP streams - java's built in zip streams.
    public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        System.out.println("String length : " + str.length());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        String outStr = out.toString("ISO-8859-1");
        System.out.println("Output String lenght : " + outStr.length());
        return outStr;
     }
    
    public static String decompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        System.out.println("Input String length : " + str.length());
        GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("ISO-8859-1")));
        BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "ISO-8859-1"));
        String outStr = "";
        String line;
        while ((line=bf.readLine())!=null) {
          outStr += line;
        }
        System.out.println("Output String lenght : " + outStr.length());
        return outStr;
     }
  
     public static void main(String[] args) throws IOException {
        String filePath = ".\response.txt";
        
        String string = getFileData(filePath);
        System.out.println("after compress:");
        String compressed = compress(string);
        System.out.println(compressed);
        System.out.println("after decompress:");
        String decomp = decompress(compressed);
        System.out.println(decomp);
 
      }
     
     public static String getFileData(String filepath) throws FileNotFoundException,
                                                           IOException {
       BufferedReader bf = new BufferedReader(new FileReader(filepath));
       String data="";
       String line;
       while ((line=bf.readLine())!=null) {
         data += line;
       }
       return data;
     }

Thursday, September 29, 2011

Location of java class file used by Application


Sometimes, while debugging the code, you will be wondering that which class is being picked by Java application from the current Classpath.
Here is some small code snippet to know the location of the class file.
Very simple, it just tries to get the system resource for the class file.

package test.java;

public class ClassSource {

    public static String getClassLocation(String fullQualifiedclassName) throws ClassNotFoundException {
        Class cls = Class.forName(fullQualifiedclassName);
        String classPath = fullQualifiedclassName.replace('.','/').concat(".class");
        if (cls!=null && cls.getClassLoader()!=null) {
            return cls.getClassLoader().getResource(classPath).toString();
        } else {
            return ClassLoader.getSystemResource(classPath).toString();
        }
          
    }
    
    public static void main(String[] args) throws Exception {
        System.out.println("Class Object source : " + getClassLocation("java.lang.Object"));
        System.out.println("Current Class : " + getClassLocation("test.java.ClassSource"));
    }
    
}

Output on my machine:

Class Object source : jar:file:/L:/Program%20Files/Java/jre1.5.0_09/lib/rt.jar!/java/lang/Object.class
Current Class : file:/I:/work/eclipse-workspace/Java-Test/classes/test/java/ClassSource.class

Tuesday, September 27, 2011

Redirecting or Ignoring System.out.println outputs

Recently one of my friend had following requirement. I guess it can be useful to anyone.
In case you want to redirect all the System.out.println() output to some file, you can do using following methods provided by System class.
System.setOut(), System.setIn() and System.setErr()

So, to redirect the output and error logs, you can simply say
    String outFile = "/tmp/sysout.log";
    System.setOut(new PrintStream(new FileOutputStream(outFile)));
    String errFile = "/tmp/syserr.log";
    System.setOut(new PrintStream(new FileOutputStream(errFile)));

Sometimes its required to hide all System.out.println outputs when the existing application is unnecessarily writing lot of outputs filling the system space and it would take some time to modify all the code. To do this, you can subclass PrintStream and override its methods for no-op.

    System.out.println("Hello, program started.");
    String filename = "D:\\sysout.log"; //or put some dummy filepath, need to this to construct PrintStream
    System.setOut(new PrintStream(new FileOutputStream(filename)) {
   
                @Override
                public void print(String paramString) {
                  //do nothing
                }

                @Override
                public void println(String paramString) {
                  //do nothing
                }
                
                //above will just hide the output for print(String) and println(String) methods.
                //override all other print/println methods so nothing gets printed at all.
              }
            );
    System.out.println("This is another string in sysout.");