Skip to content

Commit ee93d95

Browse files
committed
java.io.CharArrayWriter utilized java.lang.CharSequence.getChars
1 parent e2f6e7c commit ee93d95

File tree

1 file changed

+8
-40
lines changed

1 file changed

+8
-40
lines changed

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

+8-40
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
@@ -112,24 +112,14 @@ public void write(char[] c, int off, int len) {
112112
}
113113
}
114114

115-
/**
116-
* Write a portion of a string to the buffer.
117-
* @param str String to be written from
118-
* @param off Offset from which to start reading characters
119-
* @param len Number of characters to be written
120-
*
121-
* @throws IndexOutOfBoundsException
122-
* If {@code off} is negative, or {@code len} is negative,
123-
* or {@code off + len} is negative or greater than the length
124-
* of the given string
125-
*/
126-
public void write(String str, int off, int len) {
115+
@Override
116+
void implWrite(CharSequence csq, int off, int len) {
127117
synchronized (lock) {
128118
int newcount = count + len;
129119
if (newcount > buf.length) {
130120
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
131121
}
132-
str.getChars(off, off + len, buf, count);
122+
csq.getChars(off, off + len, buf, count);
133123
count = newcount;
134124
}
135125
}
@@ -149,20 +139,6 @@ public void writeTo(Writer out) throws IOException {
149139
/**
150140
* Appends the specified character sequence to this writer.
151141
*
152-
* <p> An invocation of this method of the form {@code out.append(csq)}
153-
* when {@code csq} is not {@code null}, behaves in exactly the same way
154-
* as the invocation
155-
*
156-
* {@snippet lang=java :
157-
* out.write(csq.toString())
158-
* }
159-
*
160-
* <p> Depending on the specification of {@code toString} for the
161-
* character sequence {@code csq}, the entire sequence may not be
162-
* appended. For instance, invoking the {@code toString} method of a
163-
* character buffer will return a subsequence whose content depends upon
164-
* the buffer's position and limit.
165-
*
166142
* @param csq
167143
* The character sequence to append. If {@code csq} is
168144
* {@code null}, then the four characters {@code "null"} are
@@ -173,23 +149,14 @@ public void writeTo(Writer out) throws IOException {
173149
* @since 1.5
174150
*/
175151
public CharArrayWriter append(CharSequence csq) {
176-
String s = String.valueOf(csq);
177-
write(s, 0, s.length());
152+
if (csq == null) csq = "null";
153+
implWrite(csq, 0, csq.length());
178154
return this;
179155
}
180156

181157
/**
182158
* Appends a subsequence of the specified character sequence to this writer.
183159
*
184-
* <p> An invocation of this method of the form
185-
* {@code out.append(csq, start, end)} when
186-
* {@code csq} is not {@code null}, behaves in
187-
* exactly the same way as the invocation
188-
*
189-
* {@snippet lang=java :
190-
* out.write(csq.subSequence(start, end).toString())
191-
* }
192-
*
193160
* @param csq
194161
* The character sequence from which a subsequence will be
195162
* appended. If {@code csq} is {@code null}, then characters
@@ -214,7 +181,8 @@ public CharArrayWriter append(CharSequence csq) {
214181
*/
215182
public CharArrayWriter append(CharSequence csq, int start, int end) {
216183
if (csq == null) csq = "null";
217-
return append(csq.subSequence(start, end));
184+
implWrite(csq, start, end - start);
185+
return this;
218186
}
219187

220188
/**

0 commit comments

Comments
 (0)