001    /**
002     * Copyright (c) 2000-2011 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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.FileInputStream;
021    import java.io.FileOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    import java.nio.channels.Channel;
027    import java.nio.channels.FileChannel;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Raymond Augé
032     */
033    public class StreamUtil {
034    
035            public static final int BUFFER_SIZE = GetterUtil.getInteger(
036                    System.getProperty(StreamUtil.class.getName() + ".buffer.size"),
037                    8192);
038    
039            public static void cleanUp(Channel channel) {
040                    try {
041                            if (channel != null) {
042                                    channel.close();
043                            }
044                    }
045                    catch (Exception e) {
046                            if (_log.isWarnEnabled()) {
047                                    _log.warn(e, e);
048                            }
049                    }
050            }
051    
052            public static void cleanUp(Channel inputChannel, Channel outputChannel) {
053                    cleanUp(inputChannel);
054                    cleanUp(outputChannel);
055            }
056    
057            public static void cleanUp(InputStream inputStream) {
058                    try {
059                            if (inputStream != null) {
060                                    inputStream.close();
061                            }
062                    }
063                    catch (Exception e) {
064                            if (_log.isWarnEnabled()) {
065                                    _log.warn(e, e);
066                            }
067                    }
068            }
069    
070            public static void cleanUp(
071                    InputStream inputStream, OutputStream outputStream) {
072    
073                    cleanUp(outputStream);
074                    cleanUp(inputStream);
075            }
076    
077            public static void cleanUp(OutputStream outputStream) {
078                    try {
079                            if (outputStream != null) {
080                                    outputStream.flush();
081                            }
082                    }
083                    catch (Exception e) {
084                            if (_log.isWarnEnabled()) {
085                                    _log.warn(e, e);
086                            }
087                    }
088    
089                    try {
090                            if (outputStream != null) {
091                                    outputStream.close();
092                            }
093                    }
094                    catch (Exception e) {
095                            if (_log.isWarnEnabled()) {
096                                    _log.warn(e, e);
097                            }
098                    }
099            }
100    
101            public static void transfer(
102                            InputStream inputStream, OutputStream outputStream)
103                    throws IOException {
104    
105                    transfer(inputStream, outputStream, BUFFER_SIZE, true);
106            }
107    
108            public static void transfer(
109                            InputStream inputStream, OutputStream outputStream, boolean cleanUp)
110                    throws IOException {
111    
112                    transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
113            }
114    
115            public static void transfer(
116                            InputStream inputStream, OutputStream outputStream, int bufferSize)
117                    throws IOException {
118    
119                    transfer(inputStream, outputStream, bufferSize, true);
120            }
121    
122            public static void transfer(
123                            InputStream inputStream, OutputStream outputStream, int bufferSize,
124                            boolean cleanUp)
125                    throws IOException {
126    
127                    if (inputStream == null) {
128                            throw new IllegalArgumentException("Input stream cannot be null");
129                    }
130    
131                    if (outputStream == null) {
132                            throw new IllegalArgumentException("Output stream cannot be null");
133                    }
134    
135                    if (bufferSize <= 0) {
136                            bufferSize = BUFFER_SIZE;
137                    }
138    
139                    if ((inputStream instanceof FileInputStream) &&
140                            (outputStream instanceof FileOutputStream)) {
141    
142                            FileInputStream fileInputStream = (FileInputStream)inputStream;
143    
144                            FileChannel sourceChannel = fileInputStream.getChannel();
145    
146                            FileOutputStream fileOutputStream = (FileOutputStream)outputStream;
147    
148                            FileChannel targetChannel = fileOutputStream.getChannel();
149    
150                            long position = 0;
151    
152                            while (position < sourceChannel.size()) {
153                                    position += sourceChannel.transferTo(
154                                            position, sourceChannel.size() - position, targetChannel);
155                            }
156    
157                            if (cleanUp) {
158                                    cleanUp(fileInputStream, fileOutputStream);
159                            }
160                    }
161                    else {
162                            try {
163                                    byte[] bytes = new byte[bufferSize];
164    
165                                    int value = -1;
166    
167                                    while ((value = inputStream.read(bytes)) != -1) {
168                                            outputStream.write(bytes, 0 , value);
169                                    }
170                            }
171                            finally {
172                                    if (cleanUp) {
173                                            cleanUp(inputStream, outputStream);
174                                    }
175                            }
176                    }
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
180    
181    }