KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > util > FileHelper


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 21, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.pentaho.messages.Messages;
26 import org.pentaho.messages.util.LocaleHelper;
27 import org.pentaho.util.logging.Logger;
28
29 public class FileHelper {
30
31     public static String JavaDoc getStringFromInputStream(InputStream JavaDoc is) {
32       try {
33         InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(is, LocaleHelper.getSystemEncoding());
34
35         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
36         if (reader != null) {
37             int bytesRead;
38             char buffer[] = new char[1000];
39             while ((bytesRead = reader.read(buffer)) > 0) {
40                 sb.append(buffer, 0, bytesRead);
41             }
42         }
43         return sb.toString();
44       } catch (Exception JavaDoc e) {
45         Logger.error(FileHelper.class.getName(), Messages.getErrorString("FileUtil.ERROR_0001_ERROR", e.getMessage()), e); //$NON-NLS-1$
46
} finally {
47         try {
48           is.close();
49         } catch (Exception JavaDoc ex) {
50           Logger.error(FileHelper.class.getName(), Messages.getErrorString("FileUtil.ERROR_0001_ERROR", ex.getMessage()), ex); //$NON-NLS-1$
51
}
52       }
53       return null;
54     }
55   
56     public static String JavaDoc getStringFromFile(File JavaDoc f) {
57         try {
58           FileInputStream JavaDoc fin = new FileInputStream JavaDoc(f);
59           return getStringFromInputStream(fin);
60         } catch (Exception JavaDoc e) {
61             Logger.error(FileHelper.class.getName(), Messages.getErrorString("FileUtil.ERROR_0001_ERROR", e.getMessage()), e); //$NON-NLS-1$
62
}
63         return null;
64     }
65
66 }
67
Popular Tags