1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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(
40              "com.liferay.portal.kernel.util.StreamUtil.buffer.size"),
41          8192);
42  
43      public static final boolean USE_NIO = GetterUtil.getBoolean(
44          System.getProperty(
45              "com.liferay.portal.kernel.util.StreamUtil.use.nio"),
46          false);
47  
48      public static void cleanUp(Channel channel) {
49          try {
50              if (channel != null) {
51                  channel.close();
52              }
53          }
54          catch (Exception e) {
55              if (_log.isWarnEnabled()) {
56                  _log.warn(e, e);
57              }
58          }
59      }
60  
61      public static void cleanUp(Channel inputChannel, Channel outputChannel) {
62          cleanUp(inputChannel);
63          cleanUp(outputChannel);
64      }
65  
66      public static void cleanUp(InputStream inputStream) {
67          try {
68              if (inputStream != null) {
69                  inputStream.close();
70              }
71          }
72          catch (Exception e) {
73              if (_log.isWarnEnabled()) {
74                  _log.warn(e, e);
75              }
76          }
77      }
78  
79      public static void cleanUp(
80          InputStream inputStream, OutputStream outputStream) {
81  
82          cleanUp(outputStream);
83          cleanUp(inputStream);
84      }
85  
86      public static void cleanUp(OutputStream outputStream) {
87          try {
88              if (outputStream != null) {
89                  outputStream.flush();
90              }
91          }
92          catch (Exception e) {
93              if (_log.isWarnEnabled()) {
94                  _log.warn(e, e);
95              }
96          }
97  
98          try {
99              if (outputStream != null) {
100                 outputStream.close();
101             }
102         }
103         catch (Exception e) {
104             if (_log.isWarnEnabled()) {
105                 _log.warn(e, e);
106             }
107         }
108     }
109 
110     public static void transfer(
111             InputStream inputStream, OutputStream outputStream)
112         throws IOException {
113 
114         transfer(inputStream, outputStream, BUFFER_SIZE, true);
115     }
116 
117     public static void transfer(
118             InputStream inputStream, OutputStream outputStream, boolean cleanUp)
119         throws IOException {
120 
121         transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
122     }
123 
124     public static void transfer(
125             InputStream inputStream, OutputStream outputStream, int bufferSize)
126         throws IOException {
127 
128         transfer(inputStream, outputStream, bufferSize, true);
129     }
130 
131     public static void transfer(
132             InputStream inputStream, OutputStream outputStream, int bufferSize,
133             boolean cleanUp)
134         throws IOException {
135 
136         if (inputStream == null) {
137             throw new IllegalArgumentException("Input stream cannot be null");
138         }
139 
140         if (outputStream == null) {
141             throw new IllegalArgumentException("Output stream cannot be null");
142         }
143 
144         if (bufferSize <= 0) {
145             bufferSize = BUFFER_SIZE;
146         }
147 
148         if (USE_NIO) {
149             ReadableByteChannel readableByteChannel = Channels.newChannel(
150                 inputStream);
151             WritableByteChannel writableByteChannel = Channels.newChannel(
152                 outputStream);
153 
154             transfer(
155                 readableByteChannel, writableByteChannel, bufferSize, cleanUp);
156         }
157         else {
158             try {
159                 byte[] bytes = new byte[bufferSize];
160 
161                 int value = -1;
162 
163                 while ((value = inputStream.read(bytes)) != -1) {
164                     outputStream.write(bytes, 0 , value);
165                 }
166             }
167             finally {
168                 if (cleanUp) {
169                     cleanUp(inputStream, outputStream);
170                 }
171             }
172         }
173     }
174 
175     public static void transfer(
176             ReadableByteChannel readableByteChannel,
177             WritableByteChannel writableByteChannel)
178         throws IOException {
179 
180         transfer(readableByteChannel, writableByteChannel, BUFFER_SIZE);
181     }
182 
183     public static void transfer(
184             ReadableByteChannel readableByteChannel,
185             WritableByteChannel writableByteChannel, boolean cleanUp)
186         throws IOException {
187 
188         transfer(
189             readableByteChannel, writableByteChannel, BUFFER_SIZE, cleanUp);
190     }
191 
192     public static void transfer(
193             ReadableByteChannel readableByteChannel,
194             WritableByteChannel writableByteChannel, int bufferSize)
195         throws IOException {
196 
197         transfer(readableByteChannel, writableByteChannel, bufferSize, true);
198     }
199 
200     public static void transfer(
201             ReadableByteChannel readableByteChannel,
202             WritableByteChannel writableByteChannel, int bufferSize,
203             boolean cleanUp)
204         throws IOException {
205 
206         if (readableByteChannel == null) {
207             throw new IllegalArgumentException(
208                 "Readable byte channel cannot be null");
209         }
210 
211         if (writableByteChannel == null) {
212             throw new IllegalArgumentException(
213                 "Writable byte channel cannot be null");
214         }
215 
216         try {
217             ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
218 
219             while (readableByteChannel.read(byteBuffer) != -1) {
220                 byteBuffer.flip();
221 
222                 writableByteChannel.write(byteBuffer);
223 
224                 byteBuffer.compact();
225             }
226 
227             byteBuffer.flip();
228 
229             while (byteBuffer.hasRemaining()) {
230                 writableByteChannel.write(byteBuffer);
231             }
232         }
233         finally {
234             if (cleanUp) {
235                 cleanUp(readableByteChannel, writableByteChannel);
236             }
237         }
238     }
239 
240     private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
241 
242 }