Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6924219

(fc spec) FileChannel.write(ByteBuffer, position) behavior when file opened for append not specified

XMLWordPrintable

    • b20
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0_18"
      Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
      Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux Darksun 2.6.32.3 #4 SMP Mon Jan 11 20:07:48 CET 2010 i686 Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz GenuineIntel GNU/Linux

      32bit Crux Linux 2.6

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      With an OpenSolaris 06/09 Virtual Machine the expected result is achieved.
      Open Solaris java version:

      java version "1.6.0_13"
      Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
      Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      The bytes in the second write call are appended to the file and not inserted at the specified position.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code below and inspect the resulting file in a hex editor.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A file containing the bytes "01 05 06 04".
      ACTUAL -
      A file containing the bytes "01 02 03 04 05 06".

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      None


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package filechanneltest;

      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;

      /**
       *
       * @author markus
       */
      public class Main {

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) throws IOException
          {
              File tmpFile = File.createTempFile("test", ".bin");

              FileChannel writer = new FileOutputStream(tmpFile).getChannel();

              byte[] bytes = {1, 2, 3, 4};
              ByteBuffer bb = ByteBuffer.wrap(bytes);

              writer.write(bb);
              writer.close();

              byte [] newBytes = {5,6};
              bb = ByteBuffer.wrap(newBytes);

              writer = new FileOutputStream(tmpFile, true).getChannel();
              writer.write(bb, 1);

              writer.close();

              System.out.println(tmpFile.getAbsolutePath());
              System.exit(0);
          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      package filechanneltest;

      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.RandomAccessFile;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;

      /**
       *
       * @author markus
       */
      public class Main {

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) throws IOException
          {
              File tmpFile = File.createTempFile("test", ".bin");

              FileChannel writer = new FileOutputStream(tmpFile).getChannel();

              byte[] bytes = {1, 2, 3, 4};
              ByteBuffer bb = ByteBuffer.wrap(bytes);

              writer.write(bb);
              writer.close();

              byte [] newBytes = {5,6};
              bb = ByteBuffer.wrap(newBytes);

      // writer = new FileOutputStream(tmpFile, true).getChannel();
              writer = new RandomAccessFile(tmpFile, "rw").getChannel();
              writer.write(bb, 1);

              writer.close();

              System.out.println(tmpFile.getAbsolutePath());
              System.exit(0);
          }

      }

            bpb Brian Burkhalter
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: