001
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.IOException;
021 import java.io.InputStream;
022 import java.io.OutputStream;
023
024 import java.nio.ByteBuffer;
025 import java.nio.channels.Channel;
026 import java.nio.channels.Channels;
027 import java.nio.channels.ReadableByteChannel;
028 import java.nio.channels.WritableByteChannel;
029
030
034 public class StreamUtil {
035
036 public static final int BUFFER_SIZE = GetterUtil.getInteger(
037 System.getProperty(StreamUtil.class.getName() + ".buffer.size"),
038 8192);
039
040 public static final boolean USE_NIO = GetterUtil.getBoolean(
041 System.getProperty(StreamUtil.class.getName() + ".use.nio"),
042 false);
043
044 public static void cleanUp(Channel channel) {
045 try {
046 if (channel != null) {
047 channel.close();
048 }
049 }
050 catch (Exception e) {
051 if (_log.isWarnEnabled()) {
052 _log.warn(e, e);
053 }
054 }
055 }
056
057 public static void cleanUp(Channel inputChannel, Channel outputChannel) {
058 cleanUp(inputChannel);
059 cleanUp(outputChannel);
060 }
061
062 public static void cleanUp(InputStream inputStream) {
063 try {
064 if (inputStream != null) {
065 inputStream.close();
066 }
067 }
068 catch (Exception e) {
069 if (_log.isWarnEnabled()) {
070 _log.warn(e, e);
071 }
072 }
073 }
074
075 public static void cleanUp(
076 InputStream inputStream, OutputStream outputStream) {
077
078 cleanUp(outputStream);
079 cleanUp(inputStream);
080 }
081
082 public static void cleanUp(OutputStream outputStream) {
083 try {
084 if (outputStream != null) {
085 outputStream.flush();
086 }
087 }
088 catch (Exception e) {
089 if (_log.isWarnEnabled()) {
090 _log.warn(e, e);
091 }
092 }
093
094 try {
095 if (outputStream != null) {
096 outputStream.close();
097 }
098 }
099 catch (Exception e) {
100 if (_log.isWarnEnabled()) {
101 _log.warn(e, e);
102 }
103 }
104 }
105
106 public static void transfer(
107 InputStream inputStream, OutputStream outputStream)
108 throws IOException {
109
110 transfer(inputStream, outputStream, BUFFER_SIZE, true);
111 }
112
113 public static void transfer(
114 InputStream inputStream, OutputStream outputStream, boolean cleanUp)
115 throws IOException {
116
117 transfer(inputStream, outputStream, BUFFER_SIZE, cleanUp);
118 }
119
120 public static void transfer(
121 InputStream inputStream, OutputStream outputStream, int bufferSize)
122 throws IOException {
123
124 transfer(inputStream, outputStream, bufferSize, true);
125 }
126
127 public static void transfer(
128 InputStream inputStream, OutputStream outputStream, int bufferSize,
129 boolean cleanUp)
130 throws IOException {
131
132 if (inputStream == null) {
133 throw new IllegalArgumentException("Input stream cannot be null");
134 }
135
136 if (outputStream == null) {
137 throw new IllegalArgumentException("Output stream cannot be null");
138 }
139
140 if (bufferSize <= 0) {
141 bufferSize = BUFFER_SIZE;
142 }
143
144 if (USE_NIO) {
145 ReadableByteChannel readableByteChannel = Channels.newChannel(
146 inputStream);
147 WritableByteChannel writableByteChannel = Channels.newChannel(
148 outputStream);
149
150 transfer(
151 readableByteChannel, writableByteChannel, bufferSize, cleanUp);
152 }
153 else {
154 try {
155 byte[] bytes = new byte[bufferSize];
156
157 int value = -1;
158
159 while ((value = inputStream.read(bytes)) != -1) {
160 outputStream.write(bytes, 0 , value);
161 }
162 }
163 finally {
164 if (cleanUp) {
165 cleanUp(inputStream, outputStream);
166 }
167 }
168 }
169 }
170
171 public static void transfer(
172 ReadableByteChannel readableByteChannel,
173 WritableByteChannel writableByteChannel)
174 throws IOException {
175
176 transfer(readableByteChannel, writableByteChannel, BUFFER_SIZE);
177 }
178
179 public static void transfer(
180 ReadableByteChannel readableByteChannel,
181 WritableByteChannel writableByteChannel, boolean cleanUp)
182 throws IOException {
183
184 transfer(
185 readableByteChannel, writableByteChannel, BUFFER_SIZE, cleanUp);
186 }
187
188 public static void transfer(
189 ReadableByteChannel readableByteChannel,
190 WritableByteChannel writableByteChannel, int bufferSize)
191 throws IOException {
192
193 transfer(readableByteChannel, writableByteChannel, bufferSize, true);
194 }
195
196 public static void transfer(
197 ReadableByteChannel readableByteChannel,
198 WritableByteChannel writableByteChannel, int bufferSize,
199 boolean cleanUp)
200 throws IOException {
201
202 try {
203 if (readableByteChannel == null) {
204 throw new IllegalArgumentException(
205 "Readable byte channel cannot be null");
206 }
207
208 if (writableByteChannel == null) {
209 throw new IllegalArgumentException(
210 "Writable byte channel cannot be null");
211 }
212
213 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferSize);
214
215 while (readableByteChannel.read(byteBuffer) != -1) {
216 byteBuffer.flip();
217
218 writableByteChannel.write(byteBuffer);
219
220 byteBuffer.compact();
221 }
222
223 byteBuffer.flip();
224
225 while (byteBuffer.hasRemaining()) {
226 writableByteChannel.write(byteBuffer);
227 }
228 }
229 finally {
230 if (cleanUp) {
231 cleanUp(readableByteChannel, writableByteChannel);
232 }
233 }
234 }
235
236 private static Log _log = LogFactoryUtil.getLog(StreamUtil.class);
237
238 }