1 16 package org.apache.commons.net.io; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 24 34 35 public final class Util 36 { 37 41 public static final int DEFAULT_COPY_BUFFER_SIZE = 1024; 42 43 private Util() 45 { } 46 47 48 83 public static final long copyStream(InputStream source, OutputStream dest, 84 int bufferSize, long streamSize, 85 CopyStreamListener listener, 86 boolean flush) 87 throws CopyStreamException 88 { 89 int bytes; 90 long total; 91 byte[] buffer; 92 93 buffer = new byte[bufferSize]; 94 total = 0; 95 96 try 97 { 98 while ((bytes = source.read(buffer)) != -1) 99 { 100 103 if (bytes == 0) 104 { 105 bytes = source.read(); 106 if (bytes < 0) 107 break; 108 dest.write(bytes); 109 if(flush) 110 dest.flush(); 111 ++total; 112 if (listener != null) 113 listener.bytesTransferred(total, 1, streamSize); 114 continue; 115 } 116 117 dest.write(buffer, 0, bytes); 118 if(flush) 119 dest.flush(); 120 total += bytes; 121 if (listener != null) 122 listener.bytesTransferred(total, bytes, streamSize); 123 } 124 } 125 catch (IOException e) 126 { 127 throw new CopyStreamException("IOException caught while copying.", 128 total, e); 129 } 130 131 return total; 132 } 133 134 135 166 public static final long copyStream(InputStream source, OutputStream dest, 167 int bufferSize, long streamSize, 168 CopyStreamListener listener) 169 throws CopyStreamException 170 { 171 return copyStream(source, dest, bufferSize, streamSize, listener, 172 true); 173 } 174 175 176 196 public static final long copyStream(InputStream source, OutputStream dest, 197 int bufferSize) 198 throws CopyStreamException 199 { 200 return copyStream(source, dest, bufferSize, 201 CopyStreamEvent.UNKNOWN_STREAM_SIZE, null); 202 } 203 204 205 208 public static final long copyStream(InputStream source, OutputStream dest) 209 throws CopyStreamException 210 { 211 return copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE); 212 } 213 214 215 246 public static final long copyReader(Reader source, Writer dest, 247 int bufferSize, long streamSize, 248 CopyStreamListener listener) 249 throws CopyStreamException 250 { 251 int chars; 252 long total; 253 char[] buffer; 254 255 buffer = new char[bufferSize]; 256 total = 0; 257 258 try 259 { 260 while ((chars = source.read(buffer)) != -1) 261 { 262 if (chars == 0) 265 { 266 chars = source.read(); 267 if (chars < 0) 268 break; 269 dest.write(chars); 270 dest.flush(); 271 ++total; 272 if (listener != null) 273 listener.bytesTransferred(total, chars, streamSize); 274 continue; 275 } 276 277 dest.write(buffer, 0, chars); 278 dest.flush(); 279 total += chars; 280 if (listener != null) 281 listener.bytesTransferred(total, chars, streamSize); 282 } 283 } 284 catch (IOException e) 285 { 286 throw new CopyStreamException("IOException caught while copying.", 287 total, e); 288 } 289 290 return total; 291 } 292 293 294 314 public static final long copyReader(Reader source, Writer dest, 315 int bufferSize) 316 throws CopyStreamException 317 { 318 return copyReader(source, dest, bufferSize, 319 CopyStreamEvent.UNKNOWN_STREAM_SIZE, null); 320 } 321 322 323 326 public static final long copyReader(Reader source, Writer dest) 327 throws CopyStreamException 328 { 329 return copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE); 330 } 331 332 } 333 | Popular Tags |