001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncFilterInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncFilterOutputStream;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.Closeable;
023    import java.io.FileInputStream;
024    import java.io.FileOutputStream;
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.io.OutputStream;
028    
029    import java.nio.channels.FileChannel;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Raymond Aug??
034     * @author Shuyang Zhou
035     */
036    public class StreamUtil {
037    
038            public static final int BUFFER_SIZE = GetterUtil.getInteger(
039                    System.getProperty(StreamUtil.class.getName() + ".buffer.size"), 8192);
040    
041            public static final boolean FORCE_TIO = GetterUtil.getBoolean(
042                    System.getProperty(StreamUtil.class.getName() + ".force.tio"));
043    
044            public static void cleanUp(boolean quite, Closeable... closeables) {
045                    IOException ioException = null;
046    
047                    for (Closeable closeable : closeables) {
048                            if (closeable != null) {
049                                    try {
050                                            closeable.close();
051                                    }
052                                    catch (IOException ioe) {
053                                            if (ioException == null) {
054                                                    ioException = ioe;
055                                            }
056                                            else {
057                                                    ioException.addSuppressed(ioe);
058                                            }
059                                    }
060                            }
061                    }
062    
063                    if (ioException == null) {
064                            return;
065                    }
066    
067                    if (quite) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn(ioException, ioException);
070                            }
071                    }
072                    else {
073                            ReflectionUtil.throwException(ioException);
074                    }
075            }
076    
077            public static void cleanUp(Closeable... closeables) {
078                    cleanUp(true, closeables);
079            }
080    
081            public static void transfer(
082                            InputStream inputStream, OutputStream outputStream)
083                    throws IOException {
084    
085                    transfer(inputStream, outputStream, BUFFER_SIZE, true);
086            }
087    
088            public static void transfer(
089                            InputStream inputStream, OutputStream outputStream, boolean cleanUp)
090                    throws IOException {
091    
092                    transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
093            }
094    
095            public static void transfer(
096                            InputStream inputStream, OutputStream outputStream, int bufferSize)
097                    throws IOException {
098    
099                    transfer(inputStream, outputStream, bufferSize, true);
100            }
101    
102            public static void transfer(
103                            InputStream inputStream, OutputStream outputStream, int bufferSize,
104                            boolean cleanUp)
105                    throws IOException {
106    
107                    transfer(inputStream, outputStream, bufferSize, cleanUp, 0);
108            }
109    
110            public static void transfer(
111                            InputStream inputStream, OutputStream outputStream, int bufferSize,
112                            boolean cleanUp, long length)
113                    throws IOException {
114    
115                    if (inputStream == null) {
116                            throw new IllegalArgumentException("Input stream is null");
117                    }
118    
119                    if (outputStream == null) {
120                            throw new IllegalArgumentException("Output stream is null");
121                    }
122    
123                    if (bufferSize <= 0) {
124                            bufferSize = BUFFER_SIZE;
125                    }
126    
127                    try {
128                            if (!FORCE_TIO && (inputStream instanceof FileInputStream) &&
129                                    (outputStream instanceof FileOutputStream)) {
130    
131                                    FileInputStream fileInputStream = (FileInputStream)inputStream;
132                                    FileOutputStream fileOutputStream =
133                                            (FileOutputStream)outputStream;
134    
135                                    transferFileChannel(
136                                            fileInputStream.getChannel(), fileOutputStream.getChannel(),
137                                            length);
138                            }
139                            else {
140                                    transferByteArray(
141                                            inputStream, outputStream, bufferSize, length);
142                            }
143                    }
144                    finally {
145                            if (cleanUp) {
146                                    cleanUp(false, inputStream, outputStream);
147                            }
148                    }
149            }
150    
151            public static void transfer(
152                            InputStream inputStream, OutputStream outputStream, long length)
153                    throws IOException {
154    
155                    transfer(inputStream, outputStream, BUFFER_SIZE, true, length);
156            }
157    
158            public static InputStream uncloseable(InputStream inputStream) {
159                    if (inputStream == null) {
160                            return null;
161                    }
162    
163                    return new UnsyncFilterInputStream(inputStream) {
164    
165                            @Override
166                            public void close() {
167                            }
168    
169                    };
170            }
171    
172            public static OutputStream uncloseable(OutputStream outputStream) {
173                    if (outputStream == null) {
174                            return null;
175                    }
176    
177                    return new UnsyncFilterOutputStream(outputStream) {
178    
179                            @Override
180                            public void close() {
181                            }
182    
183                    };
184            }
185    
186            protected static void transferByteArray(
187                            InputStream inputStream, OutputStream outputStream, int bufferSize,
188                            long length)
189                    throws IOException {
190    
191                    byte[] bytes = new byte[bufferSize];
192    
193                    if (length > 0) {
194                            long remainingLength = length;
195    
196                            while (remainingLength > 0) {
197                                    int readBytes = inputStream.read(
198                                            bytes, 0, (int)Math.min(remainingLength, bufferSize));
199    
200                                    if (readBytes == -1) {
201                                            break;
202                                    }
203    
204                                    outputStream.write(bytes, 0, readBytes);
205    
206                                    remainingLength -= readBytes;
207                            }
208                    }
209                    else {
210                            int value = -1;
211    
212                            while ((value = inputStream.read(bytes)) != -1) {
213                                    outputStream.write(bytes, 0, value);
214                            }
215                    }
216            }
217    
218            protected static void transferFileChannel(
219                            FileChannel inputFileChannel, FileChannel outputFileChannel,
220                            long length)
221                    throws IOException {
222    
223                    if (length <= 0) {
224                            length = inputFileChannel.size() - inputFileChannel.position();
225                    }
226    
227                    long count = 0;
228    
229                    while (count < length) {
230                            count += inputFileChannel.transferTo(
231                                    inputFileChannel.position() + count, length - count,
232                                    outputFileChannel);
233                    }
234            }
235    
236            private static final Log _log = LogFactoryUtil.getLog(StreamUtil.class);
237    
238    }