Behavior of java.io.FileOutputStream.getChannel() does not match the spec
Rémi Forax
forax at univ-mlv.fr
Thu Feb 17 14:42:05 UTC 2011
On 02/17/2011 10:34 AM, Alan Bateman wrote:
> Sean Chou wrote:
>> Hi,
>> I find there is a mismatch between the spec and the behavior of
>> java.io.FileOutputStream.getChannel().
>>
>> The spec reads: "The initial position of the returned channel will be
>> equal to the number of bytes written to the file so far unless this
>> stream is in append mode, in which case it will be equal to the size
>> of the file. "
>>
>> But we'll get size 0 in such situation. Here is the testcase:
>>
>> //Testcase
>> import java.io.*;
>>
>> public class FileOutputStreamTest{
>> public static void main(String args[]) throws Exception{
>> File tmpfile = File.createTempFile("FileOutputStream", "tmp");
>> tmpfile.deleteOnExit();
>> FileOutputStream fos = new FileOutputStream(tmpfile, false);
>> fos.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
>> fos.close();
>>
>> fos = new FileOutputStream(tmpfile, true);
>> long size = fos.getChannel().position();
>> if (10 != size){
>> // This return value is 0, mismatching the spec
>> System.out.println("Position Error Detected: except
>> 10, but was " + size);
>> }
>> fos.close();
>> }
>> }
>> // End of the testcase
>>
>> Any comments?
>>
> This is a long standing issue, 6526860 but hasn't been high priority
> as it probably isn't too common to query or change the file position
> when opened for append.
>
> -Alan.
Anyway, it's a good idea to fix it :)
Rémi
More information about the core-libs-dev
mailing list