Skip to content

Commit 4768e13

Browse files
committed
java.io.PrintStream prevents CharSequence.toString
1 parent e28f740 commit 4768e13

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

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

+22-28
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,18 @@ 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+
textOut.append(csq, start, end);
665669
textOut.flushBuffer();
666670
charOut.flushBuffer();
667-
if (autoFlush && (s.indexOf('\n') >= 0))
671+
if (autoFlush && (indexOf(csq, '\n') >= 0))
668672
out.flush();
669673
}
670674
}
@@ -676,6 +680,17 @@ private void write(String s) {
676680
}
677681
}
678682

683+
private static int indexOf(CharSequence csq, char c) {
684+
if (csq instanceof String s)
685+
return s.indexOf(c);
686+
687+
for (int i = 0, n = csq.length(); i < n; i++)
688+
if (csq.charAt(i) == c)
689+
return i;
690+
691+
return -1;
692+
}
693+
679694
// Used to optimize away back-to-back flushing and synchronization when
680695
// using println, but since subclasses could exist which depend on
681696
// observing a call to print followed by newLine we only use this if
@@ -1249,20 +1264,6 @@ public PrintStream format(Locale l, String format, Object ... args) {
12491264
/**
12501265
* Appends the specified character sequence to this output stream.
12511266
*
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-
*
12661267
* @param csq
12671268
* The character sequence to append. If {@code csq} is
12681269
* {@code null}, then the four characters {@code "null"} are
@@ -1273,23 +1274,15 @@ public PrintStream format(Locale l, String format, Object ... args) {
12731274
* @since 1.5
12741275
*/
12751276
public PrintStream append(CharSequence csq) {
1276-
print(String.valueOf(csq));
1277+
if (csq == null) csq = "null";
1278+
write(csq);
12771279
return this;
12781280
}
12791281

12801282
/**
12811283
* Appends a subsequence of the specified character sequence to this output
12821284
* stream.
12831285
*
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-
*
12931286
* @param csq
12941287
* The character sequence from which a subsequence will be
12951288
* appended. If {@code csq} is {@code null}, then characters
@@ -1314,7 +1307,8 @@ public PrintStream append(CharSequence csq) {
13141307
*/
13151308
public PrintStream append(CharSequence csq, int start, int end) {
13161309
if (csq == null) csq = "null";
1317-
return append(csq.subSequence(start, end));
1310+
write(csq, start, end);
1311+
return this;
13181312
}
13191313

13201314
/**

0 commit comments

Comments
 (0)