Skip to content

Commit 9210615

Browse files
committed
java.io.Writer utilizes java.lang.CharSequence.getChars
1 parent fa7c5a6 commit 9210615

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

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

Lines changed: 9 additions & 27 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,28 +289,14 @@ 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
}
292296

293297
/**
294298
* Appends the specified character sequence to this writer.
295299
*
296-
* <p> An invocation of this method of the form {@code out.append(csq)}
297-
* when {@code csq} is not {@code null}, behaves in exactly the same way
298-
* as the invocation
299-
*
300-
* {@snippet lang=java :
301-
* out.write(csq.toString())
302-
* }
303-
*
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-
*
310300
* @param csq
311301
* The character sequence to append. If {@code csq} is
312302
* {@code null}, then the four characters {@code "null"} are
@@ -320,22 +310,13 @@ public void write(String str, int off, int len) throws IOException {
320310
* @since 1.5
321311
*/
322312
public Writer append(CharSequence csq) throws IOException {
323-
write(String.valueOf(csq));
313+
append(csq, 0, csq.length());
324314
return this;
325315
}
326316

327317
/**
328318
* Appends a subsequence of the specified character sequence to this writer.
329319
*
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-
*
339320
* @param csq
340321
* The character sequence from which a subsequence will be
341322
* appended. If {@code csq} is {@code null}, then characters
@@ -363,7 +344,8 @@ public Writer append(CharSequence csq) throws IOException {
363344
*/
364345
public Writer append(CharSequence csq, int start, int end) throws IOException {
365346
if (csq == null) csq = "null";
366-
return append(csq.subSequence(start, end));
347+
implWrite(csq, start, end - start);
348+
return this;
367349
}
368350

369351
/**

0 commit comments

Comments
 (0)