Skip to content

Commit bf564cb

Browse files
committed
Limited to Writer.of(StringBuilder)
1 parent 1bb8ac0 commit bf564cb

File tree

4 files changed

+23
-93
lines changed

4 files changed

+23
-93
lines changed

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

Lines changed: 3 additions & 3 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
@@ -37,8 +37,8 @@
3737
* {@code IOException}.
3838
*
3939
* @apiNote
40-
* {@link Writer#of(Appendable)} provides a method to write into any
41-
* {@link Appendable} that may be more efficient than {@code StringWriter}.
40+
* {@link Writer#of(StringBuilder)} provides a method to write into an existing
41+
* {@link StringBuilder} that may be more efficient than {@code StringWriter}.
4242
*
4343
* @author Mark Reinhold
4444
* @since 1.1

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

Lines changed: 17 additions & 47 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
@@ -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
};

src/java.base/share/classes/java/nio/X-Buffer.java.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 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

test/jdk/java/io/Writer/Of.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 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
@@ -24,10 +24,6 @@
2424
import java.io.Writer;
2525
import java.io.StringWriter;
2626
import java.io.IOException;
27-
import java.io.StringWriter;
28-
import java.nio.ByteBuffer;
29-
import java.nio.CharBuffer;
30-
import java.nio.ReadOnlyBufferException;
3127
import java.util.function.Supplier;
3228

3329
import org.testng.annotations.*;
@@ -36,7 +32,7 @@
3632

3733
/*
3834
* @test
39-
* @bug 1234567
35+
* @bug 8353795
4036
* @summary Check for expected behavior of Writer.of().
4137
* @run testng Of
4238
*/
@@ -56,10 +52,7 @@ public String toString() {
5652
@DataProvider
5753
public static Config[] writers() {
5854
var sw = new StringWriter();
59-
var sbuf = new StringBuffer();
6055
var sbld = new StringBuilder();
61-
var dcb = ByteBuffer.allocateDirect(CONTENT.length() * 2).asCharBuffer();
62-
var wcb = CharBuffer.wrap(new char[CONTENT.length()]);
6356
var w = new Writer() {
6457
private String s = "";
6558
private boolean isClosed;
@@ -112,43 +105,10 @@ public String toString() {
112105
return s;
113106
}
114107
};
115-
var a = new Appendable() {
116-
private String s = "";
117-
118-
@Override
119-
public Appendable append(char c) throws IOException {
120-
s += c;
121-
return this;
122-
}
123-
124-
@Override
125-
public Appendable append(CharSequence csq) throws IOException {
126-
s += String.valueOf(csq);
127-
return this;
128-
}
129-
130-
@Override
131-
public Appendable append(CharSequence csq, int start, int end)
132-
throws IOException {
133-
s += String.valueOf(csq).subSequence(start, end);
134-
return this;
135-
}
136-
137-
@Override
138-
public String toString() {
139-
return s;
140-
}
141-
};
142108
return new Config[] {
143109
new Config("StringWriter", sw, sw::toString),
144-
new Config("StringBuffer", Writer.of(sbuf), sbuf::toString),
145110
new Config("StringBuilder", Writer.of(sbld), sbld::toString),
146-
new Config("Direct CharBuffer", Writer.of(dcb),
147-
() -> { dcb.flip(); return dcb.toString(); }),
148-
new Config("Wrapped CharBuffer", Writer.of(wcb),
149-
() -> { wcb.flip(); return wcb.toString(); }),
150111
new Config("Custom Writer", w, w::toString),
151-
new Config("Custom Appendable", Writer.of(a), a::toString)
152112
};
153113
}
154114

0 commit comments

Comments
 (0)