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

Sean Chou zhouyx at linux.vnet.ibm.com
Thu Feb 17 08:46:03 UTC 2011


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20110217/c264bf18/attachment.html>


More information about the core-libs-dev mailing list