001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
062            public void close() throws IOException {
063                    _ubos.close();
064            }
065    
066            @Override
067            public void flush() throws IOException {
068                    _ubos.flush();
069            }
070    
071            public byte[] getBytes() throws IOException {
072                    flush();
073                    close();
074    
075                    return FileUtil.getBytes(_tempFile);
076            }
077    
078            public File getFile() throws IOException {
079                    flush();
080                    close();
081    
082                    return _tempFile;
083            }
084    
085            public FileInputStream getFileInputStream() throws IOException {
086                    if (_fis == null) {
087                            flush();
088                            close();
089    
090                            _fis = new FileInputStream(_tempFile);
091                    }
092    
093                    return _fis;
094            }
095    
096            public long getSize() {
097                    return _tempFile.length();
098            }
099    
100            @Override
101            public void write(byte[] b) throws IOException {
102                    _ubos.write(b);
103            }
104    
105            @Override
106            public void write(byte[] b, int off, int len) throws IOException {
107                    _ubos.write(b, off, len);
108            }
109    
110            @Override
111            public void write(int b) throws IOException {
112                    _ubos.write(b);
113            }
114    
115            private static final int _BUFFER = 2048;
116    
117            private static final String _EXTENSION = ".fcos";
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    FileCacheOutputStream.class);
121    
122            private FileInputStream _fis;
123            private File _tempFile;
124            private UnsyncBufferedOutputStream _ubos;
125    
126    }