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