1 package jp.sf.grizzly.storage.impl;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10
11 import jp.sf.grizzly.storage.StreamStorage;
12 import jp.sf.grizzly.storage.StreamStorageException;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 public class FileStreamStorageImpl implements StreamStorage
18 {
19 /***
20 * Logger for this class
21 */
22 private static final Log log = LogFactory.getLog(FileStreamStorageImpl.class);
23
24 private String DEFAULT_TMPFILE_PREFIX = "STREAMSTORAGE_";
25
26 private String DEFAULT_TMPFILE_SUFFIX = ".tmp";
27
28 private InputStream inputStream;
29
30 private OutputStream outputStream;
31
32 private String encoding;
33
34 private File previousFile;
35
36 private File currentFile;
37
38 public FileStreamStorageImpl(InputStream in, String encoding)
39 throws StreamStorageException
40 {
41 init(in, encoding);
42 }
43
44 public void init(InputStream in, String encoding)
45 throws StreamStorageException
46 {
47 try
48 {
49
50 inputStream = in;
51 this.encoding = encoding;
52 this.previousFile = null;
53 this.currentFile = File.createTempFile(DEFAULT_TMPFILE_PREFIX,
54 DEFAULT_TMPFILE_SUFFIX);
55 outputStream = new FileOutputStream(currentFile);
56 }
57 catch (FileNotFoundException e)
58 {
59 log.error("Cannot find the temp file: " + currentFile.getName(), e);
60 throw new StreamStorageException((Throwable) e);
61 }
62 catch (IOException e)
63 {
64 log.error("I/O error occurs. ", e);
65 throw new StreamStorageException((Throwable) e);
66 }
67 }
68
69 public void destroy()
70 {
71 if (previousFile != null && previousFile.isFile())
72 {
73 previousFile.deleteOnExit();
74 }
75 if (currentFile != null && currentFile.isFile())
76 {
77 currentFile.deleteOnExit();
78 }
79 }
80
81
82
83
84 public void commit() throws StreamStorageException
85 {
86 try
87 {
88 if (inputStream != null)
89 {
90 inputStream.close();
91 }
92 if (outputStream != null)
93 {
94 outputStream.flush();
95 outputStream.close();
96 }
97
98 if (previousFile != null && previousFile.isFile())
99 {
100 previousFile.deleteOnExit();
101 }
102
103 previousFile = currentFile;
104 currentFile = File.createTempFile(DEFAULT_TMPFILE_PREFIX,
105 DEFAULT_TMPFILE_SUFFIX);
106 inputStream = new FileInputStream(previousFile);
107 outputStream = new FileOutputStream(currentFile);
108 }
109 catch (FileNotFoundException e)
110 {
111 log.error("Cannot find the temp file. ", e);
112 throw new StreamStorageException((Throwable) e);
113 }
114 catch (IOException e)
115 {
116 log.error("I/O error occurs. ", e);
117 throw new StreamStorageException((Throwable) e);
118 }
119 }
120
121
122
123
124 public InputStream getResultInputStream() throws StreamStorageException
125 {
126 try
127 {
128 if (log.isDebugEnabled())
129 {
130 log.debug("getResultInputStream() - previousFile="
131 + previousFile);
132 }
133
134 if (previousFile == null)
135 {
136 if (inputStream.markSupported())
137 {
138 inputStream.reset();
139 }
140 return inputStream;
141 }
142
143 return new FileInputStream(previousFile);
144 }
145 catch (FileNotFoundException e)
146 {
147 log
148 .error("Cannot find the temp file: "
149 + previousFile.getName(), e);
150 throw new StreamStorageException((Throwable) e);
151 }
152 catch (IOException e)
153 {
154 log.error("I/O error occurs. ", e);
155 throw new StreamStorageException((Throwable) e);
156 }
157 }
158
159
160
161
162 public String getEncoding()
163 {
164 return encoding;
165 }
166
167 /***
168 * @param encoding The encoding to set.
169 */
170 public void setEncoding(String encoding)
171 {
172 this.encoding = encoding;
173 }
174
175
176
177
178 public InputStream getInputStream()
179 {
180 return inputStream;
181 }
182
183 /***
184 * @param inputStream The inputStream to set.
185 */
186 public void setInputStream(InputStream inputStream)
187 {
188 this.inputStream = inputStream;
189 }
190
191
192
193
194 public OutputStream getOutputStream()
195 {
196 return outputStream;
197 }
198
199 /***
200 * @param outputStream The outputStream to set.
201 */
202 public void setOutputStream(OutputStream outputStream)
203 {
204 this.outputStream = outputStream;
205 }
206 }