31 |
/** Utility methods for copying data from input to output streams. |
/** Utility methods for copying data from input to output streams. |
32 |
*/ |
*/ |
33 |
public class CopyUtil { |
public class CopyUtil { |
34 |
|
static private int BLOCKSIZE = 4096; |
35 |
|
|
36 |
static public int copy(InputStream from, OutputStream to) |
static public int copy(InputStream from, OutputStream to) |
37 |
throws IOException { |
throws IOException { |
38 |
return copy(from, to, 4096); |
return copy(from, to, BLOCKSIZE, true); |
39 |
|
} |
40 |
|
|
41 |
|
static public int copy(InputStream from, OutputStream to, |
42 |
|
int blocksize) |
43 |
|
throws IOException { |
44 |
|
return copy(from, to, blocksize, true); |
45 |
|
} |
46 |
|
|
47 |
|
static public int copy(InputStream from, OutputStream to, |
48 |
|
boolean close) |
49 |
|
throws IOException { |
50 |
|
return copy(from, to, BLOCKSIZE, close); |
51 |
} |
} |
52 |
|
|
53 |
/** Copy data from an input to an output stream in blocks of a given size. |
/** Copy data from an input to an output stream in blocks of a given size. |
54 |
* Closes both streams when the copy operation is complete. |
* If <code>close</code> is true, both streams are closed |
55 |
|
* when the copy operation is complete. |
56 |
*/ |
*/ |
57 |
static public int copy(InputStream from, OutputStream to, int blockSize) |
static public int copy(InputStream from, OutputStream to, |
58 |
|
int blockSize, boolean close) |
59 |
throws IOException { |
throws IOException { |
60 |
try { |
try { |
61 |
byte[] buf = new byte[blockSize]; |
byte[] buf = new byte[blockSize]; |
73 |
//p("... all read."); |
//p("... all read."); |
74 |
return bytesCopied; |
return bytesCopied; |
75 |
} finally { |
} finally { |
76 |
from.close(); |
if(close) { |
77 |
to.close(); |
from.close(); |
78 |
|
to.close(); |
79 |
|
} |
80 |
} |
} |
81 |
} |
} |
82 |
|
|