KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > FileUtils


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  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/misc/FileUtils.java,v 1.4 2005/12/25 03:51:45 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:51:45 $
28  */

29  
30
31 package com.sun.appserv.management.util.misc;
32
33 import java.io.File JavaDoc;
34 import java.io.FileReader JavaDoc;
35 import java.io.FileWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38
39 /**
40     The API that should be used to output from a Cmd running within the framework.
41  */

42 public final class FileUtils
43 {
44     private FileUtils() {}
45     
46     
47         public static String JavaDoc
48     fileToString( final File JavaDoc src )
49         throws FileNotFoundException JavaDoc, IOException JavaDoc
50     {
51         return fileToString( src, 32 * 1024 );
52     }
53     
54         public static String JavaDoc
55     fileToString( final File JavaDoc src, final int readBufferSize )
56         throws FileNotFoundException JavaDoc, IOException JavaDoc
57     {
58         final FileReader JavaDoc in = new FileReader JavaDoc( src );
59
60         final long length = src.length();
61         if ( length > 1024 * 1024 * 1024 )
62         {
63             throw new IllegalArgumentException JavaDoc();
64         }
65         
66         final char[] readBuffer = new char[ readBufferSize ];
67         
68         final StringBuilder JavaDoc result = new StringBuilder JavaDoc( (int)length );
69         try
70         {
71             while ( true )
72             {
73                 final int numRead = in.read( readBuffer, 0, readBufferSize );
74                 if ( numRead < 0 )
75                     break;
76                 
77                 result.append( readBuffer, 0, numRead );
78             }
79         }
80         finally
81         {
82             in.close();
83         }
84         
85         return( result.toString() );
86     }
87     
88         public static void
89     stringToFile( final String JavaDoc s, final File JavaDoc dest )
90         throws IOException JavaDoc
91     {
92         final FileWriter JavaDoc out = new FileWriter JavaDoc( dest );
93         
94         try
95         {
96             out.write( s );
97         }
98         finally
99         {
100             out.close();
101         }
102     }
103 };
104
105
106
Popular Tags