Why this code works differently compiled and interpreted ???

Tom Tromey tromey@redhat.com
Thu Apr 17 06:12:00 GMT 2003


>>>>> "Pedro" == Pedro Izecksohn <izecksohn@yahoo.com> writes:

Pedro>    Is something wrong inside this source?

Some indentation would have made this a lot easier to read.

Pedro> really_read = FR.read(content);

You aren't guaranteed to fill the buffer here.
It might work in some cases, but in others it might not.
(The contract for Reader.read doesn't specify that the buffer will be
filled, even if enough data is available.)

In this particular case, it doesn't.

Note that your code has a subtle bug anyway.  File.length returns the
length of the file in bytes.  However, a Reader converts bytes to
characters -- not necessarily a 1-1 mapping.

So if, for instance, your current encoding is UTF-8 and you read a
file full of multi-byte characters, you could read far fewer
characters than there are bytes.

If you want to read fully, either use RandomAccessFile.readFully, or
write your own function that loops until EOF.

Tom



More information about the Java mailing list