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

java.io.RandomAccessFile: Performance enhancement broke compatibility (beta4.1)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 1.2.0
    • Fix Version/s: 1.2.0
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      1.2fcs
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Not verified

      Description

      To: ###@###.###
      Cc: ###@###.###, ken.arnold@east
      Subject: changes to java.io.RandomAccessFile.readUnsignedShort()
      Date: Wed, 26 Aug 1998 21:45:35 -0400
      From: Charlie Lamb <###@###.###>

      The following program produces different results when run with the JDK
      1.2 beta4 and the JDK 1.2 beta4.1. The reason is because of the
      (presumably) performance enhancement made to
      java.io.RandomAccessFile.readUnsignedShort() whereby it calls
      readTwoBytes(), a native method instead of sequential calls to read().

      Changing the internal implementation of java.io.RandomAccessFile is
      probably well within your rights. I will grant you that it was never
      part of the published contract of java.io.RandomAccessFile that
      readByte, readShort, readInt, etc. would necessarily use one of the
      three overloadings of read() to gather their bytes, but one might
      have surmised this from the following facts:

      (1) java.io.RandomAccessFile was/is not a final class and is therefore
      meant to be extended,

      (2) readByte, readShort, readInt, etc. are declared to be final
      methods, and

      (3) read(), read(byte[]), and read(byte[], int, int) are not declared
      to be final methods meaning that they are allowed to be extended.

      The example below is admittedly contrived, but we do in fact extend
      RandomAccessFile in ObjectStore PSE (used by JINI's ObjectSpaces) to
      provide buffering on top of RandomAccessFile. Yes, I can recode this
      so that I provide my own implementations of readFully, readByte,
      readInt, readShort, and readUTF, but I can't make strict overloadings
      of the method signatures declared for RandomAccessFile (since they're
      final).

      It also means we'll have to ship a new release of PSE in order to have
      it work with the JDK 1.2 beta 4.1.

      So, is there some really good performance improvement that comes with
      this change from read(), read() to readTwoBytes() (ditto for
      writeUnsignedShort())?

      Thanks for your comments on this.


      import java.io.*;

      public class ReadOT {
        class ExtendedRAF extends java.io.RandomAccessFile {
          ExtendedRAF(String name, String mode) throws IOException {
            super(name, mode);
          }

          private byte[] rb = new byte[1];
          public int read() throws IOException {
            System.out.println("ERAF read() called");
            int ret = read(rb, 0, 1);
            if (ret < 0)
      return -1;
            else
      return (rb[0] & 0xff);
          }

          public int read(byte[] b, int off, int len) throws IOException {
            System.out.println("ERAF read(byte[], int, int) called");
            return super.read(b, off, len);
          }

          public int read(byte b[]) throws IOException {
            System.out.println("ERAF read(byte[]) called");
            return read(b, 0, b.length);
          }
        }

        static public void main(String argv[]) throws IOException {
          ReadOT rot = new ReadOT();
          ExtendedRAF eraf = rot.new ExtendedRAF("foo", "rw");
          eraf.writeShort(123);
          eraf.seek(0);
          System.out.println(eraf.readShort());
        }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mr Mark Reinhold
              Reporter:
              mr Mark Reinhold
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: