1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -146,31 +146,25 @@ public void close() throws IOException {
146
146
}
147
147
148
148
/**
149
- * Returns a {@code Writer} that writes characters into an
150
- * {@code Appendable}. The writer is initially open and writing appends
151
- * after the last character in the appendable.
152
- *
153
- * <p> If the appendable is a {code Writer}, it is returned.
149
+ * Returns a {@code Writer} that writes characters into a
150
+ * {@code StringBuilder}. The writer is initially open and writing appends
151
+ * after the last character in the string builder.
154
152
*
155
153
* <p> The resulting writer is not safe for use by multiple
156
154
* concurrent threads. If the writer is to be used by more than one
157
155
* thread it should be controlled by appropriate synchronization.
158
156
*
159
- * <p> If the appendable changes while the writer is open, e.g. the length
157
+ * <p> If the string builder changes while the writer is open, e.g. the length
160
158
* changes, the behavior is undefined.
161
159
*
162
- * @param a {@code Appendable} consuming the character stream.
163
- * @return a {@code Writer} which writes characters into {@code a}, or
164
- * {@code a} if it is a {@code Writer}.
165
- * @throws NullPointerException if {@code a} is {@code null}
160
+ * @param sb {@code StringBuilder} consuming the character stream.
161
+ * @return a {@code Writer} which writes characters into {@code sb}.
162
+ * @throws NullPointerException if {@code sb} is {@code null}
166
163
*
167
164
* @since 25
168
165
*/
169
- public static Writer of (final Appendable a ) {
170
- Objects .requireNonNull (a );
171
-
172
- if (a instanceof Writer w )
173
- return w ;
166
+ public static Writer of (final StringBuilder sb ) {
167
+ Objects .requireNonNull (sb );
174
168
175
169
return new Writer () {
176
170
private boolean isClosed ;
@@ -184,7 +178,7 @@ private void ensureOpen() throws IOException {
184
178
@ Override
185
179
public void write (int c ) throws IOException {
186
180
ensureOpen ();
187
- a .append ((char ) c );
181
+ sb .append ((char ) c );
188
182
}
189
183
190
184
@ Override
@@ -194,73 +188,49 @@ public void write(char[] cbuf, int off, int len) throws IOException {
194
188
if (len == 0 ) {
195
189
return ;
196
190
}
197
- switch (a ) {
198
- case StringBuilder sb -> sb .append (cbuf , off , len );
199
- case StringBuffer sb -> sb .append (cbuf , off , len );
200
- case CharBuffer cb -> cb .put (cbuf , off , len );
201
- default -> {
202
- for (int i = 0 ; i < len ; i ++)
203
- a .append (cbuf [off + i ]);
204
- }
205
- }
191
+ sb .append (cbuf , off , len );
206
192
}
207
193
208
194
@ Override
209
195
public void write (String str ) throws IOException {
210
196
ensureOpen ();
211
- switch (a ) {
212
- case StringBuilder sb -> sb .append (str );
213
- case StringBuffer sb -> sb .append (str );
214
- case CharBuffer cb -> cb .put (str );
215
- default -> a .append (str );
216
- }
197
+ sb .append (str );
217
198
}
218
199
219
200
@ Override
220
201
public void write (String str , int off , int len ) throws IOException {
221
202
ensureOpen ();
222
- a .append (str , off , off + len );
203
+ sb .append (str , off , off + len );
223
204
}
224
205
225
206
@ Override
226
207
public Writer append (CharSequence csq ) throws IOException {
227
208
ensureOpen ();
228
- a .append (csq );
209
+ sb .append (csq );
229
210
return this ;
230
211
}
231
212
232
213
@ Override
233
214
public Writer append (CharSequence csq , int start , int end ) throws IOException {
234
215
ensureOpen ();
235
- a .append (csq , start , end );
216
+ sb .append (csq , start , end );
236
217
return this ;
237
218
}
238
219
239
220
@ Override
240
221
public Writer append (char c ) throws IOException {
241
222
ensureOpen ();
242
- a .append (c );
223
+ sb .append (c );
243
224
return this ;
244
225
}
245
226
246
227
@ Override
247
228
public void flush () throws IOException {
248
229
ensureOpen ();
249
- implFlush ();
250
- }
251
-
252
- private void implFlush () throws IOException {
253
- if (a instanceof Flushable f )
254
- f .flush ();
255
230
}
256
231
257
232
@ Override
258
233
public void close () throws IOException {
259
- if (isClosed )
260
- return ;
261
- implFlush ();
262
- if (a instanceof Closable c )
263
- c .close ();
264
234
isClosed = true ;
265
235
}
266
236
};
0 commit comments