001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.io;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedOutputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
023    
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.IOException;
028    import java.io.OutputStream;
029    
030    /**
031     * @author Raymond Augé
032     */
033    public class FileCacheOutputStream extends OutputStream {
034    
035            public FileCacheOutputStream() throws IOException {
036                    _tempFile = File.createTempFile(
037                            PortalUUIDUtil.generate() + StringPool.DASH, _EXTENSION);
038    
039                    _ubos = new UnsyncBufferedOutputStream(
040                            new FileOutputStream(_tempFile), _BUFFER);
041            }
042    
043            public void cleanUp() {
044                    try {
045                            flush();
046                            close();
047    
048                            if (_fis != null) {
049                                    _fis.close();
050                            }
051    
052                            FileUtil.delete(_tempFile);
053                    }
054                    catch (IOException ioe) {
055                            if (_log.isWarnEnabled()) {
056                                    _log.warn(ioe.getMessage());
057                            }
058                    }
059            }
060    
061            public void close() throws IOException {
062                    _ubos.close();
063            }
064    
065            public void flush() throws IOException {
066                    _ubos.flush();
067            }
068    
069            public byte[] getBytes() throws IOException {
070                    flush();
071                    close();
072    
073                    return FileUtil.getBytes(_tempFile);
074            }
075    
076            public File getFile() throws IOException {
077                    flush();
078                    close();
079    
080                    return _tempFile;
081            }
082    
083            public FileInputStream getFileInputStream() throws IOException {
084                    if (_fis == null) {
085                            flush();
086                            close();
087    
088                            _fis = new FileInputStream(_tempFile);
089                    }
090    
091                    return _fis;
092            }
093    
094            public long getSize() {
095                    return _tempFile.length();
096            }
097    
098            public void write(byte[] b) throws IOException {
099                    _ubos.write(b);
100            }
101    
102            public void write(byte[] b, int off, int len) throws IOException {
103                    _ubos.write(b, off, len);
104            }
105    
106            public void write(int b) throws IOException {
107                    _ubos.write(b);
108            }
109    
110            private static final int _BUFFER = 2048;
111    
112            private static final String _EXTENSION = ".fcos";
113    
114            protected FileInputStream _fis;
115            protected File _tempFile;
116            protected UnsyncBufferedOutputStream _ubos;
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    FileCacheOutputStream.class);
120    
121    }