Skip to content

Commit 0dd7c69

Browse files
author
Brian Burkhalter
committed
8357286: (bf) Remove obsolete instanceof checks in CharBuffer.append
Reviewed-by: alanb
1 parent 66535fe commit 0dd7c69

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,13 @@ class Direct$Type$Buffer$RW$$BO$
430430

431431
#if[rw]
432432
private static final int APPEND_BUF_SIZE = 1024;
433+
#end[rw]
434+
435+
public $Type$Buffer append(CharSequence csq, int start, int end) {
436+
#if[rw]
437+
if (csq == null)
438+
return super.append(csq, start, end);
433439

434-
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
435440
Objects.checkFromToIndex(start, end, csq.length());
436441

437442
int pos = position();
@@ -448,13 +453,7 @@ class Direct$Type$Buffer$RW$$BO$
448453
if (count > buf.length)
449454
count = buf.length;
450455

451-
if (csq instanceof String str) {
452-
str.getChars(start, start + count, buf, 0);
453-
} else if (csq instanceof StringBuilder sb) {
454-
sb.getChars(start, start + count, buf, 0);
455-
} else if (csq instanceof StringBuffer sb) {
456-
sb.getChars(start, start + count, buf, 0);
457-
}
456+
csq.getChars(start, start + count, buf, 0);
458457

459458
putArray(index, buf, 0, count);
460459

@@ -465,27 +464,19 @@ class Direct$Type$Buffer$RW$$BO$
465464
position(pos + length);
466465

467466
return this;
468-
}
469-
#end[rw]
470467

471-
public $Type$Buffer append(CharSequence csq) {
472-
#if[rw]
473-
if (csq instanceof StringBuilder)
474-
return appendChars(csq, 0, csq.length());
475-
476-
return super.append(csq);
477468
#else[rw]
478469
throw new ReadOnlyBufferException();
479470
#end[rw]
480471
}
481-
482-
public $Type$Buffer append(CharSequence csq, int start, int end) {
472+
473+
public $Type$Buffer append(CharSequence csq) {
483474
#if[rw]
484-
if (csq instanceof String || csq instanceof StringBuffer ||
485-
csq instanceof StringBuilder)
486-
return appendChars(csq, start, end);
475+
// See comment regarding StringBuilder on HeapBuffer.append.
476+
if (csq instanceof StringBuilder)
477+
return append(csq, 0, csq.length());
487478

488-
return super.append(csq, start, end);
479+
return super.append(csq);
489480
#else[rw]
490481
throw new ReadOnlyBufferException();
491482
#end[rw]

src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -290,7 +290,11 @@ class Heap$Type$Buffer$RW$
290290
// or the full sequence of chars is being appended, copying the chars to
291291
// an intermedite String in StringBuilder::toString is avoided.
292292
//
293-
private $Type$Buffer appendChars(CharSequence csq, int start, int end) {
293+
public $Type$Buffer append(CharSequence csq, int start, int end) {
294+
#if[rw]
295+
if (csq == null)
296+
return super.append(csq, start, end);
297+
294298
checkSession();
295299

296300
Objects.checkFromToIndex(start, end, csq.length());
@@ -302,37 +306,23 @@ class Heap$Type$Buffer$RW$
302306
if (length > rem)
303307
throw new BufferOverflowException();
304308

305-
if (csq instanceof String str) {
306-
str.getChars(start, end, hb, ix(pos));
307-
} else if (csq instanceof StringBuilder sb) {
308-
sb.getChars(start, end, hb, ix(pos));
309-
} else if (csq instanceof StringBuffer sb) {
310-
sb.getChars(start, end, hb, ix(pos));
311-
}
309+
csq.getChars(start, end, hb, ix(pos));
312310

313311
position(pos + length);
314312

315313
return this;
316-
}
317-
318-
public $Type$Buffer append(CharSequence csq) {
319-
#if[rw]
320-
if (csq instanceof StringBuilder)
321-
return appendChars(csq, 0, csq.length());
322-
323-
return super.append(csq);
324314
#else[rw]
325315
throw new ReadOnlyBufferException();
326316
#end[rw]
327317
}
328318

329-
public $Type$Buffer append(CharSequence csq, int start, int end) {
319+
public $Type$Buffer append(CharSequence csq) {
330320
#if[rw]
331-
if (csq instanceof String || csq instanceof StringBuffer ||
332-
csq instanceof StringBuilder)
333-
return appendChars(csq, start, end);
321+
// See comment regarding StringBuilder on method append() above.
322+
if (csq instanceof StringBuilder)
323+
return append(csq, 0, csq.length());
334324

335-
return super.append(csq, start, end);
325+
return super.append(csq);
336326
#else[rw]
337327
throw new ReadOnlyBufferException();
338328
#end[rw]

0 commit comments

Comments
 (0)