Sign in to follow this  
Destriarch

[java] Unexpected errors when reading long files

Recommended Posts

Ok this is driving me bonkers. I have a file type that I made to store custom tables of information. The filetype starts with a header pointing to the beginning of the information, followed by a look-up table. Each entry in the look-up table is an Integer representing the row number, followed by a long integer representing how far ahead of this point the information in that cell should be. The program needs to access this info in two ways. Firstly, it can skip directly to the long reference number using this code:
	public String ReadTable(int row, int column) {
		if (this.file.equals(""))
			return "<No Table Loaded>";
		if (row > this.rows || row < 0 || column > this.columns || column < 1)
			return "<Invalid Cell>";
		if (row == 0) column++;
		
		String result = ""; long y = 0;

		try {
			DataInputStream in = new DataInputStream(
					new BufferedInputStream(
							new FileInputStream(this.file + ".tbl")));
			in.skip(this.lookupStart + 4 + (row * 12));
			y = in.readLong(); in.skip(y);

			// in should now be positioned at the start of its data block
			for (int n = 1; n < column; n++)
				in.skip(in.readUnsignedShort());
			result = in.readUTF();
		} catch (Exception e) {
			return "<File Error>";
		}
		if (this.debug == true) return row + "/" + column + " " +
			y + ": " + result;
		else return result;
	}
That works just fine. Secondly though, it needs to be able to parse through each entry on the look-up table, testing a certain variable against the reference number to see if it's the right row. I use the following code for this:
	public String RollTable(int roll, int column) {
		if (this.file.equals(""))
			return "<No Table Loaded>";

		String result = ""; int x = 0; long y = 0; int z = 1;

		try {
			DataInputStream in = new DataInputStream(
					new BufferedInputStream(
							new FileInputStream(this.file + ".tbl")));
			in.skip(this.lookupStart+12);
			x = in.readInt();
			
			while (roll > x && z <= this.rows)
			{
				in.skip(8);
				x = in.readInt();
				z++;
			}
			
			y = in.readLong(); in.skip(y);

			// in should now be positioned at the start of its data block
			for (int n = 1; n < column; n++)
				in.skip(in.readUnsignedShort());
			result = in.readUTF();
		} catch (Exception e) {
			return "<File Error>";
		}
		if (this.debug == true) return roll + "/" + column + " " +
			y + ": " + result;
		else return result;
	}
This works up to a point, but when the table reaches a certain length, it starts to go wrong and deliver File I/O errors. The two code segments are near identical, they even return identical values of Y, and yet one works but the other fails. I have no idea what is going wrong. Logically, either both should work, both should fail, or the error should be in the code that differs from one function to the next. And yet that part of the code in the second function seems to work perfectly well. Can anyone shed any light on this problem? It's driving me mad. -Ash

Share this post


Link to post
Share on other sites
Quote:
Original post by JoeCooper
I'm reading it over. How come you don't print the stack trace to see what the exception actually is or where its thrown?


Well, I know where the error is thrown, it just doesn't make sense to me.

-Ash

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this