1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.io;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedOutputStream;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.OutputStream;
29  
30  /**
31   * <a href="FileCacheOutputStream.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Raymond Augé
34   */
35  public class FileCacheOutputStream extends OutputStream {
36  
37      public FileCacheOutputStream() throws IOException {
38          _tempFile = File.createTempFile(
39              PortalUUIDUtil.generate() + StringPool.DASH, _EXTENSION);
40  
41          _ubos = new UnsyncBufferedOutputStream(
42              new FileOutputStream(_tempFile), _BUFFER);
43      }
44  
45      public void cleanUp() {
46          try {
47              flush();
48              close();
49  
50              if (_fis != null) {
51                  _fis.close();
52              }
53  
54              FileUtil.delete(_tempFile);
55          }
56          catch (IOException ioe) {
57              if (_log.isWarnEnabled()) {
58                  _log.warn(ioe.getMessage());
59              }
60          }
61      }
62  
63      public void close() throws IOException {
64          _ubos.close();
65      }
66  
67      public void flush() throws IOException {
68          _ubos.flush();
69      }
70  
71      public byte[] getBytes() throws IOException {
72          flush();
73          close();
74  
75          return FileUtil.getBytes(_tempFile);
76      }
77  
78      public File getFile() throws IOException {
79          flush();
80          close();
81  
82          return _tempFile;
83      }
84  
85      public FileInputStream getFileInputStream() throws IOException {
86          if (_fis == null) {
87              flush();
88              close();
89  
90              _fis = new FileInputStream(_tempFile);
91          }
92  
93          return _fis;
94      }
95  
96      public long getSize() {
97          return _tempFile.length();
98      }
99  
100     public void write(byte[] b) throws IOException {
101         _ubos.write(b);
102     }
103 
104     public void write(byte[] b, int off, int len) throws IOException {
105         _ubos.write(b, off, len);
106     }
107 
108     public void write(int b) throws IOException {
109         _ubos.write(b);
110     }
111 
112     private static final int _BUFFER = 2048;
113 
114     private static final String _EXTENSION = ".fcos";
115 
116     protected FileInputStream _fis;
117     protected File _tempFile;
118     protected UnsyncBufferedOutputStream _ubos;
119 
120     private static Log _log = LogFactoryUtil.getLog(
121         FileCacheOutputStream.class);
122 
123 }