KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 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  * 185, Berry Street, Suite 6200
25  * San Francisco CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35
36 import net.sf.jasperreports.engine.JRException;
37
38
39 /**
40  * @author Teodor Danciu (teodord@users.sourceforge.net)
41  * @version $Id: JRImageSaver.java,v 1.8 2005/06/27 07:16:52 teodord Exp $
42  */

43 public class JRImageSaver
44 {
45
46
47     /**
48      *
49      */

50     public static void saveImageDataToFile(byte[] imageData, File file) throws JRException
51     {
52         FileOutputStream fos = null;
53         ByteArrayInputStream bais = null;
54
55         try
56         {
57             fos = new FileOutputStream(file);
58             bais = new ByteArrayInputStream(imageData);
59             
60             byte[] bytes = new byte[10000];
61             int ln = 0;
62             while ((ln = bais.read(bytes)) > 0)
63             {
64                 fos.write(bytes, 0, ln);
65             }
66             
67             fos.flush();
68         }
69         catch (IOException e)
70         {
71             throw new JRException("Error saving image data : " + file, e);
72         }
73         finally
74         {
75             if (fos != null)
76             {
77                 try
78                 {
79                     fos.close();
80                 }
81                 catch(IOException e)
82                 {
83                 }
84             }
85
86             if (bais != null)
87             {
88                 try
89                 {
90                     bais.close();
91                 }
92                 catch(IOException e)
93                 {
94                 }
95             }
96         }
97     }
98
99
100     /**
101      *
102      */

103     public static void saveImageDataToOutputStream(byte[] imageData, OutputStream os) throws JRException
104     {
105         ByteArrayInputStream bais = null;
106
107         try
108         {
109             bais = new ByteArrayInputStream(imageData);
110             
111             byte[] bytes = new byte[10000];
112             int ln = 0;
113             while ((ln = bais.read(bytes)) > 0)
114             {
115                 os.write(bytes, 0, ln);
116             }
117
118             os.flush();
119         }
120         catch (IOException e)
121         {
122             throw new JRException("Error saving image data to output stream.", e);
123         }
124         finally
125         {
126             if (bais != null)
127             {
128                 try
129                 {
130                     bais.close();
131                 }
132                 catch(IOException e)
133                 {
134                 }
135             }
136         }
137     }
138
139
140 }
141
Popular Tags