@@ -199,40 +199,41 @@ public void printStackTrace(PrintWriter s) {
199199 * Special handling for printing out a {@code CompositeException}.
200200 * Loops through all inner exceptions and prints them out.
201201 *
202- * @param s
202+ * @param output
203203 * stream to print to
204204 */
205- private void printStackTrace (PrintStreamOrWriter s ) {
206- StringBuilder b = new StringBuilder (128 );
207- b .append (this ).append ('\n' );
205+ private void printStackTrace (PrintStreamOrWriter output ) {
206+ output .append (this ).append ("\n " );
208207 for (StackTraceElement myStackElement : getStackTrace ()) {
209- b .append ("\t at " ).append (myStackElement ).append ('\n' );
208+ output .append ("\t at " ).append (myStackElement ).append (" \n " );
210209 }
211210 int i = 1 ;
212211 for (Throwable ex : exceptions ) {
213- b .append (" ComposedException " ).append (i ).append (" :\n " );
214- appendStackTrace (b , ex , "\t " );
212+ output .append (" ComposedException " ).append (i ).append (" :\n " );
213+ appendStackTrace (output , ex , "\t " );
215214 i ++;
216215 }
217- s . println ( b . toString () );
216+ output . append ( " \n " );
218217 }
219218
220- private void appendStackTrace (StringBuilder b , Throwable ex , String prefix ) {
221- b .append (prefix ).append (ex ).append ('\n' );
219+ private void appendStackTrace (PrintStreamOrWriter output , Throwable ex , String prefix ) {
220+ output .append (prefix ).append (ex ).append ('\n' );
222221 for (StackTraceElement stackElement : ex .getStackTrace ()) {
223- b .append ("\t \t at " ).append (stackElement ).append ('\n' );
222+ output .append ("\t \t at " ).append (stackElement ).append ('\n' );
224223 }
225224 if (ex .getCause () != null ) {
226- b .append ("\t Caused by: " );
227- appendStackTrace (b , ex .getCause (), "" );
225+ output .append ("\t Caused by: " );
226+ appendStackTrace (output , ex .getCause (), "" );
228227 }
229228 }
230229
231230 abstract static class PrintStreamOrWriter {
232- /** Prints the specified string as a line on this StreamOrWriter.
233- * @param o string to print
231+ /**
232+ * Prints the object's string representation via the underlying PrintStream or PrintWriter.
233+ * @param o the object to print
234+ * @return this
234235 */
235- abstract void println (Object o );
236+ abstract PrintStreamOrWriter append (Object o );
236237 }
237238
238239 /**
@@ -246,11 +247,15 @@ static final class WrappedPrintStream extends PrintStreamOrWriter {
246247 }
247248
248249 @ Override
249- void println (Object o ) {
250- printStream .println (o );
250+ WrappedPrintStream append (Object o ) {
251+ printStream .print (o );
252+ return this ;
251253 }
252254 }
253255
256+ /**
257+ * Same abstraction and implementation as in JDK to allow PrintStream and PrintWriter to share implementation.
258+ */
254259 static final class WrappedPrintWriter extends PrintStreamOrWriter {
255260 private final PrintWriter printWriter ;
256261
@@ -259,8 +264,9 @@ static final class WrappedPrintWriter extends PrintStreamOrWriter {
259264 }
260265
261266 @ Override
262- void println (Object o ) {
263- printWriter .println (o );
267+ WrappedPrintWriter append (Object o ) {
268+ printWriter .print (o );
269+ return this ;
264270 }
265271 }
266272
0 commit comments