Skip to content

Commit 1bb8ac0

Browse files
committed
Must not change implementation of StringWriter as getBuffer would not reflect current content of internally used Writer
1 parent 6e872eb commit 1bb8ac0

File tree

1 file changed

+11
-38
lines changed

1 file changed

+11
-38
lines changed

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

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
public class StringWriter extends Writer {
4848

4949
private final StringBuffer buf;
50-
private final Writer w;
5150

5251
/**
5352
* Create a new string writer using the default initial string-buffer
@@ -56,7 +55,6 @@ public class StringWriter extends Writer {
5655
public StringWriter() {
5756
buf = new StringBuffer();
5857
lock = buf;
59-
w = Writer.of(buf);
6058
}
6159

6260
/**
@@ -76,18 +74,13 @@ public StringWriter(int initialSize) {
7674
}
7775
buf = new StringBuffer(initialSize);
7876
lock = buf;
79-
w = Writer.of(buf);
8077
}
8178

8279
/**
8380
* Write a single character.
8481
*/
8582
public void write(int c) {
86-
try {
87-
w.write(c);
88-
} catch (IOException e) {
89-
throw new UncheckedIOException(e);
90-
}
83+
buf.append((char) c);
9184
}
9285

9386
/**
@@ -103,22 +96,18 @@ public void write(int c) {
10396
* of the given array
10497
*/
10598
public void write(char[] cbuf, int off, int len) {
106-
try {
107-
w.write(cbuf, off, len);
108-
} catch (IOException e) {
109-
throw new UncheckedIOException(e);
99+
Objects.checkFromIndexSize(off, len, cbuf.length);
100+
if (len == 0) {
101+
return;
110102
}
103+
buf.append(cbuf, off, len);
111104
}
112105

113106
/**
114107
* Write a string.
115108
*/
116109
public void write(String str) {
117-
try {
118-
w.write(str);
119-
} catch (IOException e) {
120-
throw new UncheckedIOException(e);
121-
}
110+
buf.append(str);
122111
}
123112

124113
/**
@@ -134,11 +123,7 @@ public void write(String str) {
134123
* of the given string
135124
*/
136125
public void write(String str, int off, int len) {
137-
try {
138-
w.write(str, off, len);
139-
} catch (IOException e) {
140-
throw new UncheckedIOException(e);
141-
}
126+
buf.append(str, off, off + len);
142127
}
143128

144129
/**
@@ -168,11 +153,7 @@ public void write(String str, int off, int len) {
168153
* @since 1.5
169154
*/
170155
public StringWriter append(CharSequence csq) {
171-
try {
172-
w.append(csq);
173-
} catch (IOException e) {
174-
throw new UncheckedIOException(e);
175-
}
156+
write(String.valueOf(csq));
176157
return this;
177158
}
178159

@@ -211,12 +192,8 @@ public StringWriter append(CharSequence csq) {
211192
* @since 1.5
212193
*/
213194
public StringWriter append(CharSequence csq, int start, int end) {
214-
try {
215-
w.append(csq, start, end);
216-
} catch (IOException e) {
217-
throw new UncheckedIOException(e);
218-
}
219-
return this;
195+
if (csq == null) csq = "null";
196+
return append(csq.subSequence(start, end));
220197
}
221198

222199
/**
@@ -237,11 +214,7 @@ public StringWriter append(CharSequence csq, int start, int end) {
237214
* @since 1.5
238215
*/
239216
public StringWriter append(char c) {
240-
try {
241-
w.append(c);
242-
} catch (IOException e) {
243-
throw new UncheckedIOException(e);
244-
}
217+
write(c);
245218
return this;
246219
}
247220

0 commit comments

Comments
 (0)