001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jun 21, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.util;
019:
020: import java.io.BufferedReader;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.Reader;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.messages.util.LocaleHelper;
033: import org.pentaho.util.logging.Logger;
034:
035: public class FileHelper {
036:
037: private static final Log logger = LogFactory
038: .getLog(FileHelper.class);
039:
040: private static final int BUFF_SZ = 1024;
041:
042: /**
043: *
044: * Note: wrap the InputStreamReader in a BufferedReader for efficiency. See:
045: * http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStreamReader.html
046: *
047: * @param is
048: * @return
049: */
050: public static String getStringFromInputStream(InputStream is) {
051: Reader reader = null;
052: try {
053: reader = new BufferedReader(new InputStreamReader(is,
054: LocaleHelper.getSystemEncoding()), BUFF_SZ);
055:
056: StringBuffer sb = new StringBuffer();
057: if (reader != null) {
058: int bytesRead;
059: char buffer[] = new char[BUFF_SZ];
060: while ((bytesRead = reader.read(buffer)) > 0) {
061: sb.append(buffer, 0, bytesRead);
062: }
063: }
064: return sb.toString();
065: } catch (Exception e) {
066: Logger
067: .error(
068: FileHelper.class.getName(),
069: Messages
070: .getErrorString(
071: "FileUtil.ERROR_0001_ERROR", e.getMessage()), e); //$NON-NLS-1$
072: } finally {
073: closeReader(reader);
074: }
075: return null;
076: }
077:
078: public static String getStringFromFile(File f) {
079: FileInputStream fin = null;
080: try {
081: fin = new FileInputStream(f);
082: return getStringFromInputStream(fin);
083: } catch (Exception e) {
084: Logger
085: .error(
086: FileHelper.class.getName(),
087: Messages
088: .getErrorString(
089: "FileUtil.ERROR_0001_ERROR", e.getMessage()), e); //$NON-NLS-1$
090: } finally {
091: closeInputStream(fin);
092: }
093: return null;
094: }
095:
096: public static byte[] getBytesFromFile(File file) throws IOException {
097: InputStream is = new FileInputStream(file);
098: // Create the byte array to hold the data
099: ByteArrayOutputStream baos = new ByteArrayOutputStream();
100: byte[] bytes = new byte[16384];
101: // Read in the bytes
102: int numRead = 0;
103: while ((numRead = is.read(bytes)) != -1) {
104: baos.write(bytes, 0, numRead);
105: }
106: // Close the input stream and return bytes
107: is.close();
108: return baos.toByteArray();
109: }
110:
111: /**
112: * Convenience method to close an input stream and handle (log and throw away) any exceptions.
113: * Helps keep code uncluttered.
114: *
115: * @param strm InputStream to be closed
116: */
117: private static void closeInputStream(InputStream strm) {
118: if (null != strm) {
119: try {
120: strm.close();
121: } catch (IOException e) {
122: logger
123: .warn(
124: Messages
125: .getString("FileHelper.WARN_ERROR_CLOSING_STREAM"), e); //$NON-NLS-1$
126: }
127: }
128: }
129:
130: /**
131: * Convenience method to close a Reader and handle (log and throw away) any exceptions.
132: * Helps keep code uncluttered.
133: *
134: * @param rdr InputSReadertream to be closed
135: */
136: private static void closeReader(Reader rdr) {
137: if (null != rdr) {
138: try {
139: rdr.close();
140: } catch (IOException e) {
141: logger
142: .warn(
143: Messages
144: .getString("FileHelper.WARN_ERROR_CLOSING_READER"), e); //$NON-NLS-1$
145: }
146: }
147: }
148: }
|