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 final boolean FORCE_TIO = GetterUtil.getBoolean(
040                    System.getProperty(StreamUtil.class.getName() + ".force.tio"));
041    
042            public static void cleanUp(Channel channel) {
043                    try {
044                            if (channel != null) {
045                                    channel.close();
046                            }
047                    }
048                    catch (Exception e) {
049                            if (_log.isWarnEnabled()) {
050                                    _log.warn(e, e);
051                            }
052                    }
053            }
054    
055            public static void cleanUp(Channel inputChannel, Channel outputChannel) {
056                    cleanUp(inputChannel);
057                    cleanUp(outputChannel);
058            }
059    
060            public static void cleanUp(InputStream inputStream) {
061                    try {
062                            if (inputStream != null) {
063                                    inputStream.close();
064                            }
065                    }
066                    catch (Exception e) {
067                            if (_log.isWarnEnabled()) {
068                                    _log.warn(e, e);
069                            }
070                    }
071            }
072    
073            public static void cleanUp(
074                    InputStream inputStream, OutputStream outputStream) {
075    
076                    cleanUp(outputStream);
077                    cleanUp(inputStream);
078            }
079    
080            public static void cleanUp(OutputStream outputStream) {
081                    try {
082                            if (outputStream != null) {
083                                    outputStream.flush();
084                            }
085                    }
086                    catch (Exception e) {
087                            if (_log.isWarnEnabled()) {
088                                    _log.warn(e, e);
089                            }
090                    }
091    
092                    try {
093                            if (outputStream != null) {
094                                    outputStream.close();
095                            }
096                    }
097                    catch (Exception e) {
098                            if (_log.isWarnEnabled()) {
099                                    _log.warn(e, e);
100                            }
101                    }
102            }
103    
104            public static void transfer(
105                            InputStream inputStream, OutputStream outputStream)
106                    throws IOException {
107    
108                    transfer(inputStream, outputStream, BUFFER_SIZE, true);
109            }
110    
111            public static void transfer(
112                            InputStream inputStream, OutputStream outputStream, boolean cleanUp)
113                    throws IOException {
114    
115                    transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
116            }
117    
118            public static void transfer(
119                            InputStream inputStream, OutputStream outputStream, int bufferSize)
120                    throws IOException {
121    
122                    transfer(inputStream, outputStream, bufferSize, true);
123            }
124    
125            public static void transfer(
126                            InputStream inputStream, OutputStream outputStream, int bufferSize,
127                            boolean cleanUp)
128                    throws IOException {
129    
130                    if (inputStream == null) {
131                            throw new IllegalArgumentException("Input stream cannot be null");
132                    }
133    
134                    if (outputStream == null) {
135                            throw new IllegalArgumentException("Output stream cannot be null");
136                    }
137    
138                    if (bufferSize <= 0) {
139                            bufferSize = BUFFER_SIZE;
140                    }
141    
142                    try {
143                            if (!FORCE_TIO && (inputStream instanceof FileInputStream) &&
144                                    (outputStream instanceof FileOutputStream)) {
145    
146                                    FileInputStream fileInputStream = (FileInputStream)inputStream;
147    
148                                    FileChannel sourceFileChannel = fileInputStream.getChannel();
149    
150                                    FileOutputStream fileOutputStream =
151                                            (FileOutputStream)outputStream;
152    
153                                    FileChannel targetFileChannel = fileOutputStream.getChannel();
154    
155                                    long position = 0;
156    
157                                    while (position < sourceFileChannel.size()) {
158                                            position += sourceFileChannel.transferTo(
159                                                    position, sourceFileChannel.size() - position,
160                                                    targetFileChannel);
161                                    }
162                            }
163                            else {
164                                    byte[] bytes = new byte[bufferSize];
165    
166                                    int value = -1;
167    
168                                    while ((value = inputStream.read(bytes)) != -1) {
169                                            outputStream.write(bytes, 0 , value);
170                                    }
171                            }
172                    }
173                    finally {
174                            if (cleanUp) {
175                                    cleanUp(inputStream, outputStream);
176                            }
177                    }
178            }
179    
180            private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
181    
182    }