KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > ThrowableToString


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.util;
24
25 // JDK classes
26
import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28
29 // our classes
30
import com.sun.enterprise.admin.util.Assert;
31
32
33 /**
34     Exists to convert a Throwable to a String. This gives more control
35     than having a printStackTrace() go directly to a PrintStream
36     or PrintWriter. It also defers the conversion to a String until a later
37     time when the caller finds it convenient.
38  */

39 public final class ThrowableToString
40 {
41     private final static int kDefaultBufferSize = 512;
42     
43     private final Throwable JavaDoc mThrowable;
44     
45     /**
46         Constructor--takes any non-null Throwable
47         
48         @param throwable any Throwable
49      */

50         public
51     ThrowableToString( Throwable JavaDoc throwable )
52     {
53         Assert.assertit( throwable != null, "null throwable" );
54         mThrowable = throwable;
55     }
56     
57     /**
58         Convert the Throwable to a String.
59      */

60         public String JavaDoc
61     toString()
62     {
63         final ByteArrayOutputStream JavaDoc output =
64                                     new ByteArrayOutputStream JavaDoc( kDefaultBufferSize );
65         final PrintStream JavaDoc print = new PrintStream JavaDoc( output );
66         
67         mThrowable.printStackTrace( print );
68         
69         return( output.toString() );
70     }
71 }
72
73
74
75
Popular Tags