Thursday, June 23, 2011

Java BuzzWords - 2


Object-Oriented

This is, unfortunately, one of the most overused buzzwords in the industry. But object-oriented design is very powerful because it facilitates the clean definition of interfaces and makes it possible to provide reusable "software ICs."

Simply stated, object-oriented design is a technique that focuses design on the data (=objects) and on the interfaces to it. To make an analogy with carpentry, an "object-oriented" carpenter would be mostly concerned with the chair he was building, and secondarily with the tools used to make it; a "non-object-oriented" carpenter would think primarily of his tools. Object-oriented design is also the mechanism for defining how modules "plug and play."

The object-oriented facilities of Java are essentially those of C++, with extensions from Objective C for more dynamic method resolution.

How to Do Hashing String with SHA-256

It will use SHA-256 hashing algorithm to generate a hash value for a password “123456″.

package org.best.example;
 
import java.security.MessageDigest;
 
public class SHAHashingExample 
{
    public static void main(String[] args)throws Exception
    {
     String password = "123456";
 
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes());
 
        byte byteData[] = md.digest();
 
        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
 
        System.out.println("Hex format : " + sb.toString());
 
        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
     for (int i=0;i<byteData.length;i++) {
      String hex=Integer.toHexString(0xff & byteData[i]);
          if(hex.length()==1) hexString.append('0');
          hexString.append(hex);
     }
     System.out.println("Hex format : " + hexString.toString());
    }
}
Output
Hex format : 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
Hex format : 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

Wednesday, June 22, 2011

How to Do Java SHA Hashing

SHA-2 is believe the most secure hashing algorithm as this article is written, here are few examples for the SHA implementation. The possible MessageDigest algorithm are SHA-1, SHA-256, SHA-384, and SHA-512, you can check the reference for the detail.It will use SHA-256 hashing algorithm to generate a checksum for file “c:\\loging.log”.

package org.best.example;
 
import java.io.FileInputStream;
import java.security.MessageDigest;
 
public class SHACheckSumExample 
{
    public static void main(String[] args)throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        FileInputStream fis = new FileInputStream("c:\\loging.log");
 
        byte[] dataBytes = new byte[1024];
 
        int nread = 0; 
        while ((nread = fis.read(dataBytes)) != -1) {
          md.update(dataBytes, 0, nread);
        };
        byte[] mdbytes = md.digest();
 
        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
          sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
 
        System.out.println("Hex format : " + sb.toString());
 
       //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
     for (int i=0;i<mdbytes.length;i++) {
       hexString.append(Integer.toHexString(0xFF & mdbytes[i]));
     }
 
     System.out.println("Hex format : " + hexString.toString());
    }
}
 
Output
 
Hex format : 21a57f2fe765e1ae4a8bf15d73fc1bf2a533f547f2343d12a499d9c0592044d4
Hex format : 21a57f2fe765e1ae4a8bf15d73fc1bf2a533f547f2343d12a499d9c0592044d4 

Java BuzzWords - 1


Simple

We wanted to build a system that could be programmed easily without a lot of esoteric training and which leveraged today's standard practice. Most programmers working these days use C, and most programmers doing object-oriented programming use C++. So even though we found that C++ was unsuitable, we designed Java as closely to C++ as possible in order to make the system more comprehensible.

Java omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. These omitted features primarily consist of operator overloading (although the Java language does have method overloading), multiple inheritance, and extensive automatic coercions.

We added automatic garbage collection, thereby simplifying the task of Java programming but making the system somewhat more complicated. A common source of complexity in many C and C++ applications is storage management: the allocation and freeing of memory. By virtue of having automatic garbage collection (periodic freeing of memory not being referenced) the Java language not only makes the programming task easier, it also dramatically cuts down on bugs.
Another aspect of being simple is being small. One of the goals of Java is to enable the construction of software that can run stand-alone in small machines. The Java interpreter and standard libraries have a small footprint. A small size is important for use in embedded systems and so Java can be easily downloaded over the net.

Tuesday, June 21, 2011

How to LinkedList in Java

In this example, you will see the use of java.util.LinkedList class. We will be creating an object of link list class and performing various operation like adding and removing object.
This class extends AbstractSequentialList and implements List, Cloneable, Serializable. It permits all elements including null. LinkedList class provides methods get, insert and remove an element at the beginning and end of the list.
In this example six methods of LinkedList class is demonstrated.
add(Object o): Appends the specified element to the end of this list. It returns a boolean value.
size(): Returns the number of elements in this list.
addFirst(Object o): Inserts the given element at the beginning of this list. addLast(Object o): Inserts the given element at the last of this list.
add(int index,Object o): Insert the specified element at the specified position in this list. It throws IndexOutOfBoundsException if index is out of range.
remove(int index): Remove the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException if index is out of range. 


import java.util.*;

public class LinkedListDemo{
 public static void main(String[] args){
  LinkedList link=new LinkedList();
  link.add("a");
  link.add("b");
  link.add(new Integer(10));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
  
  link.addFirst(new Integer(20));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.addLast("c");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(2,"j");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(1,"t");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.remove(3);
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
 }
}

Java BuzzWords


Java

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language,Distributed.
One way to characterize a system is with a set of buzzwords. We use a standard set of them in describing Java. Here's an explanation of what we mean by those buzzwords and the problems we were trying to solve.

Monday, June 20, 2011

How to get the file creation date in Java

There are no official way to get the file creation date in Java. However, you can use the following workaround to get the file creation date in Windows platform.

package org.best.example.file;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class GetFileCreationDateExample
{
    public static void main(String[] args)
    { 
 
     try{
 
      Process proc = 
         Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
 
      BufferedReader br = 
         new BufferedReader(
            new InputStreamReader(proc.getInputStream()));
 
      String data ="";
 
      //it's quite stupid but work
      for(int i=0; i<6; i++){
       data = br.readLine();
      }
 
      System.out.println("Extracted value : " + data);
 
      //split by space
      StringTokenizer st = new StringTokenizer(data);
      String date = st.nextToken();//Get date
      String time = st.nextToken();//Get time
 
      System.out.println("Creation Date  : " + date);
      System.out.println("Creation Time  : " + time);
 
     }catch(IOException e){
 
      e.printStackTrace();
 
     }
 
    }
}