Skip to content

Commit 15c20f5

Browse files
committed
java.io.PrintStream prevents CharSequence.toString
1 parent f755f63 commit 15c20f5

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

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

+25-28
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,21 @@ private void writeln(char[] buf) {
657657
}
658658
}
659659

660-
private void write(String s) {
660+
private void write(CharSequence csq) {
661+
write(csq, 0, csq.length());
662+
}
663+
664+
private void write(CharSequence csq, int start, int end) {
661665
try {
662666
synchronized (this) {
663667
ensureOpen();
664-
textOut.write(s);
668+
if (csq instanceof String s)
669+
textOut.write(s, start, end - start);
670+
else
671+
textOut.append(csq, start, end);
665672
textOut.flushBuffer();
666673
charOut.flushBuffer();
667-
if (autoFlush && (s.indexOf('\n') >= 0))
674+
if (autoFlush && (indexOf(csq, '\n') >= 0))
668675
out.flush();
669676
}
670677
}
@@ -676,6 +683,17 @@ private void write(String s) {
676683
}
677684
}
678685

686+
private static int indexOf(CharSequence csq, char c) {
687+
if (csq instanceof String s)
688+
return s.indexOf(c);
689+
690+
for (int i = 0, n = csq.length(); i < n; i++)
691+
if (csq.charAt(i) == c)
692+
return i;
693+
694+
return -1;
695+
}
696+
679697
// Used to optimize away back-to-back flushing and synchronization when
680698
// using println, but since subclasses could exist which depend on
681699
// observing a call to print followed by newLine we only use this if
@@ -1249,20 +1267,6 @@ public PrintStream format(Locale l, String format, Object ... args) {
12491267
/**
12501268
* Appends the specified character sequence to this output stream.
12511269
*
1252-
* <p> An invocation of this method of the form {@code out.append(csq)}
1253-
* when {@code csq} is not {@code null}, behaves in exactly the same way
1254-
* as the invocation
1255-
*
1256-
* {@snippet lang=java :
1257-
* out.print(csq.toString())
1258-
* }
1259-
*
1260-
* <p> Depending on the specification of {@code toString} for the
1261-
* character sequence {@code csq}, the entire sequence may not be
1262-
* appended. For instance, invoking the {@code toString} method of a
1263-
* character buffer will return a subsequence whose content depends upon
1264-
* the buffer's position and limit.
1265-
*
12661270
* @param csq
12671271
* The character sequence to append. If {@code csq} is
12681272
* {@code null}, then the four characters {@code "null"} are
@@ -1273,23 +1277,15 @@ public PrintStream format(Locale l, String format, Object ... args) {
12731277
* @since 1.5
12741278
*/
12751279
public PrintStream append(CharSequence csq) {
1276-
print(String.valueOf(csq));
1280+
if (csq == null) csq = "null";
1281+
write(csq);
12771282
return this;
12781283
}
12791284

12801285
/**
12811286
* Appends a subsequence of the specified character sequence to this output
12821287
* stream.
12831288
*
1284-
* <p> An invocation of this method of the form
1285-
* {@code out.append(csq, start, end)} when
1286-
* {@code csq} is not {@code null}, behaves in
1287-
* exactly the same way as the invocation
1288-
*
1289-
* {@snippet lang=java :
1290-
* out.print(csq.subSequence(start, end).toString())
1291-
* }
1292-
*
12931289
* @param csq
12941290
* The character sequence from which a subsequence will be
12951291
* appended. If {@code csq} is {@code null}, then characters
@@ -1314,7 +1310,8 @@ public PrintStream append(CharSequence csq) {
13141310
*/
13151311
public PrintStream append(CharSequence csq, int start, int end) {
13161312
if (csq == null) csq = "null";
1317-
return append(csq.subSequence(start, end));
1313+
write(csq, start, end);
1314+
return this;
13181315
}
13191316

13201317
/**

0 commit comments

Comments
 (0)