1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  
32  import java.nio.ByteBuffer;
33  import java.nio.channels.Channel;
34  import java.nio.channels.Channels;
35  import java.nio.channels.ReadableByteChannel;
36  import java.nio.channels.WritableByteChannel;
37  
38  /**
39   * <a href="StreamUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Raymond Augé
43   */
44  public class StreamUtil {
45  
46      public static final int BUFFER_SIZE = GetterUtil.getInteger(
47          System.getProperty(
48              "com.liferay.portal.kernel.util.StreamUtil.buffer.size"),
49          8192);
50  
51      public static final boolean USE_NIO = GetterUtil.getBoolean(
52          System.getProperty(
53              "com.liferay.portal.kernel.util.StreamUtil.use.nio"),
54          false);
55  
56      public static void cleanUp(Channel channel) {
57          try {
58              if (channel != null) {
59                  channel.close();
60              }
61          }
62          catch (Exception e) {
63              if (_log.isWarnEnabled()) {
64                  _log.warn(e, e);
65              }
66          }
67      }
68  
69      public static void cleanUp(Channel inputChannel, Channel outputChannel) {
70          cleanUp(inputChannel);
71          cleanUp(outputChannel);
72      }
73  
74      public static void cleanUp(InputStream inputStream) {
75          try {
76              if (inputStream != null) {
77                  inputStream.close();
78              }
79          }
80          catch (Exception e) {
81              if (_log.isWarnEnabled()) {
82                  _log.warn(e, e);
83              }
84          }
85      }
86  
87      public static void cleanUp(
88          InputStream inputStream, OutputStream outputStream) {
89  
90          cleanUp(outputStream);
91          cleanUp(inputStream);
92      }
93  
94      public static void cleanUp(OutputStream outputStream) {
95          try {
96              if (outputStream != null) {
97                  outputStream.flush();
98              }
99          }
100         catch (Exception e) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(e, e);
103             }
104         }
105 
106         try {
107             if (outputStream != null) {
108                 outputStream.close();
109             }
110         }
111         catch (Exception e) {
112             if (_log.isWarnEnabled()) {
113                 _log.warn(e, e);
114             }
115         }
116     }
117 
118     public static void transfer(
119             InputStream inputStream, OutputStream outputStream)
120         throws IOException {
121 
122         transfer(inputStream, outputStream, BUFFER_SIZE, true);
123     }
124 
125     public static void transfer(
126             InputStream inputStream, OutputStream outputStream, boolean cleanUp)
127         throws IOException {
128 
129         transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
130     }
131 
132     public static void transfer(
133             InputStream inputStream, OutputStream outputStream, int bufferSize)
134         throws IOException {
135 
136         transfer(inputStream, outputStream, bufferSize, true);
137     }
138 
139     public static void transfer(
140             InputStream inputStream, OutputStream outputStream, int bufferSize,
141             boolean cleanUp)
142         throws IOException {
143 
144         if (inputStream == null) {
145             throw new IllegalArgumentException("Input stream cannot be null");
146         }
147 
148         if (outputStream == null) {
149             throw new IllegalArgumentException("Output stream cannot be null");
150         }
151 
152         if (bufferSize <= 0) {
153             bufferSize = BUFFER_SIZE;
154         }
155 
156         if (USE_NIO) {
157             ReadableByteChannel readableByteChannel = Channels.newChannel(
158                 inputStream);
159             WritableByteChannel writableByteChannel = Channels.newChannel(
160                 outputStream);
161 
162             transfer(
163                 readableByteChannel, writableByteChannel, bufferSize, cleanUp);
164         }
165         else {
166             try {
167                 byte[] bytes = new byte[bufferSize];
168 
169                 int value = -1;
170 
171                 while ((value = inputStream.read(bytes)) != -1) {
172                     outputStream.write(bytes, 0 , value);
173                 }
174             }
175             finally {
176                 if (cleanUp) {
177                     cleanUp(inputStream, outputStream);
178                 }
179             }
180         }
181     }
182 
183     public static void transfer(
184             ReadableByteChannel readableByteChannel,
185             WritableByteChannel writableByteChannel)
186         throws IOException {
187 
188         transfer(readableByteChannel, writableByteChannel, BUFFER_SIZE);
189     }
190 
191     public static void transfer(
192             ReadableByteChannel readableByteChannel,
193             WritableByteChannel writableByteChannel, boolean cleanUp)
194         throws IOException {
195 
196         transfer(
197             readableByteChannel, writableByteChannel, BUFFER_SIZE, cleanUp);
198     }
199 
200     public static void transfer(
201             ReadableByteChannel readableByteChannel,
202             WritableByteChannel writableByteChannel, int bufferSize)
203         throws IOException {
204 
205         transfer(readableByteChannel, writableByteChannel, bufferSize, true);
206     }
207 
208     public static void transfer(
209             ReadableByteChannel readableByteChannel,
210             WritableByteChannel writableByteChannel, int bufferSize,
211             boolean cleanUp)
212         throws IOException {
213 
214         if (readableByteChannel == null) {
215             throw new IllegalArgumentException(
216                 "Readable byte channel cannot be null");
217         }
218 
219         if (writableByteChannel == null) {
220             throw new IllegalArgumentException(
221                 "Writable byte channel cannot be null");
222         }
223 
224         try {
225             ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
226 
227             while (readableByteChannel.read(byteBuffer) != -1) {
228                 byteBuffer.flip();
229 
230                 writableByteChannel.write(byteBuffer);
231 
232                 byteBuffer.compact();
233             }
234 
235             byteBuffer.flip();
236 
237             while (byteBuffer.hasRemaining()) {
238                 writableByteChannel.write(byteBuffer);
239             }
240         }
241         finally {
242             if (cleanUp) {
243                 cleanUp(readableByteChannel, writableByteChannel);
244             }
245         }
246     }
247 
248     private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
249 
250 }