Question

Your task is to write a multithreaded program (using persistent threads) for calculating and printing out...

Your task is to write a multithreaded program (using persistent threads) for calculating and printing out the MD5 hash values of the files in a directory (or a even a single file). Your program should accept as a command-line parameter the directory to examine. Example: $ ./dirHash Documents You should use the producer-consumer pattern for setting-up the communication between your main and your worker threads.

using java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is the sample code for generating the MD5 for files present in any given directory.

procedure :

1) First, get the list of all files present in the directory.

2) Then we have a function called convertFileToMD5() for generating MD5.

3) Input for this can be a file path for directory path, if it is a file path, then it will directly generate the md5 values for the file and for the directory it will generate the md5 values for all files present.

code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Scanner;

public class createMd5 {
   public static String convertFileToMD5(String filePath) {
       InputStream inputStream = null;
       try {
           inputStream = new FileInputStream(filePath);
           byte[] buffer = new byte[1024];
           MessageDigest digest = MessageDigest.getInstance("MD5");
           int numRead = 0;
           while (numRead != -1) {
               numRead = inputStream.read(buffer);
               if (numRead > 0)
                   digest.update(buffer, 0, numRead);
           }
           byte[] md5Bytes = digest.digest();
           return convertHashToString(md5Bytes);
       } catch (Exception e) {
           return null;
       } finally {
           if (inputStream != null) {
               try {
                   inputStream.close();
               } catch (Exception e) {
               }
           }
       }
   }

   private static String convertHashToString(byte[] md5Bytes) {
       String returnVal = "";
       for (int i = 0; i < md5Bytes.length; i++) {
           returnVal += Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16)
                   .substring(1);
       }
       return returnVal.toUpperCase();
   }

   private static ArrayList<String> getAllFilePath(String dir)
           throws IOException {
       File folder = new File(dir);
       File[] listOfFiles = folder.listFiles();
       ArrayList<String> filePaths = new ArrayList();
       for (int i = 0; i < listOfFiles.length; i++) {
           if (listOfFiles[i].isFile()) {
               filePaths.add(listOfFiles[i].getCanonicalPath());
           } else if (listOfFiles[i].isDirectory()) {
           }
       }

       return filePaths;
   }

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the path for directory:");
       String path;
       path = s.next();
       try {

           File givenPathFile = new File(path);
           if (givenPathFile.isDirectory()) {
               ArrayList<String> filePaths = getAllFilePath(path);
               for (int i = 0; i < filePaths.size(); i++) {

                   String md5 = convertFileToMD5(filePaths.get(i));
                   if (md5 != null)
                       {
                       System.out.println("file:"+filePaths.get(i)+"\nMD5:"+md5);
                       }
                   else
                       System.out.println("Not valid path");

               }
           } else {
               String md5 = convertFileToMD5(givenPathFile.getCanonicalPath());
               if (md5 != null)
                   System.out.println("file:"+givenPathFile.getCanonicalPath()+"\nMD5:"+md5);
               else
                   System.out.println("Not valid path");
           }

       } catch (Exception e) {
           e.printStackTrace();
           System.out.println("Not valid path");
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Your task is to write a multithreaded program (using persistent threads) for calculating and printing out...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following multithreaded program, use system level scope for thread scheduling. Submit your program and...

    For the following multithreaded program, use system level scope for thread scheduling. Submit your program and screenshot of output in a single word/pdf file. Write a multithreaded program (USING C PROGRAM) that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the...

  • Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive...

    Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive integers from the command line as the amounts to withdraw a. and deposit from an account. b. Creates two threads to perform withdraw and deposit operations repectively. Passes the amount to withdraw/deposit as the parameter to thread's runner function c. Waits for both threads to terminate. Since both threads need to update the account balance, it is necessary to protect sections. You may use...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Prelab Exercises Your task is to write a Java program that will print out the following...

    Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...

  • Program C: Start by printing out your name, then your code should prompt the user to...

    Program C: Start by printing out your name, then your code should prompt the user to enter integers using the following prompt: Enter some integers, end with a 0: Your code should: Level 1 Task: count the number of integers entered (not including the 0) Level 2 Task: produce the sum of all of the integers Level 3 Task: produce the sum of the even integers These calculated values need to be displayed as follows: There were ___ numbers read...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • Question III This question carries 20% of the marks for this assignment. Given the following mix...

    Question III This question carries 20% of the marks for this assignment. Given the following mix of tasks, task lengths and arrival times, compute the completion [5 marks and response time time from the arrival to the finish time) (5 marks for each task, along with the average response time for the FIFO. RR and SJF algorithms. Assume a time slice of 10 milliseconds and that all times are in milliseconds. You are kindly asked to provide the Gantt Chart...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically,...

    Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically, the program will first determine the number of files to be processed. Then, the program will createmultiple threads where each thread is responsible for one file to count the number of words appeared in the file and report the number of time each word appears in a global linked-list....

  • CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and sh...

    *Help Please with the code** CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT