Val's Blog
Lots of stuff for Web 2.0 freaks and Javaholics
Feeds RSS | Atom | RDF
 
 
Frederik Brooks: "Adding manpower to a late software project makes it later."
[ Login ]

May 2008
SunMonTueWedThuFriSat
     1  2  3 
 4  5  6  7  8  9  10 
 11  12  13  14  15  16  17 
 18  19  20  21  22  23  24 
 25  26  27  28  29  30  31 
Apr  |  Today  |  Jun
XML Feeds   Subscribe with Bloglines

Javaranch Sheriff   My LinkedIn Profile
Drop me a line or two   Bloglines Blogroll
JavaRSS   Referers
How cool are you?   My Reviews

Next trips...
SpringOne 2008 (Jun 11-12, 08)
Ajax Exp. 2008 (Sep 29-Oct 1, 08)
Top 10 entries (#hits)
(As of Nov 30, 2007)


Top 10 entries (#hits/day)
Come Back (5.032)
(As of Nov 30, 2007)
Recent Blog Entries
Recent Blog Comments
Re: Review of "Marketing Management 12th"
i know marketing management by kotler is good book but the problem is that the management part of this book is totally missing as fare as i know managemet is complete different subject and it should not be mixed i am student of MBA i was looking at ass...

Re: Review of "Pro Spring"
Using simple POJOs + factories without Spring for "echo" and "counter" would be a lot more easier. No need to write those XML files... So, in this case using Spring makes me write a lot more code... (OK, you can generate everything with the help of And...

pls urgent
Hi I am trying to generate the word doc but i m not understanding wats happening any one pls figure it out /* * WordAPI.java * * Created on May 30, 2006, 10:50 AM * * To change this template, choose Tools | Template Manager * and open the te...
Archives (# entries)
Links
Other Blogs
Other Blogs

Reviewing
Reading
Locations of visitors to this page
What they once said...
 

A very common mistake that might seem obvious (or so I hope), yet that I've seen in many places is the one where we need to read characters from any kind of input streams and we cast an int read from that input stream to a char a little bit too early.

The usual code goes like this:

InputStream in = ...;
char c = 0;
while ((c = (char) in.read()) > -1) {
   //do something with c
}

The while loop will never break and depending on what you do within the loop probably end up in an OutOfMemoryError being raised, for instance if you keep appending the read character to a StringBuffer or something similar.

The reason for this is that the type char is the only unsigned primitive type in Java, and thus, the range of allowed values for a variable having the type char starts at 0. It's obvious then that the local variable c will never get assigned the value -1 but in case i is -1 (i.e., end of stream reached), c will get the value Character.MAX_VALUE (or 0xFFFF). Thus the solution consists of applying the type cast within the while loop only after the value check is successful, as shown below:

InputStream in = ...;
int i = 0;
while ((i = in.read()) > -1) {
   char c = (char) i;
   //do something with c
}
//EOF reached

That way the loop will always break at some point and you'll be safe.

 
About this Blog