8357286: (bf) Remove obsolete instanceof checks in CharBuffer.append

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2025-06-12 20:21:50 +00:00
parent 66535fe26d
commit 0dd7c69b9e
2 changed files with 26 additions and 45 deletions

View File

@ -430,8 +430,13 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw]
private static final int APPEND_BUF_SIZE = 1024;
#end[rw]
public $Type$Buffer append(CharSequence csq, int start, int end) {
#if[rw]
if (csq == null)
return super.append(csq, start, end);
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
Objects.checkFromToIndex(start, end, csq.length());
int pos = position();
@ -448,13 +453,7 @@ class Direct$Type$Buffer$RW$$BO$
if (count > buf.length)
count = buf.length;
if (csq instanceof String str) {
str.getChars(start, start + count, buf, 0);
} else if (csq instanceof StringBuilder sb) {
sb.getChars(start, start + count, buf, 0);
} else if (csq instanceof StringBuffer sb) {
sb.getChars(start, start + count, buf, 0);
}
csq.getChars(start, start + count, buf, 0);
putArray(index, buf, 0, count);
@ -465,27 +464,19 @@ class Direct$Type$Buffer$RW$$BO$
position(pos + length);
return this;
}
#end[rw]
public $Type$Buffer append(CharSequence csq) {
#if[rw]
if (csq instanceof StringBuilder)
return appendChars(csq, 0, csq.length());
return super.append(csq);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer append(CharSequence csq, int start, int end) {
#if[rw]
if (csq instanceof String || csq instanceof StringBuffer ||
csq instanceof StringBuilder)
return appendChars(csq, start, end);
return super.append(csq, start, end);
public $Type$Buffer append(CharSequence csq) {
#if[rw]
// See comment regarding StringBuilder on HeapBuffer.append.
if (csq instanceof StringBuilder)
return append(csq, 0, csq.length());
return super.append(csq);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, 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
@ -290,7 +290,11 @@ class Heap$Type$Buffer$RW$
// or the full sequence of chars is being appended, copying the chars to
// an intermedite String in StringBuilder::toString is avoided.
//
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
public $Type$Buffer append(CharSequence csq, int start, int end) {
#if[rw]
if (csq == null)
return super.append(csq, start, end);
checkSession();
Objects.checkFromToIndex(start, end, csq.length());
@ -302,37 +306,23 @@ class Heap$Type$Buffer$RW$
if (length > rem)
throw new BufferOverflowException();
if (csq instanceof String str) {
str.getChars(start, end, hb, ix(pos));
} else if (csq instanceof StringBuilder sb) {
sb.getChars(start, end, hb, ix(pos));
} else if (csq instanceof StringBuffer sb) {
sb.getChars(start, end, hb, ix(pos));
}
csq.getChars(start, end, hb, ix(pos));
position(pos + length);
return this;
}
public $Type$Buffer append(CharSequence csq) {
#if[rw]
if (csq instanceof StringBuilder)
return appendChars(csq, 0, csq.length());
return super.append(csq);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer append(CharSequence csq, int start, int end) {
public $Type$Buffer append(CharSequence csq) {
#if[rw]
if (csq instanceof String || csq instanceof StringBuffer ||
csq instanceof StringBuilder)
return appendChars(csq, start, end);
// See comment regarding StringBuilder on method append() above.
if (csq instanceof StringBuilder)
return append(csq, 0, csq.length());
return super.append(csq, start, end);
return super.append(csq);
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]