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

(fs spec) Random write produces different results on Linux vs Windows from same .class

XMLWordPrintable

    • x86
    • linux

      Name: rmT116609 Date: 11/06/2003


      FULL PRODUCT VERSION :
      java version "1.4.2_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
      Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)


      FULL OS VERSION :
      RH 7.2 - 2.4.7-10smp
      RH 7.2 - 2.4.7-10uni
      RH 9 - 2.4.20-20.9 uni
      Windows 2000 Pro SP2

      A DESCRIPTION OF THE PROBLEM :
      One .class file compiled on Linux and then run on Linux & Windows produces different results.

      Random write works on Windows. On Linux, the write output is always appended to the end of the existing file no matter where the position indicates.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Simply use the FileChannel write method with the (ByteBuffer src, long position) signature to write anywhere into the file except the end. i.e. Try writing into the middle of the file.

      When executed on Windows it works. On Linux it always appends to the end of the file.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Since the Windows JVM does this properly and the Linux JVM doesn't, the expected results are what Windows produces. i.e. The write puts the bytes where the "position" parameter indicated.
      ACTUAL -
      In Linux the write always appends to the end of the file, no matter what. The position is ignored.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // To test, do the following on a Linux box:
      // javac bug.java
      // java bug debug or java bug
      // Then take the bug.class file to a Windows box and run it there to see
      // the difference. The identical bug.class file produces different results
      // depending on the JVM being used.

      import java.io.*;
      import java.nio.*;
      import java.nio.channels.FileChannel;

      public class bug {
        public static void main(String[] args)
        {
          boolean debug = false;
          if (args.length == 1 && args[0].equals("debug"))
            debug=true;

          String phrase = new String("Mary had a little lamb.");
          File aFile = new File("bugfile.txt");

          // Create the file output stream
          FileOutputStream outFile = null;
          try {
            outFile = new FileOutputStream(aFile);
          } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
          }
          FileChannel outChannel = outFile.getChannel();
          ByteBuffer buf = ByteBuffer.allocate(phrase.length());

          try {
            outChannel.write((ByteBuffer)buf.put(phrase.getBytes()).flip());
            outFile.close(); // Close the output stream & the channel
          } catch (IOException e) {
            e.printStackTrace(System.err);
          }

          buf.clear();

          try {
            outFile = new FileOutputStream(aFile, true);
          } catch(FileNotFoundException e) {
            e.printStackTrace(System.err);
            System.exit(1);
          }

          outChannel = outFile.getChannel();
          if (debug) {
            try {
              System.out.println("channel position = " + outChannel.position());
            } catch(java.io.IOException e) {
              e.printStackTrace(System.err);
              System.exit(1);
            }
          }

          try {
            if (debug) {
              try {
                System.out.println("File size at start="+outChannel.size());
              } catch(java.io.IOException e) {
                e.printStackTrace(System.err);
                System.exit(1);
              }
            }

            buf.put((byte)'X');
            buf.put((byte)'Y');
            buf.put((byte)'Z');
            buf.flip();
            if (debug) {
              try {
                System.out.println("OutChannel position prior to write = " + outChannel.position());
              } catch(java.io.IOException e) {
                e.printStackTrace(System.err);
                System.exit(1);
              }
            }
            outChannel.write(buf, 10);
            if (debug) {
              try {
                System.out.println("outChannel position after write = " + outChannel.position());
              } catch(java.io.IOException e) {
                e.printStackTrace(System.err);
                System.exit(1);
              }
            }
            buf.clear();

            if (debug) {
              try {
                System.out.println("File size at end="+outChannel.size());
              } catch(java.io.IOException e) {
                e.printStackTrace(System.err);
                System.exit(1);
              }
            }
            outFile.close();
          } catch(IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
          }
          System.exit(0);
        }
      }

      ---------- END SOURCE ----------
      (Incident Review ID: 206991)
      ======================================================================

            bpb Brian Burkhalter
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: