M
markspace
Hi all,
The following code demonstrates a bug in the java.util.Scanner class, I
think. It creates a large file, then attempts to read in the same file
with a Scanner using a delimiter of "\z".
This doesn't work. Only a part of the file is read (the first 1024
bytes). The result is that the comparison operation fails. I can
manually inspect the file created and it does have the correct number of
strings -- 16384. This code finds the last string at number 284.
Am I doing something wrong? Or should I report this to Sun?
<output>
run:
fileContents.length()=1024
Exception in thread "main" java.lang.RuntimeException: Result was: 28,
expected 283
at scannerbug.ScannerBug.testContents(ScannerBug.java:55)
at scannerbug.ScannerBug.main(ScannerBug.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
</output>
<sscce>
package scannerbug;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ScannerBug
{
private static final String FILE = "test.txt";
private static final int TEST_FILE_SIZE = 16 * 1024;
public static void main( String[] args )
throws Exception
{
makeTestSource( FILE );
String fileContents = new Scanner( new File( FILE ) ).useDelimiter(
"\\z" ).next();
System.err.println( "fileContents.length()=" +
fileContents.length() );
testContents( fileContents );
}
private static void makeTestSource( String string )
throws IOException
{
BufferedWriter bw = new BufferedWriter( new FileWriter( string ) );
for( int i = 0; i < TEST_FILE_SIZE; i++ )
{
bw.write( Integer.toString( i ) );
bw.write( '\n' );
}
bw.close();
}
private static void testContents( String string )
{
Scanner scanner = new Scanner( string );
for( int i = 0; i < TEST_FILE_SIZE; i++ )
{
if( scanner.hasNextInt() )
{
int result = scanner.nextInt();
if( i != result )
{
throw new RuntimeException( "Result was: " + result
+ ", expected " + i );
}
}else
{
throw new RuntimeException(
"Ran out of ints in string at pos "
+ i );
}
}
}
}
</sscce>
The following code demonstrates a bug in the java.util.Scanner class, I
think. It creates a large file, then attempts to read in the same file
with a Scanner using a delimiter of "\z".
This doesn't work. Only a part of the file is read (the first 1024
bytes). The result is that the comparison operation fails. I can
manually inspect the file created and it does have the correct number of
strings -- 16384. This code finds the last string at number 284.
Am I doing something wrong? Or should I report this to Sun?
<output>
run:
fileContents.length()=1024
Exception in thread "main" java.lang.RuntimeException: Result was: 28,
expected 283
at scannerbug.ScannerBug.testContents(ScannerBug.java:55)
at scannerbug.ScannerBug.main(ScannerBug.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
</output>
<sscce>
package scannerbug;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ScannerBug
{
private static final String FILE = "test.txt";
private static final int TEST_FILE_SIZE = 16 * 1024;
public static void main( String[] args )
throws Exception
{
makeTestSource( FILE );
String fileContents = new Scanner( new File( FILE ) ).useDelimiter(
"\\z" ).next();
System.err.println( "fileContents.length()=" +
fileContents.length() );
testContents( fileContents );
}
private static void makeTestSource( String string )
throws IOException
{
BufferedWriter bw = new BufferedWriter( new FileWriter( string ) );
for( int i = 0; i < TEST_FILE_SIZE; i++ )
{
bw.write( Integer.toString( i ) );
bw.write( '\n' );
}
bw.close();
}
private static void testContents( String string )
{
Scanner scanner = new Scanner( string );
for( int i = 0; i < TEST_FILE_SIZE; i++ )
{
if( scanner.hasNextInt() )
{
int result = scanner.nextInt();
if( i != result )
{
throw new RuntimeException( "Result was: " + result
+ ", expected " + i );
}
}else
{
throw new RuntimeException(
"Ran out of ints in string at pos "
+ i );
}
}
}
}
</sscce>