1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.BufferedOutputStream ; 31 import java.io.BufferedWriter ; 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.FileWriter ; 35 import java.io.IOException ; 36 import java.io.ObjectOutputStream ; 37 import java.io.OutputStream ; 38 39 import net.sf.jasperreports.engine.JRException; 40 41 42 46 public class JRSaver 47 { 48 49 50 53 public static void saveObject( 54 Object obj, 55 String fileName 56 ) throws JRException 57 { 58 saveObject( obj, new File (fileName) ); 59 } 60 61 62 65 public static void saveObject( 66 Object obj, 67 File file 68 ) throws JRException 69 { 70 FileOutputStream fos = null; 71 ObjectOutputStream oos = null; 72 73 try 74 { 75 fos = new FileOutputStream (file); 76 BufferedOutputStream bos = new BufferedOutputStream (fos); 77 oos = new ObjectOutputStream (bos); 78 oos.writeObject(obj); 79 oos.flush(); 80 bos.flush(); 81 fos.flush(); 82 } 83 catch (IOException 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 e) 96 { 97 } 98 } 99 100 if (fos != null) 101 { 102 try 103 { 104 fos.close(); 105 } 106 catch(IOException e) 107 { 108 } 109 } 110 } 111 } 112 113 114 117 public static void saveObject( 118 Object obj, 119 OutputStream os 120 ) throws JRException 121 { 122 ObjectOutputStream oos = null; 123 124 try 125 { 126 oos = new ObjectOutputStream (os); 127 oos.writeObject(obj); 128 oos.flush(); 129 } 130 catch (IOException e) 131 { 132 throw new JRException("Error saving object to OutputStream", e); 133 } 134 finally 135 { 136 if (oos != null) 138 { 139 try 140 { 141 oos.close(); 142 } 143 catch(IOException e) 144 { 145 } 146 } 147 } 148 } 149 150 151 154 public static void saveClassSource( 155 String source, 156 File file 157 ) throws JRException 158 { 159 FileWriter fwriter = null; 160 161 try 162 { 163 fwriter = new FileWriter (file); 164 BufferedWriter bufferedWriter = new BufferedWriter (fwriter); 165 bufferedWriter.write(source); 166 bufferedWriter.flush(); 167 fwriter.flush(); 168 } 169 catch (IOException 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 e) 182 { 183 } 184 } 185 } 186 } 187 188 189 } 190 | Popular Tags |