Monday, March 23, 2015

Top Simplest Codes in JAVA- Dividing a file in chunks

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nwproject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Muhammad Asim
 */
public class Nwproject {

     private static String Driver_Class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String URL = "jdbc:sqlserver://localhost\\localhost (sa):1433;databaseName=Tracker_Server";
    private static String User_Name = "sa";
    private static String Password = "123";
    private static int No_Of_Chunks;
    private static String File_Size;
    private static String File_Name;
    private static String IP;
   
    public static ArrayList<File> File_List = new ArrayList<>();
   
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Socket Server = new Socket("localhost", 9999);
        Recieve_Files(Server);
        Merge_Files();
        Server.close();
        Data_Shared_With_DataBase();
        Update_Tracker_Server();
    }

    private static void Recieve_Files(Socket Server) throws IOException {
        int File_Size = 262144,File_Index = 0;
            BufferedInputStream IS =new BufferedInputStream (Server.getInputStream());
            File newFile ;
            FileOutputStream ChunkOS ;
            BufferedOutputStream ChunkBOS = null  ;
            byte[] Byte_Array = new byte[File_Size];
            int tmp;
               
            while ( (tmp = IS.read(Byte_Array,0,File_Size)) != -1) {
                newFile = new File("C:\\Users\\MuhammadKhaliq\\Desktop\\hi",  "ChunkReceived" + String.format("%03d", ++File_Index));     //making file chunks
                ChunkOS   = new FileOutputStream(newFile);
                ChunkBOS = new BufferedOutputStream(ChunkOS);
                ChunkBOS.write(Byte_Array);
                File_List.add(newFile);
            }
           
           
            ChunkBOS.flush();
            ChunkBOS.close();
           
        }
   

    private static void Merge_Files() throws FileNotFoundException, IOException {
        FileOutputStream ChunkOS = new FileOutputStream("C:\\Users\\MuhammadKhaliq\\Desktop\\receive.mp3");       //Merge the chunks to destination with name
        BufferedOutputStream ChunkBOS = new BufferedOutputStream(ChunkOS);
        for (File f : File_List) {
            Files.copy(f.toPath(), ChunkBOS);
        }
    }

    private static void Update_Tracker_Server() {
        Connection conn = null;
        try {
            Class.forName(Driver_Class);
            conn = DriverManager.getConnection(URL, User_Name, Password);
            System.out.println("Connected!");
            String query = "{ call Add_Data( ?, ?, ?, ? )}";
            CallableStatement con = conn.prepareCall(query);
            con.setString(1, IP);
            con.setString(2, File_Name);
            con.setString(3, File_Size + " KB");
            con.setInt(4, No_Of_Chunks);
            con.executeUpdate();
        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println("Error!" + ex + "");
        }
    }

    private static void Data_Shared_With_DataBase() throws UnknownHostException {
        File file = new File("C:\\Users\\MuhammadKhaliq\\Desktop\\receive.mp3");
        File_Name = file.getName();
        double Size = file.length() / 1024;
        File_Size = String.valueOf(Size);
        No_Of_Chunks = File_List.size();
        InetAddress ip = InetAddress.getLocalHost();
        IP = ip.getHostAddress();
    }
}

No comments:

Post a Comment