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
src/java.base/share/classes/java/nio

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

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