1 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 43 public class JRImageSaver 44 { 45 46 47 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 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 |