1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.io;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
30  
31  import java.io.BufferedOutputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.FileOutputStream;
35  import java.io.IOException;
36  import java.io.OutputStream;
37  
38  /**
39   * <a href="FileCacheOutputStream.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Raymond Augé
42   */
43  public class FileCacheOutputStream extends OutputStream {
44  
45      public FileCacheOutputStream() throws IOException {
46          _tempFile = File.createTempFile(
47              PortalUUIDUtil.generate() + StringPool.DASH, _EXTENSION);
48  
49          _bos = new BufferedOutputStream(
50              new FileOutputStream(_tempFile), _BUFFER);
51      }
52  
53      public void cleanUp() {
54          try {
55              flush();
56              close();
57  
58              if (_fis != null) {
59                  _fis.close();
60              }
61  
62              FileUtil.delete(_tempFile);
63          }
64          catch (IOException ioe) {
65              if (_log.isWarnEnabled()) {
66                  _log.warn(ioe.getMessage());
67              }
68          }
69      }
70  
71      public void close() throws IOException {
72          _bos.close();
73      }
74  
75      public void flush() throws IOException {
76          _bos.flush();
77      }
78  
79      public byte[] getBytes() throws IOException {
80          flush();
81          close();
82  
83          return FileUtil.getBytes(_tempFile);
84      }
85  
86      public File getFile() throws IOException {
87          flush();
88          close();
89  
90          return _tempFile;
91      }
92  
93      public FileInputStream getFileInputStream() throws IOException {
94          if (_fis == null) {
95              flush();
96              close();
97  
98              _fis = new FileInputStream(_tempFile);
99          }
100 
101         return _fis;
102     }
103 
104     public long getSize() {
105         return _tempFile.length();
106     }
107 
108     public void write(byte[] b) throws IOException {
109         _bos.write(b);
110     }
111 
112     public void write(byte[] b, int off, int len) throws IOException {
113         _bos.write(b, off, len);
114     }
115 
116     public void write(int b) throws IOException {
117         _bos.write(b);
118     }
119 
120     private static final int _BUFFER = 2048;
121 
122     private static final String _EXTENSION = ".fcos";
123 
124     protected BufferedOutputStream _bos;
125     protected FileInputStream _fis;
126     protected File _tempFile;
127 
128     private static Log _log =
129         LogFactoryUtil.getLog(FileCacheOutputStream.class);
130 
131 }