8244136: Improved Buffer supports

Reviewed-by: alanb, ahgross, rhalade, psandoz
This commit is contained in:
Brian Burkhalter 2020-05-20 13:56:19 -07:00 committed by Henry Jen
parent 27f1ebc0af
commit a6723c8552
2 changed files with 52 additions and 24 deletions

View file

@ -155,20 +155,31 @@ class StringCharBuffer // package-private
if (!(ob instanceof CharBuffer)) if (!(ob instanceof CharBuffer))
return false; return false;
CharBuffer that = (CharBuffer)ob; CharBuffer that = (CharBuffer)ob;
if (this.remaining() != that.remaining()) int thisPos = this.position();
int thisRem = this.limit() - thisPos;
int thatPos = that.position();
int thatRem = that.limit() - thatPos;
if (thisRem < 0 || thisRem != thatRem)
return false; return false;
return BufferMismatch.mismatch(this, this.position(), return BufferMismatch.mismatch(this, thisPos,
that, that.position(), that, thatPos,
this.remaining()) < 0; thisRem) < 0;
} }
public int compareTo(CharBuffer that) { public int compareTo(CharBuffer that) {
int i = BufferMismatch.mismatch(this, this.position(), int thisPos = this.position();
that, that.position(), int thisRem = this.limit() - thisPos;
Math.min(this.remaining(), that.remaining())); int thatPos = that.position();
int thatRem = that.limit() - thatPos;
int length = Math.min(thisRem, thatRem);
if (length < 0)
return -1;
int i = BufferMismatch.mismatch(this, thisPos,
that, thatPos,
length);
if (i >= 0) { if (i >= 0) {
return Character.compare(this.get(this.position() + i), that.get(that.position() + i)); return Character.compare(this.get(thisPos + i), that.get(thatPos + i));
} }
return this.remaining() - that.remaining(); return thisRem - thatRem;
} }
} }

View file

@ -453,11 +453,11 @@ public abstract class $Type$Buffer
public int read(CharBuffer target) throws IOException { public int read(CharBuffer target) throws IOException {
// Determine the number of bytes n that can be transferred // Determine the number of bytes n that can be transferred
int targetRemaining = target.remaining(); int targetRemaining = target.remaining();
int remaining = remaining(); int limit = limit();
int remaining = limit - position();
if (remaining == 0) if (remaining == 0)
return -1; return -1;
int n = Math.min(remaining, targetRemaining); int n = Math.min(remaining, targetRemaining);
int limit = limit();
// Set source limit to prevent target overflow // Set source limit to prevent target overflow
if (targetRemaining < remaining) if (targetRemaining < remaining)
limit(position() + n); limit(position() + n);
@ -1599,11 +1599,15 @@ public abstract class $Type$Buffer
if (!(ob instanceof $Type$Buffer)) if (!(ob instanceof $Type$Buffer))
return false; return false;
$Type$Buffer that = ($Type$Buffer)ob; $Type$Buffer that = ($Type$Buffer)ob;
if (this.remaining() != that.remaining()) int thisPos = this.position();
int thisRem = this.limit() - thisPos;
int thatPos = that.position();
int thatRem = that.limit() - thatPos;
if (thisRem < 0 || thisRem != thatRem)
return false; return false;
return BufferMismatch.mismatch(this, this.position(), return BufferMismatch.mismatch(this, thisPos,
that, that.position(), that, thatPos,
this.remaining()) < 0; thisRem) < 0;
} }
/** /**
@ -1630,13 +1634,20 @@ public abstract class $Type$Buffer
* is less than, equal to, or greater than the given buffer * is less than, equal to, or greater than the given buffer
*/ */
public int compareTo($Type$Buffer that) { public int compareTo($Type$Buffer that) {
int i = BufferMismatch.mismatch(this, this.position(), int thisPos = this.position();
that, that.position(), int thisRem = this.limit() - thisPos;
Math.min(this.remaining(), that.remaining())); int thatPos = that.position();
int thatRem = that.limit() - thatPos;
int length = Math.min(thisRem, thatRem);
if (length < 0)
return -1;
int i = BufferMismatch.mismatch(this, thisPos,
that, thatPos,
length);
if (i >= 0) { if (i >= 0) {
return compare(this.get(this.position() + i), that.get(that.position() + i)); return compare(this.get(thisPos + i), that.get(thatPos + i));
} }
return this.remaining() - that.remaining(); return thisRem - thatRem;
} }
private static int compare($type$ x, $type$ y) { private static int compare($type$ x, $type$ y) {
@ -1675,11 +1686,17 @@ public abstract class $Type$Buffer
* @since 11 * @since 11
*/ */
public int mismatch($Type$Buffer that) { public int mismatch($Type$Buffer that) {
int length = Math.min(this.remaining(), that.remaining()); int thisPos = this.position();
int r = BufferMismatch.mismatch(this, this.position(), int thisRem = this.limit() - thisPos;
that, that.position(), int thatPos = that.position();
int thatRem = that.limit() - thatPos;
int length = Math.min(thisRem, thatRem);
if (length < 0)
return -1;
int r = BufferMismatch.mismatch(this, thisPos,
that, thatPos,
length); length);
return (r == -1 && this.remaining() != that.remaining()) ? length : r; return (r == -1 && thisRem != thatRem) ? length : r;
} }
// -- Other char stuff -- // -- Other char stuff --