4774077: Use covariant return types in the NIO buffer hierarchy
authorrwarburton
Wed, 29 Oct 2014 14:10:34 +0100
changeset 27292 7ff4b24b33ce
parent 27291 42769f9ca831
child 27293 901a9a6f8658
4774077: Use covariant return types in the NIO buffer hierarchy Reviewed-by: psandoz, alanb, mr, darcy
jdk/src/java.base/share/classes/java/nio/Buffer.java
jdk/src/java.base/share/classes/java/nio/MappedByteBuffer.java
jdk/src/java.base/share/classes/java/nio/X-Buffer.java.template
jdk/src/java.base/share/classes/sun/security/ssl/CipherBox.java
jdk/src/java.base/share/classes/sun/security/ssl/EngineInputRecord.java
jdk/src/java.base/share/classes/sun/security/ssl/EngineOutputRecord.java
jdk/test/java/io/FileDescriptor/Finalize.java
jdk/test/java/nio/charset/CharsetEncoder/Flush.java
jdk/test/sun/nio/cs/TestUTF_16.java
jdk/test/sun/nio/cs/TestUTF_32.java
--- a/jdk/src/java.base/share/classes/java/nio/Buffer.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/java/nio/Buffer.java	Wed Oct 29 14:10:34 2014 +0100
@@ -239,7 +239,7 @@
      * @throws  IllegalArgumentException
      *          If the preconditions on <tt>newPosition</tt> do not hold
      */
