# HG changeset patch # User bpb # Date 1596041533 25200 # Node ID 8f805fb8d9f3f3fa9a7144b2eaadda699a7e10ef # Parent 102b03f903a3337d38d5bd066757a37da5c73ab3 8247619: Improve Direct Buffering of Characters Reviewed-by: alanb, ahgross, rhalade, psandoz diff -r 102b03f903a3 -r 8f805fb8d9f3 src/share/classes/java/nio/Buffer.java --- 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; } /** diff -r 102b03f903a3 -r 8f805fb8d9f3 src/share/classes/java/nio/Heap-X-Buffer.java.template --- 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()); diff -r 102b03f903a3 -r 8f805fb8d9f3 src/share/classes/java/nio/X-Buffer.java.template --- 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);