changeset 10013:8f805fb8d9f3

8247619: Improve Direct Buffering of Characters Reviewed-by: alanb, ahgross, rhalade, psandoz
author bpb
date Wed, 29 Jul 2020 09:52:13 -0700
parents 102b03f903a3
children da9f439a68d6
files src/share/classes/java/nio/Buffer.java src/share/classes/java/nio/Heap-X-Buffer.java.template src/share/classes/java/nio/X-Buffer.java.template
diffstat 3 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/Buffer.java	Fri Dec 11 17:06:38 2020 +0000
+++ b/src/share/classes/java/nio/Buffer.java	Wed Jul 29 09:52:13 2020 -0700
@@ -234,8 +234,8 @@
     public final Buffer position(int newPosition) {
         if ((newPosition > limit) || (newPosition < 0))
             throw new IllegalArgumentException();
+        if (mark > newPosition) mark = -1;
         position = newPosition;
-        if (mark > position) mark = -1;
         return this;
     }
 
@@ -380,7 +380,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/share/classes/java/nio/Heap-X-Buffer.java.template	Fri Dec 11 17:06:38 2020 +0000
+++ b/src/share/classes/java/nio/Heap-X-Buffer.java.template	Wed Jul 29 09:52:13 2020 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2008, 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
@@ -228,7 +228,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/share/classes/java/nio/X-Buffer.java.template	Fri Dec 11 17:06:38 2020 +0000
+++ b/src/share/classes/java/nio/X-Buffer.java.template	Wed Jul 29 09:52:13 2020 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2011, 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
@@ -410,15 +410,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);