Tuesday, September 27, 2011

Convert Byte Array to PDF in java



import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestPDF {

public static void main(String[] args) throws FileNotFoundException {
Test obj = new Test();
boolean result = obj.savePDF();
System.out.println("Result of Saving PDF : "+result);
obj.getPDF();
}
public boolean savePDF() throws FileNotFoundException{
File file = new File("c:/Java/Input_File.pdf");
FileInputStream fis = new FileInputStream(file);
Connection con = null;
try{
con = DAO.getConnection();
PreparedStatement pstmt = con.prepareStatement("Insert into test (pdf) values (?)");
pstmt.setBinaryStream(1, (InputStream)fis, (int)file.length());
int flag = pstmt.executeUpdate();
con.close();
if(flag>0)
return true;
else
return false;
}catch(Exception e){
System.out.println("Exception in saving PDF : "+e);
}
return false;
}
public void getPDF(){
Connection con = null;
InputStream fetchStream = null;
try{
con = DAO.getConnection();  //Getting the Database Connection
PreparedStatement pstmt = con.prepareStatement("select pdf from test where sl=1");
ResultSet rs = pstmt.executeQuery();
if(rs.next())
{
fetchStream = rs.getBinaryStream("pdf");
}
}catch(Exception e){
System.out.println("Exception in getting PDF : "+e);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fetchStream.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
System.out.println("read " + readNum + " bytes,");
}
System.out.println("Byte array is="+bos);
} catch (Exception ex) {
ex.printStackTrace();
}
byte[] bytes = bos.toByteArray();
File someFile = new File("c:/Java/Output_File.pdf");
try{
FileOutputStream fos = new FileOutputStream(someFile);
fos.write(bytes);
fos.flush();
fos.close();
}catch(Exception e){
}
}
}

Monday, September 26, 2011

MD5 Password Decryption


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 public class AeSimpleMD5 {
     private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        }
        return buf.toString();
    }
     public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  {
        MessageDigest md;
        md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        md5hash = md.digest();
        return convertToHex(md5hash);
    }
}


**************************************



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
public class Ex01 {
private static String convertToHex(byte[] data) {
      StringBuffer buf = new StringBuffer();
      for (int i = 0; i < data.length; i++) {
          int halfbyte = (data[i] >>> 6) & 0x0F;
          int two_halfs = 0;
          do {
              if ((0 <= halfbyte) && (halfbyte <= 9))
                  buf.append((char) ('0' + halfbyte));
              else
                  buf.append((char) ('a' + (halfbyte - 10)));
              halfbyte = data[i] & 0x0F;
          } while(two_halfs++ < 1);
      }
      return buf.toString();
  }
 
public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  {
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] md5hash = new byte[32];
      md.update(text.getBytes("iso-8859-1"), 0, text.length());
      md5hash = md.digest();
      return convertToHex(md5hash);
  }
 
 
 
 
 
    public static void main(String[] args) throws IOException {
        BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Enter string:");
        String rawString = userInput.readLine();
        try {
            System.out.println("MD5 hash of string: " + MD5(rawString));
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}




Sunday, September 25, 2011

Factorial Value


import java.math.BigInteger;

public class FactVal{
    public static void main(String[] args) {
        BigInteger a[]= new BigInteger[10];
        a[0]=BigInteger.ONE;
        a[1]=BigInteger.ONE;
        for(int i=2;i<10;i++)
        {
                String f="";
                f=f+i;
                a[i]=a[i-1].multiply(BigInteger.valueOf(i));
                System.out.println("The Factorial Number is ="+a[i]);
        }
    }
}

Saturday, September 10, 2011

Matrix Substraction

import java.io.*;
class matrixSub{
    int a[][] = new int[10][10];
    int b[][] = new int[10][10];
    int c[][] = new int[10][10];
    DataInputStream din = new DataInputStream(System.in);
    void getData(){
        try{
            System.out.println("Enter First Matics");
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    a[i][j] = Integer.parseInt(din.readLine());
                }
            }
            System.out.println("Enter Second Matrix");
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    b[i][j] = Integer.parseInt(din.readLine());
                }
            }
        }catch (Exception e){System.out.println("Exception araise "+e);}
    }
    void addition(){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                c[i][j] = a[i][j] - b[i][j];
            }
        }
    }
    void display(){
        System.out.println("The Sustracted Matrices are ");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.println(c[i][j]);
            }
        }
    }
}
public class MatrixSubstraction {
    public static void main(String args[]){
        matrixSub subm = new matrixSub();
        subm.getData();
        subm.addition();
        subm.display();
    }
}

