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

Increasing the size of a RandomAccessFile via a MappedByteBuffer with the "map" function changes file pointer in JDK 8

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 Professional
      JDK 1.8.0_172
      JDK 10.0.1

      A DESCRIPTION OF THE PROBLEM :
      Increasing the size of a RandomAccessFile via a MappedByteBuffer with the "map" function results in overwriting the already existing part.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Install JDK 1.8.0_172 (short: JDK8)
      2. Install JDK 10.0.1 (short: JDK10)
      3. Modify the "path" and "fileName" constants in the class MMFFail
      4. Compile the given code with JDK8 (<path to JDK8>\\javac.exe -cp ... MMFFail.java)
      5. Run the .class file with JDK8 (<path to JDK8>\\java.exe -cp ... MMFFail) [result: File with 1,048,576 Byte]
      6. Run the .class file with JDK10 (<path to JDK10>\\java.exe -cp ... MMFFail) [result: File with 524,288 Byte]
      7. Compile the given code with JDK10 (<path to JDK10>\\javac.exe -cp ... MMFFail.java)
      8. Run the .class file with JDK10 (<path to JDK10>\\java.exe -cp ... MMFFail) [result: File with 524,288 Byte]

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Running MMFFail should produce two similar files (with a size of 1,048,576 Byte) independent of running it with either JDK8 or JDK10.
      ACTUAL -
      Running MMFFail with JDK8 produces a file with a size of 1,048,576 Byte
      Running MMFFail with JDK10 produces a file with a size of 524,288 Byte.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import java.io.IOException;
      import java.io.RandomAccessFile;
      import java.nio.MappedByteBuffer;
      import java.nio.channels.FileChannel;

      public final class MMFFail {

          //Path to the generated file
          private static final String path = "C:\\tmp\\";
          private static final String fileName = "myMem";

          public static void main(String[] args){

              File myFile = new File(path+fileName+"_"+System.currentTimeMillis()+".txt");

              if(myFile.exists()){
                  myFile.delete();
                  System.out.println("Delete old file.");
              }

              try {
                  // Create a RAF
                  RandomAccessFile raf = new RandomAccessFile(myFile, "rw");

                  // Allocate 524288 byte on the disc
                  MappedByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 524288);

                  //Fill with 'a'
                  while (buf.remaining()>=1){
                      buf.put((byte) 'a');
                  }
                  buf.force();

                  // Allocate additional 524288 byte on the disc, at the pointer position
                  // expect: 1048576 byte allocated on the disc
                  // result for 1.8.0_172: 1048576 byte allocated
                  // result for 10.0.1: 524288 byte allocated
                  buf = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, raf.getFilePointer(), 524288);

                  //Fill the 524288 byte with 'b'
                  while (buf.remaining()>=1){
                      buf.put((byte) 'b');
                  }
                  buf.force();

                  //Close the RAF
                  raf.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }

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

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dkejriwal Deepak Kejriwal (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: