Sunday, August 18, 2013

Java New IO 2.0

The New IO is one of the features introduced in Java7. A new package java.nio has been added as part of this feature. Even though the usage wise it's same as java.io, most of the issues in java.io has been addressed. We will see the features introduced in New IO (NIO)
Path
The package java.nio.file consists of classes and interfaces Path, Paths, FileSystem, FileSystems and others. Each of represent's the file path or file system as is.
java.nio.file.Path works exactly same as java.io.File with additional features.
Path path = Paths.get("C:\\temp\\sample.txt");
System.out.println("Name Count : "+path.getNameCount());
System.out.println("Parent     : "+path.getParent());
System.out.println("Root       : "+path.getRoot());
The output will be
Name Count : 2
Parent     : C:\temp
Root       : C:\
Files
Path can also be used for deleting. There are two delete methods. One delete method can throw NoSuchFileException if the file doesn't exist.
Files.delete(path);
Where as other deletes the file if exists.
Files.deleteIfExists(path);
There are copy, move, create (Both for directories and files) with options. There are mthods to create symbolic links, temporary directories as well.
WatchService
This is the service which we can get the file change notifications like delete, update, create, change etc. WatchService API makes to receive notifications on a file or directory.
   Path path = Paths.get("C:\\Temp");
   WatchService service = path.getFileSystem().newWatchService();
   path.register(service, StandardWatchEventKinds.ENTRY_CREATE,
            StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY);
   WatchKey watckKey = service.take();
   List<watchevent>&gt; events = watckKey.pollEvents();
   for(WatchEvent event : events)
   {
       System.out.println("Event : "+event.kind().name()+"; Context : "+event.context());
   }
The steps for creating a WatchService are
  • Create WatchService instance using path
  • Register required events (Create, delete and modify are available) on the path
  • Get the watch key and poll for the events. (For continuously watching the events, put it in infinite loop).
Happy Learning!!!

Thursday, August 15, 2013

What's in .Class file - Contd.

This is continuation of post. Now, we will see bit in detail with an example
package com.test;
public class Employee
{
     .....
     public void setEmpid(int empid)
     {
          this.empid = empid;
     }
     ....    
     public double getSalary()
     {
          return salary;
     }
     public void setSalary(double salary)
     {
          this.salary = salary;
     }
}
For example, take the above block's byte code one by one
public void setEmpid(int);
  Code:
   0:   aload_0
   1:   iload_1
   2:   putfield        #2; //Field empid:I
   5:   return
The code is pretty readable. load means loading a value. Each load is prefixed by a character ( 'i' means integer, 'd' means decimal and 'a' means an object. putfield means setting a value to other variable. Each function will have a return statement at the end. If the function has a void return type, then simple return otherwise return will be prefixed by a character. For example getSalary() method as below
public double getSalary();
  Code:
   0:   aload_0
   1:   getfield        #4; //Field salary:D
   4:   dreturn
Each and every line in the java byte code is called opcode as we discussed has a special meaning. We can find the list of all opcodes in the JVM Spec or on Wiki. Apart from the load, put, get, return, new (That we saw). There are method invocations which you will find more common. These start with invoke prefix
  • invokespecial: Used to call the instance private as well as public method (including initializations)
  • invokevirtual : Used to call a method based on the class of the object. 
  • invokestatic : Invokes a static method. 
  • so on....
Will post some more details in the next posts.