Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,824
1,126
The Land of Hope and Glory
I have a very strange problem with a Java servlet. Here is the code:

Code:
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		processRequest(request, response);
		PrintWriter out = response.getWriter();
		out.println(response.getHeader("line_one"));
		out.println(response.getHeader("line_two"));
		out.println(response.getHeader("line_three"));
		out.println(response.getHeader("line_four"));
		out.close();
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		
		line_number = Integer.parseInt(request.getHeader("line_number"));
		
		file_reader.setLineNumber(line_number);
		response.addHeader("line_one", file_reader.readLine());
		
		file_reader.setLineNumber(line_number + 1);
		response.addHeader("line_two", file_reader.readLine());
		
		file_reader.setLineNumber(line_number + 700);
		response.addHeader("line_three", file_reader.readLine());
		
		file_reader.setLineNumber(line_number + 701);
		response.addHeader("line_four", file_reader.readLine());
	}

Basically it takes an integer sent from the iPhone via NSURLConnection in an NSURLRequest and returns 4 lines from a text file. The lines should be the line number of the integer received, integer + 1, integer + 700 and integer + 701. Yet when I send the request the servlet sends lines 1, 2, 3 and 4 back. If I stop the iPhone app and restart it the servlet then sends lines 5, 6, 7 and 8 back. Yet I was sure that the setLineNumber() method was meant to explicitly set the line number in the file input before reading it.

I have hard coded the number 1 into the iPhone app for testing so that is not a problem and checked that the servlet is receiving the correct number on the server side. Does anyone have any idea what could be happening?
 
From the documentation "Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber()".

This class (which I must admit I've never looked at before) just tracks the line numbers to stop you doing so yourself. If you want to skip lines you still have to sequentially read each one till you get to the one you want...
 
From the documentation "Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber()".

This class (which I must admit I've never looked at before) just tracks the line numbers to stop you doing so yourself. If you want to skip lines you still have to sequentially read each one till you get to the one you want...

/facepalm

Well I'll be...

This is the problem with self documenting code. You get in the habit of reading the code rather than the documentation. Thanks for pointing that out to me I completely missed that.

I was originally going to write the line number reader myself but was quite impressed when I saw (what I thought) was a standard class to do it.
 
This is the problem with self documenting code. You get in the habit of reading the code rather than the documentation. Thanks for pointing that out to me I completely missed that.

If it had been me writing the java docs for that class I'd have put that note with the method as well as at the top of the class. I imagine you are not the first person to skip right to the method and then wonder why it's not doing what you expect.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.