1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import java.nio.ByteBuffer;
25  import java.nio.channels.Channel;
26  import java.nio.channels.Channels;
27  import java.nio.channels.ReadableByteChannel;
28  import java.nio.channels.WritableByteChannel;
29  
30  /**
31   * <a href="StreamUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Raymond Augé
35   */
36  public class StreamUtil {
37  
38      public static final int BUFFER_SIZE = GetterUtil.getInteger(
39          System.getProperty(StreamUtil.class.getName() + ".buffer.size"),
40          8192);
41  
42      public static final boolean USE_NIO = GetterUtil.getBoolean(
43          System.getProperty(StreamUtil.class.getName() + ".use.nio"),
44          false);
45  
46      public static void cleanUp(Channel channel) {
47          try {
48              if (channel != null) {
49                  channel.close();
50              }
51          }
52          catch (Exception e) {
53              if (_log.isWarnEnabled()) {
54                  _log.warn(e, e);
55              }
56          }
57      }
58  
59      public static void cleanUp(Channel inputChannel, Channel outputChannel) {
60          cleanUp(inputChannel);
61          cleanUp(outputChannel);
62      }
63  
64      public static void cleanUp(InputStream inputStream) {
65          try {
66              if (inputStream != null) {
67                  inputStream.close();
68              }
69          }
70          catch (Exception e) {
71              if (_log.isWarnEnabled()) {
72                  _log.warn(e, e);
73              }
74          }
75      }
76  
77      public static void cleanUp(
78          InputStream inputStream, OutputStream outputStream) {
79  
80          cleanUp(outputStream);
81          cleanUp(inputStream);
82      }
83  
84      public static void cleanUp(OutputStream outputStream) {
85          try {
86              if (outputStream != null) {
87                  outputStream.flush();
88              }
89          }
90          catch (Exception e) {
91              if (_log.isWarnEnabled()) {
92                  _log.warn(e, e);
93              }
94          }
95  
96          try {
97              if (outputStream != null) {
98                  outputStream.close();
99              }
100         }
101         catch (Exception e) {
102             if (_log.isWarnEnabled()) {
103                 _log.warn(e, e);
104             }
105         }
106     }
107 
108     public static void transfer(
109             InputStream inputStream, OutputStream outputStream)
110         throws IOException {
111 
112         transfer(inputStream, outputStream, BUFFER_SIZE, true);
113     }
114 
115     public static void transfer(
116             InputStream inputStream, OutputStream outputStream, boolean cleanUp)
117         throws IOException {
118 
119         transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
120     }
121 
122     public static void transfer(
123             InputStream inputStream, OutputStream outputStream, int bufferSize)
124         throws IOException {
125 
126         transfer(inputStream, outputStream, bufferSize, true);
127     }
128 
129     public static void transfer(
130             InputStream inputStream, OutputStream outputStream, int bufferSize,
131             boolean cleanUp)
132         throws IOException {
133 
134         if (inputStream == null) {
135             throw new IllegalArgumentException("Input stream cannot be null");
136         }
137 
138         if (outputStream == null) {
139             throw new IllegalArgumentException("Output stream cannot be null");
140         }
141 
142         if (bufferSize <= 0) {
143             bufferSize = BUFFER_SIZE;
144         }
145 
146         if (USE_NIO) {
147             ReadableByteChannel readableByteChannel = Channels.newChannel(
148                 inputStream);
149             WritableByteChannel writableByteChannel = Channels.newChannel(
150                 outputStream);
151 
152             transfer(
153                 readableByteChannel, writableByteChannel, bufferSize, cleanUp);
154         }
155         else {
156             try {
157                 byte[] bytes = new byte[bufferSize];
158 
159                 int value = -1;
160 
161                 while ((value = inputStream.read(bytes)) != -1) {
162                     outputStream.write(bytes, 0 , value);
163                 }
164             }
165             finally {
166                 if (cleanUp) {
167                     cleanUp(inputStream, outputStream);
168                 }
169             }
170         }
171     }
172 
173     public static void transfer(
174             ReadableByteChannel readableByteChannel,
175             WritableByteChannel writableByteChannel)
176         throws IOException {
177 
178         transfer(readableByteChannel, writableByteChannel, BUFFER_SIZE);
179     }
180 
181     public static void transfer(
182             ReadableByteChannel readableByteChannel,
183             WritableByteChannel writableByteChannel, boolean cleanUp)
184         throws IOException {
185 
186         transfer(
187             readableByteChannel, writableByteChannel, BUFFER_SIZE, cleanUp);
188     }
189 
190     public static void transfer(
191             ReadableByteChannel readableByteChannel,
192             WritableByteChannel writableByteChannel, int bufferSize)
193         throws IOException {
194 
195         transfer(readableByteChannel, writableByteChannel, bufferSize, true);
196     }
197 
198     public static void transfer(
199             ReadableByteChannel readableByteChannel,
200             WritableByteChannel writableByteChannel, int bufferSize,
201             boolean cleanUp)
202         throws IOException {
203 
204         try {
205             if (readableByteChannel == null) {
206                 throw new IllegalArgumentException(
207                     "Readable byte channel cannot be null");
208             }
209 
210             if (writableByteChannel == null) {
211                 throw new IllegalArgumentException(
212                     "Writable byte channel cannot be null");
213             }
214 
215             ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
216 
217             while (readableByteChannel.read(byteBuffer) != -1) {
218                 byteBuffer.flip();
219 
220                 writableByteChannel.write(byteBuffer);
221 
222                 byteBuffer.compact();
223             }
224 
225             byteBuffer.flip();
226 
227             while (byteBuffer.hasRemaining()) {
228                 writableByteChannel.write(byteBuffer);
229             }
230         }
231         finally {
232             if (cleanUp) {
233                 cleanUp(readableByteChannel, writableByteChannel);
234             }
235         }
236     }
237 
238     private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
239 
240 }