Java Virus Scanner

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Java Virus Scanner

Post by Nipuna » Mon Feb 20, 2012 8:25 am

As usual I found this online :) And didn't tried but read a very little of it and it seems a little code and I can understand very little because I am still learning Java. After that I hope I will be able to read almost any Java code :)

But this code has a little problem that is, the programmer who wrote this code hasn't made any comment how is this code is working

Code: Select all

package edu.learn.upload.virus.scanner;

import java.io.IOException;
import java.util.Scanner;

public class VirusScanner {
        public static String fileScanner(String path) {
                String fileStatus = "";
                try 
                {
                        Process process = Runtime.getRuntime().exec("C:/Progra~1/ClamWin/bin/clamscan.exe " + path);
                        Scanner scanner = new Scanner(process.getInputStream());
                        while(scanner.hasNextLine()){
                                String status = scanner.nextLine();
                                if((status.lastIndexOf(":",1)) != -1){
                                        fileStatus = status.substring(status.lastIndexOf(":") + 1);
                                        System.out.println("Filename: " + status.substring(0, status.lastIndexOf(":")) + "\nStatus: " + status.substring(status.lastIndexOf(":") + 1));
                                }
                        }
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                
                if(fileStatus.trim().toLowerCase().equals("ok")){
                        return  "OK";
                }else{
                        return fileStatus.trim().toLowerCase();
                }
        }
        
        public static void main(String[] args) {
                System.out.println(fileScanner("C:/temp/add_state_reuse.txt"));
        }
}

User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Java Virus Scanner

Post by Nipuna » Mon Feb 20, 2012 8:29 am

Syntax highlighting testing

Code: Select all

class ArrayTable{
	public static void main(String args[]){
        
		System.out.println("Index \tValue");
		int array[] ={1,2,3,4,5,6,7,8};
        
		for(int counter = 0; counter < array.length; counter++ ){
            
			System.out.println(counter + "\t" + array[counter]);
            
		}
	}
}

We have a little problem with syntax highlighting, I am going to inform Neo.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Java Virus Scanner

Post by SemiconductorCat » Mon Feb 20, 2012 1:37 pm

>> After that I hope I will be able to read almost any Java code :)

unfortunately the things won't work extract in that way. I'm not a experienced developer but I have seen
even experienced developers also turn out the book and re-refer the API's and Docs even the simple common
once like "CreateProcess". If you can keep them in mind it's oky. But don't force it or push it.It won't work.
APIS are there for some reason. Instead learn to read the API's and Docs. make reading the API's and Docs
is your hobby.

There some set of skills that you also need to develop too, if you want to be a programmer learn how to
extract information from books and docs in very little time. Such soft skills like , google hacking,
using the glossaries etc etc.Structures of API and how they normally organized.Learn to it and be use to
it.


So in your program start with the main first.

Code: Select all


      public static void main(String[] args) {
                System.out.println(fileScanner("C:/temp/add_state_reuse.txt"));
        }
so it will call fileScanner() , now find where is fileScanner() is, When you want to find a method like this,
follow the extract method that compiler is using , first it searches in the local scope, then class scope, then
global scope [in Java there's no global scope]. If your using a IDE your IDE may have a search tag.

so I analyze the code this time for you, next time you have to DIY.
please follow my comments .

Code: Select all

public class VirusScanner {
        public static String fileScanner(String path) {
                String fileStatus = "";    // can't understand this ? 
                try // creates a try block 
                {
                        Process process = Runtime.getRuntime().exec("C:/Progra~1/ClamWin/bin/clamscan.exe " + path);
                        // the return value of the right hand side will be stored in the process.
                        // so you have to , read the what is 'Process' first. 
                        // Here is the API for that class http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

                        // then go to the expression , Runtime.getRuntime().exec() function is caleld with that parameter.

                         // so what's Runtime , Just search on API,
                         // API DOC : http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#getRuntime%28%29
                        // so Here is the API for getRuntime() is a static method in that class, so you 
                        // call that function without making a object.
                        // http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#getRuntime%28%29
                        
                         // so it will return a Runtime Object. and that .exec() function will call.
                         // so we need to refer to the API of Runtime class's exec() method.
                         // API : http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29

                        // so to that function it will pass the string parameter.
                        // path is now path= "C:/temp/add_state_reuse.txt" and there is a string concatration in the 
                        // line. so parameter that will be passed to exec() will be 
                        // "C:/Progra~1/ClamWin/bin/clamscan.exe  C:/temp/add_state_reuse.txt"
                        
                       //  so finally you can understand the line. It just create a new process 
                       // with command line parameter.

                        Scanner scanner = new Scanner(process.getInputStream());
                        // likewise review the appropriate API docs. If you unfamiliar , which means it's
                        // necessary to read it. More reading more skills will rise up, Believe me.
                        // Scanner class , and Process's (type of object process ) getInputStream() method API.
                        
                        while(scanner.hasNextLine()){
                                // when you analyzing a while loop divide it to two logical parts,
                                // when will the termination condition will met, you can see it will
                                // exit the loop when there is no more lines to process.

                                // then flawn back to the loop context.
                                // what it does? Can you tell me?
                                // I could tell what it does without reviewing any API, believe me experience will
                                // made you to that[anyway I'm not that much of experienced].
                                String status = scanner.nextLine();
                                if((status.lastIndexOf(":",1)) != -1){
                                        fileStatus = status.substring(status.lastIndexOf(":") + 1);
                                        System.out.println("Filename: " + status.substring(0, status.lastIndexOf(":")) + "\nStatus: " + status.substring(status.lastIndexOf(":") + 1));
                                }
                        }
                }
                // the routine of exception routine.
                catch (IOException e) {
                        e.printStackTrace();
                }
               
                if(fileStatus.trim().toLowerCase().equals("ok")){
                        return  "OK";
                }else{
                        return fileStatus.trim().toLowerCase();
                }
        }


The code above is well commented. do you wonder if I say that? No it is. Only comment the the questions that
you will get when you reading the code. Too much comment is bad too. Comment what your code won't say
implicitly to your mind when you reading it.For a example not obvious filed in a structure.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Java Virus Scanner

Post by Nipuna » Mon Feb 20, 2012 4:10 pm

Wow Thanks man :clap: great comments you have there.

And I need to tell some more stuff that I forgot to post before.

1. By any code, I didn't mean all the codes in the world which is written in Java :) I do know we need to refer API and don't need to memorize every stuff in the API that's why API is for if not they will be in books for us to remember. I do know no one can even an experienced programmer can understand some codes without API ,internet or any other.
2. I know we need to start reading from the main method and look for any methods or objects in the way you said.
3. Also I already remembered that we don't need to comment everything, Like we don't need to comment about variable declaring and etc.. even a noob know very simple stuff.


Thank you very much. (I honestly thank you, for the time you spent for this and for ideas) This is a great help for learners like me.
Post Reply

Return to “Java Programming”