Monday, April 19, 2010

Bug in java's System.out.write with large byte array

Here is a program that just writes lot of "a"s. Compile it and run it with
java B 60000
It doesn't write. Now try,
java B 50000
It does write

----------------------------------------------------------------------------------------
import java.io.*;

public class B {
public static void main(String[] args) throws IOException{
StringBuffer sb = new StringBuffer();
for(int i=0;i {
sb.append("a");
}
byte [] b= sb.toString().getBytes();
System.out.write(b);
System.out.println("Number of bytes: "+b.length);

}
}

---------------------------------------
My colleague Venkatesh pointed out that some error has occurred and because this method doesn't throw an exception, it sets the error,

System.out.println("Error=" + System.out.checkError());

No comments: