1
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
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 }