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]

Why this code works differently compiled and interpreted ???


   In Windows I used Mingw's gcj 3.2. In Linux I used gcj 2.95.3. java and
javac I used Sun's j2sdk1.4.0 for Linux and j2sdk1.4.1_01 for Windows.

   Is something wrong inside this source?

   If you are not seeing the difference, try RFTest with a big file, that
contains more than 250 bytes.

//--------------------------------------------------

import java.io.*;

/** By: Pedro Izecksohn , izecksohn at yahoo dot com <br>
    Version: 16/Apr/2003 22:22
<br>
<br>
RFTest means Read File Test.<br>
<br>
This class works differently when compiled with javac and runned with java ,
and when compiled with gcj and runned in Windows or Linux.<br>
The difference occurr at: if ( really_read != (int) file_length ) ...<br>
<br>
*/

public class RFTest {

public RFTest ( String fileName ) {

System.out.println("I\'m at the beggining of RFTest.RFTest (String) .");

File file = null ;

try {
file = new File( fileName );
} catch ( NullPointerException npe ) {
System.err.println( npe.toString() );
System.exit(1);
} // Ends catch.

if ( ! file.exists() ) {
System.err.println( fileName + " does NOT exist.");
System.exit(1);
}

final long file_length = file.length();

if ( file_length == 0 ) {
System.err.println ( fileName + " is empty.");
System.exit(1);
} // Ends if.

System.out.println("The File " + file.toString() + " will be loaded.");

FileReader FR = null ;

try {
FR = new FileReader ( file );
} catch ( IOException ioe ) {
System.err.println( fileName + " could NOT be opened for reading.");
System.exit(1);
} // Ends catch.

char [] content = new char [ (int) file_length ];

System.out.println("file_length==" + file_length);
System.out.println("content.length==" + content.length);

int really_read=0;

try {
really_read = FR.read(content);
} catch ( IOException ioe ) {
System.err.println("The following error occurred when trying to
FR.read(content); :\n" + ioe.toString() );
System.exit(1);
} // Ends catch.

try {
FR.close();
} catch ( IOException ioe ) {
System.err.println( fileName + " could NOT be closed.\n" + ioe.toString() );
System.exit(1);
} // Ends catch.

if ( really_read != (int) file_length ) {
System.err.println( "( really_read != (int) file_length )\nfile_length == "
+ file_length + "\treally_read == " + really_read );
System.err.println("This is the content that was read:\n" + (new
String(content)) );
System.exit(1);
} else {
System.out.println("Every thing is OK .");
System.exit(0);
} // Ends else.

} // Ends constructor RFTest ( String ) .

public static void main ( String [] args ) {

System.out.println("I\'m inside RFTest.main(String[]) .");

if ( args.length > 0 ) { new RFTest ( args[0] ); }
else { System.out.println("No argument was passed to me."); }

} // Ends main .

} // Ends class RFTest .



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