-    public final Buffer position(int newPosition) {
+    public Buffer position(int newPosition) {
         if ((newPosition > limit) || (newPosition < 0))
             throw new IllegalArgumentException();
         position = newPosition;
@@ -270,7 +270,7 @@
      * @throws  IllegalArgumentException
      *          If the preconditions on <tt>newLimit</tt> do not hold
      */
-    public final Buffer limit(int newLimit) {
+    public Buffer limit(int newLimit) {
         if ((newLimit > capacity) || (newLimit < 0))
             throw new IllegalArgumentException();
         limit = newLimit;
@@ -284,7 +284,7 @@
      *
      * @return  This buffer
      */
-    public final Buffer mark() {
+    public Buffer mark() {
         mark = position;
         return this;
     }
@@ -300,7 +300,7 @@
      * @throws  InvalidMarkException
      *          If the mark has not been set
      */
-    public final Buffer reset() {
+    public Buffer reset() {
         int m = mark;
         if (m < 0)
             throw new InvalidMarkException();
@@ -325,7 +325,7 @@
      *
      * @return  This buffer
      */
-    public final Buffer clear() {
+    public Buffer clear() {
         position = 0;
         limit = capacity;
         mark = -1;
@@ -353,7 +353,7 @@
      *
      * @return  This buffer
      */
-    public final Buffer flip() {
+    public Buffer flip() {
         limit = position;
         position = 0;
         mark = -1;
@@ -375,7 +375,7 @@
      *
      * @return  This buffer
      */
-    public final Buffer rewind() {
+    public Buffer rewind() {
         position = 0;
         mark = -1;
         return this;
--- a/jdk/src/java.base/share/classes/java/nio/MappedByteBuffer.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/java/nio/MappedByteBuffer.java	Wed Oct 29 14:10:34 2014 +0100
@@ -208,4 +208,76 @@
     private native boolean isLoaded0(long address, long length, int pageCount);
     private native void load0(long address, long length);
     private native void force0(FileDescriptor fd, long address, long length);
+
+    // -- Covariant return type overrides
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer position(int newPosition) {
+        super.position(newPosition);
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer limit(int newLimit) {
+        super.limit(newLimit);
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer mark() {
+        super.mark();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer reset() {
+        super.reset();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer clear() {
+        super.clear();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer flip() {
+        super.flip();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public final MappedByteBuffer rewind() {
+        super.rewind();
+        return this;
+    }
 }
--- a/jdk/src/java.base/share/classes/java/nio/X-Buffer.java.template	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/java/nio/X-Buffer.java.template	Wed Oct 29 14:10:34 2014 +0100
@@ -1025,6 +1025,106 @@
         return offset;
     }
 
+    // -- Covariant return type overrides
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer position(int newPosition) {
+        super.position(newPosition);
+        return this;
+    }
+    
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer limit(int newLimit) {
+        super.limit(newLimit);
+        return this;
+    }
+    
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public 
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer mark() {
+        super.mark();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public 
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer reset() {
+        super.reset();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public 
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer clear() {
+        super.clear();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public 
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer flip() {
+        super.flip();
+        return this;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @since 1.9
+     */
+    @Override
+    public 
+#if[!byte]
+    final
+#end[!byte]
+    $Type$Buffer rewind() {
+        super.rewind();
+        return this;
+    }
+
     /**
      * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
      *
--- a/jdk/src/java.base/share/classes/sun/security/ssl/CipherBox.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/sun/security/ssl/CipherBox.java	Wed Oct 29 14:10:34 2014 +0100
@@ -560,7 +560,7 @@
                         + newLen);
 
                     hd.encodeBuffer(
-                        (ByteBuffer)bb.duplicate().position(pos), System.out);
+                        bb.duplicate().position(pos), System.out);
                 } catch (IOException e) { }
             }
 
@@ -790,7 +790,7 @@
 
         // The padding data should be filled with the padding length value.
         int[] results = checkPadding(
-                (ByteBuffer)bb.duplicate().position(offset + newLen),
+                bb.duplicate().position(offset + newLen),
                 (byte)(padLen & 0xFF));
         if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
             if (results[0] != 0) {          // padding data has invalid bytes
--- a/jdk/src/java.base/share/classes/sun/security/ssl/EngineInputRecord.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/sun/security/ssl/EngineInputRecord.java	Wed Oct 29 14:10:34 2014 +0100
@@ -349,8 +349,7 @@
         /*
          * Copy data out of buffer, it's ready to go.
          */
-        ByteBuffer netBB = (ByteBuffer)
-            (ByteBuffer.allocate(len).put(buf, 0, len).flip());
+        ByteBuffer netBB = ByteBuffer.allocate(len).put(buf, 0, len).flip();
         engine.writer.putOutboundDataSync(netBB);
     }
 
--- a/jdk/src/java.base/share/classes/sun/security/ssl/EngineOutputRecord.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/src/java.base/share/classes/sun/security/ssl/EngineOutputRecord.java	Wed Oct 29 14:10:34 2014 +0100
@@ -113,9 +113,7 @@
         /*
          * Copy data out of buffer, it's ready to go.
          */
-        ByteBuffer netBB = (ByteBuffer)
-            ByteBuffer.allocate(len).put(buf, off, len).flip();
-
+        ByteBuffer netBB = ByteBuffer.allocate(len).put(buf, off, len).flip();
         writer.putOutboundData(netBB);
     }
 
--- a/jdk/test/java/io/FileDescriptor/Finalize.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/test/java/io/FileDescriptor/Finalize.java	Wed Oct 29 14:10:34 2014 +0100
@@ -245,9 +245,10 @@
          * write to fc2 - when fos1 is gc'ed and finalizer is run,
          * write to fc2 should not fail
          */
-        bb = ByteBuffer.allocateDirect(data.length);
-        bb = bb.put(data);
-        bb = (ByteBuffer) bb.flip();
+        bb = ByteBuffer.allocateDirect(data.length)
+                       .put(data)
+                       .flip();
+
         ret = fc2.write(bb);
         System.out.println("Wrote:" +  ret + " bytes to fc2");
         fc2.close();
--- a/jdk/test/java/nio/charset/CharsetEncoder/Flush.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/test/java/nio/charset/CharsetEncoder/Flush.java	Wed Oct 29 14:10:34 2014 +0100
@@ -35,7 +35,7 @@
 public class Flush {
     private static byte[] contents(ByteBuffer bb) {
         byte[] contents = new byte[bb.position()];
-        ((ByteBuffer)(bb.duplicate().flip())).get(contents);
+        bb.duplicate().flip().get(contents);
         return contents;
     }
 
--- a/jdk/test/sun/nio/cs/TestUTF_16.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/test/sun/nio/cs/TestUTF_16.java	Wed Oct 29 14:10:34 2014 +0100
@@ -150,7 +150,7 @@
             if (CoderResult.OVERFLOW !=
                 Charset.forName("UTF_16")
                 .newDecoder()
-                .decode((ByteBuffer)(ByteBuffer.allocate(4)
+                .decode((ByteBuffer.allocate(4)
                                      .put(new byte[]
                                           {(byte)0xd8,(byte)0x00,
                                            (byte)0xdc,(byte)0x01})
--- a/jdk/test/sun/nio/cs/TestUTF_32.java	Wed Oct 29 11:53:37 2014 +0000
+++ b/jdk/test/sun/nio/cs/TestUTF_32.java	Wed Oct 29 14:10:34 2014 +0100
@@ -184,7 +184,7 @@
         if (CoderResult.OVERFLOW !=
             Charset.forName("UTF_32")
             .newDecoder()
-            .decode((ByteBuffer)(ByteBuffer.allocate(4)
+            .decode((ByteBuffer.allocate(4)
                                  .put(new byte[]
                                       {(byte)0,(byte)1, (byte)0,(byte)01})
                                  .flip()),