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
@@ -146,31 +146,25 @@ public void close() throws IOException {
146146 }
147147
148148 /**
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.
154152 *
155153 * <p> The resulting writer is not safe for use by multiple
156154 * concurrent threads. If the writer is to be used by more than one
157155 * thread it should be controlled by appropriate synchronization.
158156 *
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
160158 * changes, the behavior is undefined.
161159 *
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}
166163 *
167164 * @since 25
168165 */
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 );
174168
175169 return new Writer () {
176170 private boolean isClosed ;
@@ -184,7 +178,7 @@ private void ensureOpen() throws IOException {
184178 @ Override
185179 public void write (int c ) throws IOException {
186180 ensureOpen ();
187- a .append ((char ) c );
181+ sb .append ((char ) c );
188182 }
189183
190184 @ Override
@@ -194,73 +188,49 @@ public void write(char[] cbuf, int off, int len) throws IOException {
194188 if (len == 0 ) {
195189 return ;
196190 }
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 );
206192 }
207193
208194 @ Override
209195 public void write (String str ) throws IOException {
210196 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 );
217198 }
218199
219200 @ Override
220201 public void write (String str , int off , int len ) throws IOException {
221202 ensureOpen ();
222- a .append (str , off , off + len );
203+ sb .append (str , off , off + len );
223204 }
224205
225206 @ Override
226207 public Writer append (CharSequence csq ) throws IOException {
227208 ensureOpen ();
228- a .append (csq );
209+ sb .append (csq );
229210 return this ;
230211 }
231212
232213 @ Override
233214 public Writer append (CharSequence csq , int start , int end ) throws IOException {
234215 ensureOpen ();
235- a .append (csq , start , end );
216+ sb .append (csq , start , end );
236217 return this ;
237218 }
238219
239220 @ Override
240221 public Writer append (char c ) throws IOException {
241222 ensureOpen ();
242- a .append (c );
223+ sb .append (c );
243224 return this ;
244225 }
245226
246227 @ Override
247228 public void flush () throws IOException {
248229 ensureOpen ();
249- implFlush ();
250- }
251-
252- private void implFlush () throws IOException {
253- if (a instanceof Flushable f )
254- f .flush ();
255230 }
256231
257232 @ Override
258233 public void close () throws IOException {
259- if (isClosed )
260- return ;
261- implFlush ();
262- if (a instanceof Closable c )
263- c .close ();
264234 isClosed = true ;
265235 }
266236 };
0 commit comments