Matrix Addition

import java.io.*;
class matrix{
    int a[][] = new int[10][10];
    int b[][] = new int[10][10];
    int c[][] = new int[10][10];
    DataInputStream din = new DataInputStream(System.in);
    void getData(){
        try{
            System.out.println("Enter First Matics");
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    a[i][j] = Integer.parseInt(din.readLine());
                }
            }
            System.out.println("Enter Second Matrix");
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    b[i][j] = Integer.parseInt(din.readLine());
                }
            }
        }catch (Exception e){System.out.println("Exception araise "+e);}
    }
    void addition(){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                c[i][j] = a[i][j]+b[i][j];
            }
        }
    }
    void display(){
        System.out.println("The added Matrices are ");
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                System.out.println(c[i][j]);
            }
        }
    }
}
public class MatrixAddition {
    public static void main(String args[]){
        matrix addm = new matrix();
        addm.getData();
        addm.addition();
        addm.display();
    }
}

Binary Search


import java.io.*;
class Binary {
    int i=0, j=0, n=0, t=0, I=0, u=0, x=0, mid=0;
    int a[] = new int[100];
    DataInputStream s = new DataInputStream(System.in);
    void getData(){
        System.out.println("Enter the Number of elements");
        try{
            n = Integer.parseInt(s.readLine());
            System.out.println("Enter the Elements");
            for(i=0 ; i<n; i++){
                a[i] = Integer.parseInt(s.readLine());
            }
        }catch(Exception e){System.out.println("the Exception Araise ="+e);}
    }
    void arrange(){
        for(i=1 ; i<n; i++){
            for(j=1;j<n;j++){
                if(a[i]>a[j]){
                    t    = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
        }
    }
    void search(){
        I = 1;
        u = n;
        System.out.println("Enter the Elements to be searched");
        try{
            x = Integer.parseInt(s.readLine());
        }catch(Exception e){System.out.println("Exception araise ="+e);}
        while(I<u){
            mid = (i+u) / 2;
            if(a[mid] == x){
                System.out.println("The Element is found on "+mid+" Location");
                System.exit(1);
            }
            else if(x > a[mid]){
                I = mid + 1;
            }
            else {u = mid-1;}
        }
        System.out.println("Element is not Found");
    }
}
public class BinarySearch{
    public static void main(String args[])throws IOException{
        Binary a = new Binary();
        a.getData();
        a.arrange();
        a.search();
    }
}

Sorting Numbers

import java.io.*;
class Ascend {
    int n,i,j,t;
    String v;
    int a[] = new int[10];
    DataInputStream s = new DataInputStream(System.in);
    @SuppressWarnings("deprecation")
    void getdata(){
        System.out.println("Enter the Number of Elements =");
        try{
            v = (s.readLine());
            n = Integer.parseInt(v);
            System.out.println("Enter the elements one by one ");
            for(i=1; i<=n; i++){
                a[i] = Integer.parseInt(s.readLine());
            }
        }catch(Exception e){
            System.out.println("Excetion Raise = "+e);
        }
    }
    void arrange(){
        for(i=1; i<=n; i++){
            for(j=1; j<n; j++){
                if(a[i] > a[j]){
                    t    = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
        }
    }
    void putdata(){
        System.out.println("The Sorted Elements are ");
        for(i=1;i<=n;i++){
            System.out.println(a[i]);
        }
    }
}
public class Ascending{
        public static void main(String args[]){
            Ascend a = new Ascend();
            a.getdata();
            a.arrange();
            a.putdata();
    }
}