changeset 53732:8b3498547395 jdk-11.0.10+9 jdk-11.0.10-ga

8247619: Improve Direct Buffering of Characters Reviewed-by: alanb, ahgross, rhalade, psandoz
author bpb
date Wed, 29 Jul 2020 09:52:13 -0700
parents 405102e26a62
children 0541256c13df
files src/java.base/share/classes/java/nio/Buffer.java src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template src/java.base/share/classes/java/nio/X-Buffer.java.template
diffstat 3 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nio/Buffer.java	Wed Dec 23 09:07:55 2020 +0100
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Wed Jul 29 09:52:13 2020 -0700
@@ -291,8 +291,8 @@
     public Buffer position(int newPosition) {
         if (newPosition > limit | newPosition < 0)
             throw createPositionException(newPosition);
+        if (mark > newPosition) mark = -1;
         position = newPosition;
-        if (mark > position) mark = -1;
         return this;
     }
 
@@ -481,7 +481,8 @@
      * @return  The number of elements remaining in this buffer
      */
     public final int remaining() {
-        return limit - position;
+        int rem = limit - position;
+        return rem > 0 ? rem : 0;
     }
 
     /**
--- a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Wed Dec 23 09:07:55 2020 +0100
+++ b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Wed Jul 29 09:52:13 2020 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -261,7 +261,9 @@
     public $Type$Buffer compact() {
 #if[rw]
         int pos = position();
-        int rem = limit() - pos;
+        int lim = limit();
+        assert (pos <= lim);
+        int rem = (pos <= lim ? lim - pos : 0);
         System.arraycopy(hb, ix(pos), hb, ix(0), rem);
         position(rem);
         limit(capacity());
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Wed Dec 23 09:07:55 2020 +0100
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Wed Jul 29 09:52:13 2020 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -440,15 +440,23 @@
      */
     public int read(CharBuffer target) throws IOException {
         // Determine the number of bytes n that can be transferred
+        int limit = limit();
+        int pos = position();
+        int remaining = limit - pos;
+        assert remaining >= 0;
+        if (remaining <= 0) // include equality condition when remaining == 0
+            return -1;
+
         int targetRemaining = target.remaining();
-        int limit = limit();
-        int remaining = limit - position();
-        if (remaining == 0)
-            return -1;
+        assert targetRemaining >= 0;
+        if (targetRemaining <= 0) // include condition targetRemaining == 0
+            return 0;
+
         int n = Math.min(remaining, targetRemaining);
+
         // Set source limit to prevent target overflow
         if (targetRemaining < remaining)
-            limit(position() + n);
+            limit(pos + n);
         try {
             if (n > 0)
                 target.put(this);