1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.io.unsync.UnsyncBufferedOutputStream;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
31  
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          _ubos = new UnsyncBufferedOutputStream(
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          _ubos.close();
73      }
74  
75      public void flush() throws IOException {
76          _ubos.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         _ubos.write(b);
110     }
111 
112     public void write(byte[] b, int off, int len) throws IOException {
113         _ubos.write(b, off, len);
114     }
115 
116     public void write(int b) throws IOException {
117         _ubos.write(b);
118     }
119 
120     private static final int _BUFFER = 2048;
121 
122     private static final String _EXTENSION = ".fcos";
123 
124     protected FileInputStream _fis;
125     protected File _tempFile;
126     protected UnsyncBufferedOutputStream _ubos;
127 
128     private static Log _log =
129         LogFactoryUtil.getLog(FileCacheOutputStream.class);
130 
131 }