@@ -657,14 +657,18 @@ private void writeln(char[] buf) {
657
657
}
658
658
}
659
659
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 ) {
661
665
try {
662
666
synchronized (this ) {
663
667
ensureOpen ();
664
- textOut .write ( s );
668
+ textOut .append ( csq , start , end );
665
669
textOut .flushBuffer ();
666
670
charOut .flushBuffer ();
667
- if (autoFlush && (s . indexOf ('\n' ) >= 0 ))
671
+ if (autoFlush && (indexOf (csq , '\n' ) >= 0 ))
668
672
out .flush ();
669
673
}
670
674
}
@@ -676,6 +680,17 @@ private void write(String s) {
676
680
}
677
681
}
678
682
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
+
679
694
// Used to optimize away back-to-back flushing and synchronization when
680
695
// using println, but since subclasses could exist which depend on
681
696
// 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) {
1249
1264
/**
1250
1265
* Appends the specified character sequence to this output stream.
1251
1266
*
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
- *
1266
1267
* @param csq
1267
1268
* The character sequence to append. If {@code csq} is
1268
1269
* {@code null}, then the four characters {@code "null"} are
@@ -1273,23 +1274,15 @@ public PrintStream format(Locale l, String format, Object ... args) {
1273
1274
* @since 1.5
1274
1275
*/
1275
1276
public PrintStream append (CharSequence csq ) {
1276
- print (String .valueOf (csq ));
1277
+ if (csq == null ) csq = "null" ;
1278
+ write (csq );
1277
1279
return this ;
1278
1280
}
1279
1281
1280
1282
/**
1281
1283
* Appends a subsequence of the specified character sequence to this output
1282
1284
* stream.
1283
1285
*
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
- *
1293
1286
* @param csq
1294
1287
* The character sequence from which a subsequence will be
1295
1288
* appended. If {@code csq} is {@code null}, then characters
@@ -1314,7 +1307,8 @@ public PrintStream append(CharSequence csq) {
1314
1307
*/
1315
1308
public PrintStream append (CharSequence csq , int start , int end ) {
1316
1309
if (csq == null ) csq = "null" ;
1317
- return append (csq .subSequence (start , end ));
1310
+ write (csq , start , end );
1311
+ return this ;
1318
1312
}
1319
1313
1320
1314
/**
0 commit comments