Harmony
  1. Harmony
  2. HARMONY-26

The API of buffer classes in java.nio are not compliant with the specification of Java 5.0

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Classlib
    • Labels:
      None

      Description

      1. java.nio.CharBuffer
      1.1) java.nio.CharBuffer needs to implement two new interface java.lang.Appendable and java.lang.Readable
      1.2) The following methods should NOT be "protected":
      protected CharBuffer(int capacity)
      protected abstract char[] protectedArray();
      protected abstract int protectedArrayOffset();
      protected abstract boolean protectedHasArray();

      1.3) The following method should be "final":
      public CharBuffer put(char[] src)

      2. java.nio.Buffer
      2.1) The following fields should NOT be "protected":
      int UNSET_MARK
      int capacity
      int limit
      int mark should
      int position
      2.3) The following method should NOT be "protected":
      protected Buffer(int capacity)

      3. java.nio.ByteBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer
      3.1) The following field should NOT be "protected":
      com.ibm.platform.Endianness order
      3.2) The following methods should NOT be "protected":
      protected ByteBuffer(int capacity)
      protected abstract byte[] protectedArray();
      protected abstract int protectedArrayOffset();
      protected abstract boolean protectedHasArray();
      3.3) The following method should be "final":
      public ByteBuffer order(ByteOrder byteOrder)

      4. The implementation of bulk put/get methods of all the buffer classes are low-efficiency

      1. DirectBuffer.java
        1.0 kB
        Richard Liang
      2. nio-tests.jar
        31 kB
        Richard Liang

        Activity

        Tim Ellison made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Hide
        Tim Ellison added a comment -

        Richard, I have taken your tests (with slight refactoring of pcakage names), and they pass with the latest version of the NIO code.

        The tests are in NIO module, src/test/java/** at repository revision 370544.

        Show
        Tim Ellison added a comment - Richard, I have taken your tests (with slight refactoring of pcakage names), and they pass with the latest version of the NIO code. The tests are in NIO module, src/test/java/** at repository revision 370544.
        Richard Liang made changes -
        Attachment nio-tests.jar [ 12322123 ]
        Hide
        Richard Liang added a comment -

        Yes, Tim. The fix has been properly applied Thanks a lot.

        And I have attached my unit test cases to JIRA.

        Show
        Richard Liang added a comment - Yes, Tim. The fix has been properly applied Thanks a lot. And I have attached my unit test cases to JIRA.
        Tim Ellison made changes -
        Status In Progress [ 3 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Hide
        Tim Ellison added a comment -

        Actually, that wasn't as bad as it looks at first sight. It is mostly refactoring to be spec compliant, no functional change.

        Richard please can you review and verify.

        Show
        Tim Ellison added a comment - Actually, that wasn't as bad as it looks at first sight. It is mostly refactoring to be spec compliant, no functional change. Richard please can you review and verify.
        Hide
        Tim Ellison added a comment -

        Gulp – ok I'll start looking through this laundry list and fixing the NIO buffer code.

        Do you have accompanying JUnit tests to go along with these defects? Since you have contributor paperwork on file I suggest you simply attach them to this issue (ensuring you mark them as granting ASF license).

        Show
        Tim Ellison added a comment - Gulp – ok I'll start looking through this laundry list and fixing the NIO buffer code. Do you have accompanying JUnit tests to go along with these defects? Since you have contributor paperwork on file I suggest you simply attach them to this issue (ensuring you mark them as granting ASF license).
        Tim Ellison made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        Richard Liang made changes -
        Attachment DirectBuffer.java [ 12322104 ]
        Hide
        Richard Liang added a comment -

        This is the new interface added in com.ibm.io.nio

        Show
        Richard Liang added a comment - This is the new interface added in com.ibm.io.nio
        Hide
        Richard Liang added a comment -

        Hello Tim,

        There are several unnecessary fields/methods caused by the implementation classes of NIO buffers being in a separate internal package com.ibm.io.nio. To solve this issue, we can just move the implementation classes back to java.nio and make them package private. I will describe details below. There are lots of steps required to keep the code building right, if you prefer I can just send the updated code. Let me know whether you are satisfied with our solution. Thanks a lot.

        1. Move all classes in com.ibm.io.nio except FileChannelImpl, FileLockImpl and LockManager (total 35 classes) from com.ibm.io.nio into java.nio.

        2. For the 35 moved classes, change their package statement from com.ibm.io.nio to java.nio

        3. For the 35 moved classes, remove the unnecessary import statements of java.nio

        4. For the 7 classes java.nio.ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, and
        ShortBuffer, Remove the unnecessary import statement "import com.ibm.io.nio.BufferFactory;"

        *note* The above 4 steps can be conveniently achieved if you use "Refactor -> move" in Eclipse.

        5. Add a new interface in com.ibm.io.nio called DirectBuffer (I will attach the file to JIRA)

        6. Make java.nio.DirectByteBuffer to implement the new interface com.ibm.io.nio.DirectBuffer by:
        6.1 Adding import statement: import com.ibm.io.nio.DirectBuffer
        6.2 Changing Class declaration from
        "abstract class DirectByteBuffer extends BaseByteBuffer"
        to
        "abstract class DirectByteBuffer extends BaseByteBuffer implements DirectBuffer"

        7. Change the following "protected" methods of java.nio.DirectByteBuffer to "public"
        7.1 isAddressValid()
        7.2 addressValidityCheck()
        7.3 PlatformAddress getBaseAddress()
        7.4 PlatformAddress getEffectiveAddress()
        7.5 free()

        8. In com.ibm.io.nio.FileChannelImpl:
        8.1 Add import statement: "import java.nio.Buffer"
        8.2 In method "public int read(ByteBuffer buffer)",
        8.2.1 Change the line 270:
        "DirectByteBuffer directBuffer = (DirectByteBuffer) buffer;"
        to "DirectBuffer directBuffer = (DirectBuffer) buffer;"
        8.2.2 Remove line 275:
        "HeapByteBuffer heapBuffer = (HeapByteBuffer) buffer;"
        8.2.3 Change the line 275 & 276:
        "bytesRead = (int) fileSystem.read(handle, heapBuffer.array(),
        heapBuffer.arrayOffset(), buffer.remaining());"
        to "bytesRead = (int) fileSystem.read(handle, buffer.array(),
        buffer.arrayOffset(), buffer.remaining());"

        8.3 We do the same for the method "public int write(ByteBuffer buffer)",
        8.3.1 Change the line 348:
        "DirectByteBuffer directBuffer = (DirectByteBuffer) buffer;"
        to "DirectBuffer directBuffer = (DirectBuffer) buffer;"
        8.3.2 Remove line 353:
        "HeapByteBuffer heapBuffer = (HeapByteBuffer) buffer;"
        8.3.3 Change the line 353 & 354:
        "bytesWritten = (int) fileSystem.write(handle, heapBuffer
        .array(), heapBuffer.arrayOffset(), buffer.remaining());"
        to "bytesWritten = (int) fileSystem.write(handle, buffer
        .array(), buffer.arrayOffset(), buffer.remaining());"

        9. Make java.nio.BufferFactory as package private by removing "public" at line 31

        10. Make java.nio.DirectByteBuffer.SafeAddress as package private by removing "protected" at line 39.

        11. Make java.nio.DirectByteBuffers as package private by removing "public" at line 27

        12. Remove the "protected" modifier of the following fields:
        java.nio.Buffer.UNSET_MARK
        java.nio.Buffer.capacity
        java.nio.Buffer.limit
        java.nio.Buffer.mark
        java.nio.Buffer.position

        java.nio.ByteBuffer.order

        java.nio.MappedByteBuffer.mapMode
        java.nio.MappedByteBuffer.mappedFile
        java.nio.MappedByteBuffer.offset
        java.nio.MappedByteBuffer.wrappedBuffer

        12. Remove the "protected" modifier of the constructor of java.nio.Buffer: "protected Buffer(int capacity)"

        13. For the classes java.nio.ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer, remove the "protected" modifier of:
        13.1 protected constructor
        13.2 protected abstract byte[] protectedArray()
        13.3 protected abstract int protectedArrayOffset()
        13.4 protected abstract boolean protectedHasArray()

        14. Remove the "protected" modifier of the following methods in java.nio.MappedByteBuffer
        protected MappedByteBuffer(File mappedFile, long offset, int size, MapMode mapMode)
        protected MappedByteBuffer(File mappedFile, long offset, int size, MapMode mapMode, ByteBuffer wrappedBuffer)

        15. Make the following field "final"
        public ByteBuffer order(ByteOrder byteOrder) in java.nio.ByteBuffer
        public CharBuffer put(char[] src) in java.nio.CharBuffer

        16. Remove method "public ByteBuffer order(ByteOrder byteOrder)" in java.nio.MappedToByteBufferAdapter (because this method is defined as "final" in its super class java.nio.ByteBuffer)

        Show
        Richard Liang added a comment - Hello Tim, There are several unnecessary fields/methods caused by the implementation classes of NIO buffers being in a separate internal package com.ibm.io.nio. To solve this issue, we can just move the implementation classes back to java.nio and make them package private. I will describe details below. There are lots of steps required to keep the code building right, if you prefer I can just send the updated code. Let me know whether you are satisfied with our solution. Thanks a lot. 1. Move all classes in com.ibm.io.nio except FileChannelImpl, FileLockImpl and LockManager (total 35 classes) from com.ibm.io.nio into java.nio. 2. For the 35 moved classes, change their package statement from com.ibm.io.nio to java.nio 3. For the 35 moved classes, remove the unnecessary import statements of java.nio 4. For the 7 classes java.nio.ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, and ShortBuffer, Remove the unnecessary import statement "import com.ibm.io.nio.BufferFactory;" * note * The above 4 steps can be conveniently achieved if you use "Refactor -> move" in Eclipse. 5. Add a new interface in com.ibm.io.nio called DirectBuffer (I will attach the file to JIRA) 6. Make java.nio.DirectByteBuffer to implement the new interface com.ibm.io.nio.DirectBuffer by: 6.1 Adding import statement: import com.ibm.io.nio.DirectBuffer 6.2 Changing Class declaration from "abstract class DirectByteBuffer extends BaseByteBuffer" to "abstract class DirectByteBuffer extends BaseByteBuffer implements DirectBuffer" 7. Change the following "protected" methods of java.nio.DirectByteBuffer to "public" 7.1 isAddressValid() 7.2 addressValidityCheck() 7.3 PlatformAddress getBaseAddress() 7.4 PlatformAddress getEffectiveAddress() 7.5 free() 8. In com.ibm.io.nio.FileChannelImpl: 8.1 Add import statement: "import java.nio.Buffer" 8.2 In method "public int read(ByteBuffer buffer)", 8.2.1 Change the line 270: "DirectByteBuffer directBuffer = (DirectByteBuffer) buffer;" to "DirectBuffer directBuffer = (DirectBuffer) buffer;" 8.2.2 Remove line 275: "HeapByteBuffer heapBuffer = (HeapByteBuffer) buffer;" 8.2.3 Change the line 275 & 276: "bytesRead = (int) fileSystem.read(handle, heapBuffer.array(), heapBuffer.arrayOffset(), buffer.remaining());" to "bytesRead = (int) fileSystem.read(handle, buffer.array(), buffer.arrayOffset(), buffer.remaining());" 8.3 We do the same for the method "public int write(ByteBuffer buffer)", 8.3.1 Change the line 348: "DirectByteBuffer directBuffer = (DirectByteBuffer) buffer;" to "DirectBuffer directBuffer = (DirectBuffer) buffer;" 8.3.2 Remove line 353: "HeapByteBuffer heapBuffer = (HeapByteBuffer) buffer;" 8.3.3 Change the line 353 & 354: "bytesWritten = (int) fileSystem.write(handle, heapBuffer .array(), heapBuffer.arrayOffset(), buffer.remaining());" to "bytesWritten = (int) fileSystem.write(handle, buffer .array(), buffer.arrayOffset(), buffer.remaining());" 9. Make java.nio.BufferFactory as package private by removing "public" at line 31 10. Make java.nio.DirectByteBuffer.SafeAddress as package private by removing "protected" at line 39. 11. Make java.nio.DirectByteBuffers as package private by removing "public" at line 27 12. Remove the "protected" modifier of the following fields: java.nio.Buffer.UNSET_MARK java.nio.Buffer.capacity java.nio.Buffer.limit java.nio.Buffer.mark java.nio.Buffer.position java.nio.ByteBuffer.order java.nio.MappedByteBuffer.mapMode java.nio.MappedByteBuffer.mappedFile java.nio.MappedByteBuffer.offset java.nio.MappedByteBuffer.wrappedBuffer 12. Remove the "protected" modifier of the constructor of java.nio.Buffer: "protected Buffer(int capacity)" 13. For the classes java.nio.ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer, remove the "protected" modifier of: 13.1 protected constructor 13.2 protected abstract byte[] protectedArray() 13.3 protected abstract int protectedArrayOffset() 13.4 protected abstract boolean protectedHasArray() 14. Remove the "protected" modifier of the following methods in java.nio.MappedByteBuffer protected MappedByteBuffer(File mappedFile, long offset, int size, MapMode mapMode) protected MappedByteBuffer(File mappedFile, long offset, int size, MapMode mapMode, ByteBuffer wrappedBuffer) 15. Make the following field "final" public ByteBuffer order(ByteOrder byteOrder) in java.nio.ByteBuffer public CharBuffer put(char[] src) in java.nio.CharBuffer 16. Remove method "public ByteBuffer order(ByteOrder byteOrder)" in java.nio.MappedToByteBufferAdapter (because this method is defined as "final" in its super class java.nio.ByteBuffer)
        Tim Ellison made changes -
        Field Original Value New Value
        Assignee Geir Magnusson Jr [ geir ] Tim Ellison [ tellison ]
        Hide
        Tim Ellison added a comment -

        Wow - thanks for your thorough analysis Richard.

        Show
        Tim Ellison added a comment - Wow - thanks for your thorough analysis Richard.
        Richard Liang created issue -

          People

          • Assignee:
            Tim Ellison
            Reporter:
            Richard Liang
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development