Skip to content

Commit a1fb6d5

Browse files
committed
java.io.Writer utilizes java.lang.CharSequence.getChars
1 parent cb2a2ef commit a1fb6d5

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/java.base/share/classes/java/io/Writer.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 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
@@ -275,6 +275,10 @@ public void write(String str) throws IOException {
275275
* If an I/O error occurs
276276
*/
277277
public void write(String str, int off, int len) throws IOException {
278+
implWrite(str, off, len);
279+
}
280+
281+
void implWrite(CharSequence csq, int off, int len) throws IOException {
278282
synchronized (lock) {
279283
char cbuf[];
280284
if (len <= WRITE_BUFFER_SIZE) {
@@ -285,7 +289,7 @@ public void write(String str, int off, int len) throws IOException {
285289
} else { // Don't permanently allocate very large buffers.
286290
cbuf = new char[len];
287291
}
288-
str.getChars(off, (off + len), cbuf, 0);
292+
csq.getChars(off, (off + len), cbuf, 0);
289293
write(cbuf, 0, len);
290294
}
291295
}
@@ -298,15 +302,9 @@ public void write(String str, int off, int len) throws IOException {
298302
* as the invocation
299303
*
300304
* {@snippet lang=java :
301-
* out.write(csq.toString())
305+
* out.append(csq, 0, csq.length())
302306
* }
303307
*
304-
* <p> Depending on the specification of {@code toString} for the
305-
* character sequence {@code csq}, the entire sequence may not be
306-
* appended. For instance, invoking the {@code toString} method of a
307-
* character buffer will return a subsequence whose content depends upon
308-
* the buffer's position and limit.
309-
*
310308
* @param csq
311309
* The character sequence to append. If {@code csq} is
312310
* {@code null}, then the four characters {@code "null"} are
@@ -320,22 +318,13 @@ public void write(String str, int off, int len) throws IOException {
320318
* @since 1.5
321319
*/
322320
public Writer append(CharSequence csq) throws IOException {
323-
write(String.valueOf(csq));
321+
append(csq, 0, csq.length());
324322
return this;
325323
}
326324

327325
/**
328326
* Appends a subsequence of the specified character sequence to this writer.
329327
*
330-
* <p> An invocation of this method of the form
331-
* {@code out.append(csq, start, end)} when {@code csq}
332-
* is not {@code null} behaves in exactly the
333-
* same way as the invocation
334-
*
335-
* {@snippet lang=java :
336-
* out.write(csq.subSequence(start, end).toString())
337-
* }
338-
*
339328
* @param csq
340329
* The character sequence from which a subsequence will be
341330
* appended. If {@code csq} is {@code null}, then characters
@@ -363,7 +352,8 @@ public Writer append(CharSequence csq) throws IOException {
363352
*/
364353
public Writer append(CharSequence csq, int start, int end) throws IOException {
365354
if (csq == null) csq = "null";
366-
return append(csq.subSequence(start, end));
355+
implWrite(csq, start, end - start);
356+
return this;
367357
}
368358

369359
/**

0 commit comments

Comments
 (0)