Solved

IOException

Posted on 2011-02-28
7
439 Views
Last Modified: 2012-05-11
I am currently developing a cache cohrence protocol in java but I am having an IOException when reading my trace file and i have no idea why. Below is the codes generating the error.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;


public class CacheSim {
	public int simIndex;
	public Hashtable<Integer, String> myCache; 
	public int notValid;													
	public static int hRate;
	public static int mRate;
	public static int noProcessors = 4;
	private static String processor;
	private static String state;
	private static String word;
	private static int cacheLocation;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CacheSim cacheSim = new CacheSim();
		File traceFile = new File("C:\\Users\\mjarju\\Documents\\Uni Stuff\\Course Work\\Prallel Architecture\\Assignments\\Assignment2\\memtrace.txt");
	    FileInputStream takeInTrace = null;
	    BufferedInputStream streamData = null;
	    DataInputStream streamOperation = null;
	    boolean switch_off = false;
	    String state_value;
	    CacheSim []proc=new CacheSim[noProcessors];

	    try {
	      takeInTrace = new FileInputStream(traceFile);

	      // Here BufferedInputStream is added for fast reading.
	      streamData = new BufferedInputStream(takeInTrace);
	      streamOperation = new DataInputStream(streamData);
	      String readLine;

	      // dis.available() returns 0 if the file does not have more lines.
	      while (streamOperation.available() != 0) {
	    	  readLine = streamOperation.readLine();
	    	  
	    	  //========================================================
	    	  
	    	  if(readLine.length()>1){	    		  
	  	         processor = readLine.substring(0,2);	        
	  	         state = readLine.substring(3,4);
	  	         word = readLine.substring(5);
	  	         cacheLocation = Integer.parseInt(word);
	  	    	}

				if(readLine.length()==1)
				{


					if(readLine.equals("i"))
					{
						System.out.println("Total number of Invalids"+ " " + cacheSim.notValid);
						for(int i=0;i<noProcessors;i++)
							System.out.println("Invalidation on Processor "+ i+ " = "+ proc[i].simIndex);
					}
					else if(readLine.equals("h"))
					{
						System.out.println("hit rate is"+" "+hRate);
						System.out.println("miss rate is"+" "+mRate);
					}
					if(readLine.equals("v"))
					{
						if(switch_off==true)
						{
							switch_off=false;
						}
						else
						{
							switch_off=true;
						}
					}
					if(readLine.equals("p"))
					{
						System.out.println(proc[0].myCache);
						System.out.println(proc[1].myCache);
						System.out.println(proc[2].myCache);
						System.out.println(proc[3].myCache);
					}
				}
				else
				{ 
					if(state.equals("l"))
					{
						state_value="loads";
					}
					else
					{
						state_value="store";
					}
					if(switch_off==true)
					{
						System.out.println("a"+ " "+ state_value + "form"+state+ "to word"+word );
					}
					
	    	  //==============================================================

	      // this statement reads the line from the file and print it to
	        // the console.
//	        System.out.println(streamOperation.readLine());
//	    	  if(readLine.length()>1){	    		  
//	        String processor = readLine.substring(0,2);	        
//	        String state = readLine.substring(3,4);
//	        String word = readLine.substring(5);
//	        int cacheLocation = Integer.parseInt(word);
//	    	  }
	      }	 
	      // dispose all the resources after using them.
	      takeInTrace.close();
	      streamData.close();
	      streamOperation.close();
	      }
	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	
	
	}	
	
	
	
	public boolean getCachValue(int memoryLineKey)									
	{
		if(this.myCache.containsKey(new Integer(memoryLineKey)))
			return true;						
		else
			return false;
	}
	
	public int locationOnCache(int size, int lineNos, int memoryLineKey)
	{
		int location= (memoryLineKey>>size);
		int INTmaximymLength=2147483647;
		int mov=(31-lineNos);
		int max=(INTmaximymLength>>mov);
		int cacheLocation=((location) & max);
		return cacheLocation;
	}

}

