8244136: Improved Buffer supports

Wed, 17 Jun 2020 08:48:03 +0000

author
yan
date
Wed, 17 Jun 2020 08:48:03 +0000
changeset 14216
f6804947798c
parent 14215
05922d77fc13
child 14217
a9306ab5534a

8244136: Improved Buffer supports
Reviewed-by: mbalao, andrew

src/share/classes/java/nio/X-Buffer.java.template file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/java/nio/X-Buffer.java.template	Wed May 20 13:56:21 2020 -0700
     1.2 +++ b/src/share/classes/java/nio/X-Buffer.java.template	Wed Jun 17 08:48:03 2020 +0000
     1.3 @@ -415,11 +415,11 @@
     1.4      public int read(CharBuffer target) throws IOException {
     1.5          // Determine the number of bytes n that can be transferred
     1.6          int targetRemaining = target.remaining();
     1.7 -        int remaining = remaining();
     1.8 +        int limit = limit();
     1.9 +        int remaining = limit - position();
    1.10          if (remaining == 0)
    1.11              return -1;
    1.12          int n = Math.min(remaining, targetRemaining);
    1.13 -        int limit = limit();
    1.14          // Set source limit to prevent target overflow
    1.15          if (targetRemaining < remaining)
    1.16              limit(position() + n);
    1.17 @@ -1162,10 +1162,15 @@
    1.18          if (!(ob instanceof $Type$Buffer))
    1.19              return false;
    1.20          $Type$Buffer that = ($Type$Buffer)ob;
    1.21 -        if (this.remaining() != that.remaining())
    1.22 +        int thisPos = this.position();
    1.23 +        int thisLim = this.limit();
    1.24 +        int thatPos = that.position();
    1.25 +        int thatLim = that.limit();
    1.26 +        int thisRem = thisLim - thisPos;
    1.27 +        int thatRem = thatLim - thatPos;
    1.28 +        if (thisRem < 0 || thisRem != thatRem)
    1.29              return false;
    1.30 -        int p = this.position();
    1.31 -        for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--)
    1.32 +        for (int i = thisLim - 1, j = thatLim - 1; i >= thisPos; i--, j--)
    1.33              if (!equals(this.get(i), that.get(j)))
    1.34                  return false;
    1.35          return true;
    1.36 @@ -1203,13 +1208,20 @@
    1.37       *          is less than, equal to, or greater than the given buffer
    1.38       */
    1.39      public int compareTo($Type$Buffer that) {
    1.40 -        int n = this.position() + Math.min(this.remaining(), that.remaining());
    1.41 -        for (int i = this.position(), j = that.position(); i < n; i++, j++) {
    1.42 +        int thisPos = this.position();
    1.43 +        int thisRem = this.limit() - thisPos;
    1.44 +        int thatPos = that.position();
    1.45 +        int thatRem = that.limit() - thatPos;
    1.46 +        int length = Math.min(thisRem, thatRem);
    1.47 +        if (length < 0)
    1.48 +            return -1;
    1.49 +        int n = thisPos + Math.min(thisRem, thatRem);
    1.50 +        for (int i = thisPos, j = thatPos; i < n; i++, j++) {
    1.51              int cmp = compare(this.get(i), that.get(j));
    1.52              if (cmp != 0)
    1.53                  return cmp;
    1.54          }
    1.55 -        return this.remaining() - that.remaining();
    1.56 +        return thisRem - thatRem;
    1.57      }
    1.58  
    1.59      private static int compare($type$ x, $type$ y) {

mercurial