001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.util;
029:
030: import java.io.BufferedOutputStream;
031: import java.io.BufferedWriter;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.FileWriter;
035: import java.io.IOException;
036: import java.io.ObjectOutputStream;
037: import java.io.OutputStream;
038:
039: import net.sf.jasperreports.engine.JRException;
040:
041: /**
042: * @author Teodor Danciu (teodord@users.sourceforge.net)
043: * @version $Id: JRSaver.java 1507 2006-11-27 15:12:17Z teodord $
044: */
045: public class JRSaver {
046:
047: /**
048: *
049: */
050: public static void saveObject(Object obj, String fileName)
051: throws JRException {
052: saveObject(obj, new File(fileName));
053: }
054:
055: /**
056: *
057: */
058: public static void saveObject(Object obj, File file)
059: throws JRException {
060: FileOutputStream fos = null;
061: ObjectOutputStream oos = null;
062:
063: try {
064: fos = new FileOutputStream(file);
065: BufferedOutputStream bos = new BufferedOutputStream(fos);
066: oos = new ObjectOutputStream(bos);
067: oos.writeObject(obj);
068: oos.flush();
069: bos.flush();
070: fos.flush();
071: } catch (IOException e) {
072: throw new JRException("Error saving file : " + file, e);
073: } finally {
074: if (oos != null) {
075: try {
076: oos.close();
077: } catch (IOException e) {
078: }
079: }
080:
081: if (fos != null) {
082: try {
083: fos.close();
084: } catch (IOException e) {
085: }
086: }
087: }
088: }
089:
090: /**
091: *
092: */
093: public static void saveObject(Object obj, OutputStream os)
094: throws JRException {
095: ObjectOutputStream oos = null;
096:
097: try {
098: oos = new ObjectOutputStream(os);
099: oos.writeObject(obj);
100: oos.flush();
101: } catch (IOException e) {
102: throw new JRException(
103: "Error saving object to OutputStream", e);
104: } finally {
105: //FIXMENOW should not close the stream
106: if (oos != null) {
107: try {
108: oos.close();
109: } catch (IOException e) {
110: }
111: }
112: }
113: }
114:
115: /**
116: *
117: */
118: public static void saveClassSource(String source, File file)
119: throws JRException {
120: FileWriter fwriter = null;
121:
122: try {
123: fwriter = new FileWriter(file);
124: BufferedWriter bufferedWriter = new BufferedWriter(fwriter);
125: bufferedWriter.write(source);
126: bufferedWriter.flush();
127: fwriter.flush();
128: } catch (IOException e) {
129: throw new JRException(
130: "Error saving expressions class file : " + file, e);
131: } finally {
132: if (fwriter != null) {
133: try {
134: fwriter.close();
135: } catch (IOException e) {
136: }
137: }
138: }
139: }
140:
141: }
|