KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > common > FileBufferedOutputStream


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.common;
22
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.jaspersoft.jasperserver.api.JSException;
36
37 /**
38  * @author Lucian Chirita (lucianc@users.sourceforge.net)
39  * @version $Id: FileBufferedOutputStream.java 4106 2006-08-04 15:21:10Z lucian $
40  */

41 public class FileBufferedOutputStream extends OutputStream JavaDoc {
42     
43     private static final Log log = LogFactory.getLog(FileBufferedOutputStream.class);
44     
45     public static final int DEFAULT_MEMORY_THRESHOLD = 1 << 18;
46     public static final int INFINIT_MEMORY_THRESHOLD = -1;
47     public static final int DEFAULT_INITIAL_MEMORY_BUFFER_SIZE = 1 << 16;
48     public static final int DEFAULT_INPUT_BUFFER_LENGTH = 1 << 14;
49     
50     private final int memoryThreshold;
51     private final int initialMemoryBufferSize;
52     private final int inputBufferLenght;
53     
54     private final ByteArrayOutputStream JavaDoc memoryOutput;
55     private int size;
56     private File JavaDoc file;
57     private BufferedOutputStream JavaDoc fileOutput;
58     private boolean closed;
59     private boolean disposed;
60     
61     public FileBufferedOutputStream() {
62         this(DEFAULT_MEMORY_THRESHOLD, DEFAULT_INITIAL_MEMORY_BUFFER_SIZE, DEFAULT_INPUT_BUFFER_LENGTH);
63     }
64     
65     public FileBufferedOutputStream(int memoryThreshold) {
66         this(memoryThreshold, DEFAULT_INITIAL_MEMORY_BUFFER_SIZE, DEFAULT_INPUT_BUFFER_LENGTH);
67     }
68     
69     public FileBufferedOutputStream(int memoryThreshold, int initialMemoryBufferSize) {
70         this(memoryThreshold, initialMemoryBufferSize, DEFAULT_INPUT_BUFFER_LENGTH);
71     }
72     
73     public FileBufferedOutputStream(int memoryThreshold, int initialMemoryBufferSize, int inputBufferLenght) {
74         this.memoryThreshold = memoryThreshold;
75         this.initialMemoryBufferSize = initialMemoryBufferSize;
76         this.inputBufferLenght = inputBufferLenght;
77         
78         size = 0;
79         memoryOutput = this.memoryThreshold != 0 ? new ByteArrayOutputStream JavaDoc(this.initialMemoryBufferSize) : null;
80     }
81
82     public void write(int b) throws IOException JavaDoc {
83         checkClosed();
84         
85         if (availableMemorySpace() > 0) {
86             memoryOutput.write(b);
87         } else {
88             ensureFileOutput().write(b);
89         }
90         
91         ++size;
92     }
93
94     protected int availableMemorySpace() {
95         int availableMemorySpace;
96         if (memoryOutput != null
97                 && (memoryThreshold < 0 || memoryOutput.size() < memoryThreshold)) {
98             availableMemorySpace = memoryThreshold - memoryOutput.size();
99         } else {
100             availableMemorySpace = 0;
101         }
102         return availableMemorySpace;
103     }
104     
105     protected BufferedOutputStream JavaDoc ensureFileOutput() throws IOException JavaDoc, FileNotFoundException JavaDoc {
106         if (fileOutput == null) {
107             file = File.createTempFile("exported_report", ".tmp");
108             FileOutputStream JavaDoc fileOutputStream = new FileOutputStream JavaDoc(file);
109             fileOutput = new BufferedOutputStream JavaDoc(fileOutputStream);
110         }
111         return fileOutput;
112     }
113
114     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
115         checkClosed();
116         
117         int memoryLen = availableMemorySpace();
118         if (len < memoryLen) {
119             memoryLen = len;
120         }
121         
122         if (memoryLen > 0) {
123             memoryOutput.write(b, off, memoryLen);
124         }
125         
126         if (memoryLen < len) {
127             ensureFileOutput().write(b, off + memoryLen, len - memoryLen);
128         }
129         
130         size += len;
131     }
132
133     public void checkClosed() {
134         if (closed) {
135             throw new JSException("Output stream already closed");
136         }
137     }
138
139     public void close() throws IOException JavaDoc {
140         if (!closed && fileOutput != null) {
141             fileOutput.flush();
142             fileOutput.close();
143         }
144         
145         closed = true;
146     }
147
148     public void flush() throws IOException JavaDoc {
149         if (fileOutput != null) {
150             fileOutput.flush();
151         }
152     }
153
154     public int size() {
155         return size;
156     }
157     
158     public void writeData(OutputStream JavaDoc out) throws IOException JavaDoc {
159         if (!closed) {
160             close();
161         }
162
163         if (memoryOutput != null) {
164             memoryOutput.writeTo(out);
165         }
166         
167         if (file != null) {
168             FileInputStream JavaDoc fileInput = new FileInputStream JavaDoc(file);
169             boolean inputClosed = false;
170             try {
171                 byte[] buffer = new byte[inputBufferLenght];
172                 int read;
173                 while((read = fileInput.read(buffer)) > 0) {
174                     out.write(buffer, 0, read);
175                 }
176                 fileInput.close();
177                 inputClosed = true;
178             } finally {
179                 if (!inputClosed) {
180                     try {
181                         fileInput.close();
182                     } catch (IOException JavaDoc e) {
183                         log.warn("Could not close file input stream", e);
184                     }
185                 }
186             }
187         }
188     }
189     
190     public void dispose() {
191         if (disposed) {
192             return;
193         }
194         
195         boolean success = true;
196         if (!closed && fileOutput != null) {
197             try {
198                 fileOutput.close();
199             } catch (IOException JavaDoc e) {
200                 log.warn("Error while closing the temporary file output stream", e);
201                 success = false;
202             }
203         }
204         
205         if (file != null && !file.delete()) {
206             log.warn("Error while deleting the temporary file");
207             success = false;
208         }
209         
210         disposed = success;
211     }
212
213     protected void finalize() throws Throwable JavaDoc {
214         dispose();
215         super.finalize();
216     }
217     
218     
219 }
220
Popular Tags