KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRSaver


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.io.BufferedOutputStream JavaDoc;
31 import java.io.BufferedWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.FileWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 import net.sf.jasperreports.engine.JRException;
40
41
42 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: JRSaver.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
45  */

46 public class JRSaver
47 {
48
49
50     /**
51      *
52      */

53     public static void saveObject(
54         Object JavaDoc obj,
55         String JavaDoc fileName
56         ) throws JRException
57     {
58         saveObject( obj, new File JavaDoc(fileName) );
59     }
60
61
62     /**
63      *
64      */

65     public static void saveObject(
66         Object JavaDoc obj,
67         File JavaDoc file
68         ) throws JRException
69     {
70         FileOutputStream JavaDoc fos = null;
71         ObjectOutputStream JavaDoc oos = null;
72
73         try
74         {
75             fos = new FileOutputStream JavaDoc(file);
76             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
77             oos = new ObjectOutputStream JavaDoc(bos);
78             oos.writeObject(obj);
79             oos.flush();
80             bos.flush();
81             fos.flush();
82         }
83         catch (IOException JavaDoc e)
84         {
85             throw new JRException("Error saving file : " + file, e);
86         }
87         finally
88         {
89             if (oos != null)
90             {
91                 try
92                 {
93                     oos.close();
94                 }
95                 catch(IOException JavaDoc e)
96                 {
97                 }
98             }
99
100             if (fos != null)
101             {
102                 try
103                 {
104                     fos.close();
105                 }
106                 catch(IOException JavaDoc e)
107                 {
108                 }
109             }
110         }
111     }
112
113
114     /**
115      *
116      */

117     public static void saveObject(
118         Object JavaDoc obj,
119         OutputStream JavaDoc os
120         ) throws JRException
121     {
122         ObjectOutputStream JavaDoc oos = null;
123
124         try
125         {
126             oos = new ObjectOutputStream JavaDoc(os);
127             oos.writeObject(obj);
128             oos.flush();
129         }
130         catch (IOException JavaDoc e)
131         {
132             throw new JRException("Error saving object to OutputStream", e);
133         }
134         finally
135         {
136             //FIXMENOW should not close the stream
137
if (oos != null)
138             {
139                 try
140                 {
141                     oos.close();
142                 }
143                 catch(IOException JavaDoc e)
144                 {
145                 }
146             }
147         }
148     }
149         
150
151     /**
152      *
153      */

154     public static void saveClassSource(
155         String JavaDoc source,
156         File JavaDoc file
157         ) throws JRException
158     {
159         FileWriter JavaDoc fwriter = null;
160
161         try
162         {
163             fwriter = new FileWriter JavaDoc(file);
164             BufferedWriter JavaDoc bufferedWriter = new BufferedWriter JavaDoc(fwriter);
165             bufferedWriter.write(source);
166             bufferedWriter.flush();
167             fwriter.flush();
168         }
169         catch (IOException JavaDoc e)
170         {
171             throw new JRException("Error saving expressions class file : " + file, e);
172         }
173         finally
174         {
175             if (fwriter != null)
176             {
177                 try
178                 {
179                     fwriter.close();
180                 }
181                 catch(IOException JavaDoc e)
182                 {
183                 }
184             }
185         }
186     }
187
188
189 }
190
Popular Tags