Open in new window

0
Comment
Question by:Atouray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35004267
You have printStackTrace in the code; so which line throws IOException?

0
 

Author Comment

by:Atouray
ID: 35004273
           while (streamOperation.available() != 0) { line 45
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35004295
Didin't you try to change it
to something like  

while((readline=stream.readLIne())! = null) {...}

 should be the same
but perhaps there are some subtle differences
 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35004425
           takeInTrace.close();
            streamData.close();
            streamOperation.close();

looks like you're closing the streams *inside* the loop
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35005708
// dis.available() returns 0 if the file does not have more lines.

That's not really the case:

a. it only reports the number of bytes that can be read *without blocking*
b. it doesn't guarantee that number of bytes will be read - you should normally read a file until EOF

In fact you can simplify your code greatly by using a Scanner (see below)

Also, consider using the lighter and faster HashMap rather than the legacy class Hashtable
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Hashtable;
import java.util.Scanner;


public class CacheSim {
    public static int hRate;
    public static int mRate;
    public static int noProcessors = 4;
    private static String processor;
    private static String state;
    private static String word;
    private static int cacheLocation;
    public int simIndex;
    public Hashtable<Integer, String> myCache;
    public int notValid;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CacheSim cacheSim = new CacheSim();
        File traceFile = new File(
                "C:/Users/mjarju/Documents/Uni Stuff/Course Work/Prallel Architecture/Assignments/Assignment2/memtrace.txt");
        boolean switch_off = false;
        String state_value;
        CacheSim[] proc = new CacheSim[noProcessors];

        try {
            Scanner sc = new Scanner(traceFile);
            String readLine = null;

            while (sc.hasNextLine()) {
                readLine = sc.nextLine();

                //========================================================
                if (readLine.length() > 1) {
                    processor = readLine.substring(0, 2);
                    state = readLine.substring(3, 4);
                    word = readLine.substring(5);
                    cacheLocation = Integer.parseInt(word);
                }

                if (readLine.length() == 1) {
                    if (readLine.equals("i")) {
                        System.out.println("Total number of Invalids" + " " +
                            cacheSim.notValid);

                        for (int i = 0; i < noProcessors; i++)
                            System.out.println("Invalidation on Processor " +
                                i + " = " + proc[i].simIndex);
                    } else if (readLine.equals("h")) {
                        System.out.println("hit rate is" + " " + hRate);
                        System.out.println("miss rate is" + " " + mRate);
                    }

                    if (readLine.equals("v")) {
                        if (switch_off == true) {
                            switch_off = false;
                        } else {
                            switch_off = true;
                        }
                    }

                    if (readLine.equals("p")) {
                        System.out.println(proc[0].myCache);
                        System.out.println(proc[1].myCache);
                        System.out.println(proc[2].myCache);
                        System.out.println(proc[3].myCache);
                    }
                } else {
                    if (state.equals("l")) {
                        state_value = "loads";
                    } else {
                        state_value = "store";
                    }

                    if (switch_off == true) {
                        System.out.println("a" + " " + state_value + "form" +
                            state + "to word" + word);
                    }
                }
            }

            // dispose all the resources after using them.
            sc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean getCachValue(int memoryLineKey) {
        if (this.myCache.containsKey(new Integer(memoryLineKey))) {
            return true;
        } else {
            return false;
        }
    }

    public int locationOnCache(int size, int lineNos, int memoryLineKey) {
        int location = (memoryLineKey >> size);
        int INTmaximymLength = 2147483647;
        int mov = (31 - lineNos);
        int max = (INTmaximymLength >> mov);
        int cacheLocation = ((location) & max);

        return cacheLocation;
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35044103
:)
0
 
LVL 92

Expert Comment

by:objects
ID: 35044946
Good to see my suggestion fixed your problem :)
your use of available() had nothing to do with it
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question