changeset 9997:9023dd3e87df

8244136: Improved Buffer supports Reviewed-by: bae, dcherepanov Contributed-by: Ekaterina Vergizova <katya@azul.com>
author yan
date Wed, 17 Jun 2020 08:48:03 +0000
parents 245d5bd872ed
children 7345953eea82
files src/share/classes/java/nio/X-Buffer.java.template
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/X-Buffer.java.template	Wed Apr 29 13:20:25 2020 -0700
+++ b/src/share/classes/java/nio/X-Buffer.java.template	Wed Jun 17 08:48:03 2020 +0000
@@ -411,11 +411,11 @@
     public int read(CharBuffer target) throws IOException {
         // Determine the number of bytes n that can be transferred
         int targetRemaining = target.remaining();
-        int remaining = remaining();
+        int limit = limit();
+        int remaining = limit - position();
         if (remaining == 0)
             return -1;
         int n = Math.min(remaining, targetRemaining);
-        int limit = limit();
         // Set source limit to prevent target overflow
         if (targetRemaining < remaining)
             limit(position() + n);
@@ -1130,10 +1130,15 @@
         if (!(ob instanceof $Type$Buffer))
             return false;
         $Type$Buffer that = ($Type$Buffer)ob;
-        if (this.remaining() != that.remaining())
+        int thisPos = this.position();
+        int thisLim = this.limit();
+        int thatPos = that.position();
+        int thatLim = that.limit();
+        int thisRem = thisLim - thisPos;
+        int thatRem = thatLim - thatPos;
+        if (thisRem < 0 || thisRem != thatRem)
             return false;
-        int p = this.position();
-        for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--)
+        for (int i = thisLim - 1, j = thatLim - 1; i >= thisPos; i--, j--)
             if (!equals(this.get(i), that.get(j)))
                 return false;
         return true;
@@ -1171,13 +1176,20 @@
      *          is less than, equal to, or greater than the given buffer
      */
     public int compareTo($Type$Buffer that) {
-        int n = this.position() + Math.min(this.remaining(), that.remaining());
-        for (int i = this.position(), j = that.position(); i < n; i++, j++) {
+        int thisPos = this.position();
+        int thisRem = this.limit() - thisPos;
+        int thatPos = that.position();
+        int thatRem = that.limit() - thatPos;
+        int length = Math.min(thisRem, thatRem);
+        if (length < 0)
+            return -1;
+        int n = thisPos + Math.min(thisRem, thatRem);
+        for (int i = thisPos, j = thatPos; i < n; i++, j++) {
             int cmp = compare(this.get(i), that.get(j));
             if (cmp != 0)
                 return cmp;
         }
-        return this.remaining() - that.remaining();
+        return thisRem - thatRem;
     }
 
     private static int compare($type$ x, $type$ y) {