@@ -657,14 +657,21 @@ 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
+ if (csq instanceof String s )
669
+ textOut .write (s , start , end - start );
670
+ else
671
+ textOut .append (csq , start , end );
665
672
textOut .flushBuffer ();
666
673
charOut .flushBuffer ();
667
- if (autoFlush && (s . indexOf ('\n' ) >= 0 ))
674
+ if (autoFlush && (indexOf (csq , '\n' ) >= 0 ))
668
675
out .flush ();
669
676
}
670
677
}
@@ -676,6 +683,17 @@ private void write(String s) {
676
683
}
677
684
}
678
685
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
+
679
697
// Used to optimize away back-to-back flushing and synchronization when
680
698
// using println, but since subclasses could exist which depend on
681
699
// 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) {
1249
1267
/**
1250
1268
* Appends the specified character sequence to this output stream.
1251
1269
*
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
1270
* @param csq
1267
1271
* The character sequence to append. If {@code csq} is
1268
1272
* {@code null}, then the four characters {@code "null"} are
@@ -1273,23 +1277,15 @@ public PrintStream format(Locale l, String format, Object ... args) {
1273
1277
* @since 1.5
1274
1278
*/
1275
1279
public PrintStream append (CharSequence csq ) {
1276
- print (String .valueOf (csq ));
1280
+ if (csq == null ) csq = "null" ;
1281
+ write (csq );
1277
1282
return this ;
1278
1283
}
1279
1284
1280
1285
/**
1281
1286
* Appends a subsequence of the specified character sequence to this output
1282
1287
* stream.
1283
1288
*
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
1289
* @param csq
1294
1290
* The character sequence from which a subsequence will be
1295
1291
* appended. If {@code csq} is {@code null}, then characters
@@ -1314,7 +1310,8 @@ public PrintStream append(CharSequence csq) {
1314
1310
*/
1315
1311
public PrintStream append (CharSequence csq , int start , int end ) {
1316
1312
if (csq == null ) csq = "null" ;
1317
- return append (csq .subSequence (start , end ));
1313
+ write (csq , start , end );
1314
+ return this ;
1318
1315
}
1319
1316
1320
1317
/**
0 commit comments