This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Why this code works differently compiled and interpreted ???


>>>>> "Pedro" == Pedro Izecksohn <izecksohn at yahoo dot 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]