| FileCacheOutputStream.java |
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.util.FileUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
28
29 import java.io.BufferedOutputStream;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35
36 /**
37 * <a href="FileCacheOutputStream.java.html"><b><i>View Source</i></b></a>
38 *
39 * @author Raymond Augé
40 *
41 */
42 public class FileCacheOutputStream extends OutputStream {
43
44 public FileCacheOutputStream() throws IOException {
45 _tempFile = File.createTempFile(
46 PortalUUIDUtil.generate() + StringPool.DASH, _EXTENSION);
47
48 _bos = new BufferedOutputStream(
49 new FileOutputStream(_tempFile), _BUFFER);
50 }
51
52 public void close() throws IOException {
53 _bos.close();
54 }
55
56 public void flush() throws IOException {
57 _bos.flush();
58 }
59
60 public byte[] getBytes() throws IOException {
61 flush();
62 close();
63
64 return FileUtil.getBytes(_tempFile);
65 }
66
67 public File getFile() throws IOException {
68 flush();
69 close();
70
71 return _tempFile;
72 }
73
74 public FileInputStream getFileInputStream() throws IOException {
75 flush();
76 close();
77
78 return new FileInputStream(_tempFile);
79 }
80
81 public long getSize() {
82 return _tempFile.length();
83 }
84
85 public void write(byte[] b) throws IOException {
86 _bos.write(b);
87 }
88
89 public void write(byte[] b, int off, int len) throws IOException {
90 _bos.write(b, off, len);
91 }
92
93 public void write(int b) throws IOException {
94 _bos.write(b);
95 }
96
97 private static final int _BUFFER = 2048;
98
99 private static final String _EXTENSION = ".fcos";
100
101 protected BufferedOutputStream _bos;
102 protected File _tempFile;
103
104 }