Behavior of java.io.FileOutputStream.getChannel() does not match the spec

Yixun Zhou coveringzyx at gmail.com
Fri Feb 18 04:48:35 UTC 2011


I missed the mail about the fix given by Alan, please just ignore the
previous mail.

2011/2/18 Sean Chou <zhouyx at linux.vnet.ibm.com>

>
> Oh, I should have done a little more investigation.
>
> Is there a plan to fix it? I can provide a patch if it is needed.
>
> 2011/2/17 Alan Bateman <Alan.Bateman at oracle.com>
>
> 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.
>>
>
>
>
> --
> Best Regards,
> Sean Chou
>
>


-- 
Best Regards,
Yixun Zhou



More information about the core-libs-dev mailing list