1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.util;
36
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40
41 /***
42 * Contains various utiity functions related to io operations.
43 *
44 * @author Markus
45 * @version $Id: IOUtil.java,v 1.1 2005/04/07 19:58:36 c95men Exp $
46 */
47 public final class IOUtil
48 {
49 /***
50 * The default buffer size to use for the copy method.
51 */
52 private static final int DEFAULT_COPY_SIZE = 1024 * 8;
53
54 /***
55 * This class isn't intended to be instantiated.
56 */
57 private IOUtil()
58 {
59
60 }
61
62 /***
63 * Copy data from in to out using the workbuff as temporary storage.
64 *
65 * @param in stream to read from
66 * @param out stream to write to
67 * @param workbuff buffer to use as temporary storage while copying
68 * @return number of bytes copied
69 * @throws IOException if an I/O error occurs
70 */
71 public static int copy(InputStream in, OutputStream out, byte[] workbuff)
72 throws IOException
73 {
74 int bytescopied = 0;
75 int bytesread = 0;
76
77 while ((bytesread = in.read(workbuff)) != -1)
78 {
79 out.write(workbuff, 0, bytesread);
80 bytescopied += bytesread;
81 }
82
83 return bytescopied;
84 }
85
86 /***
87 * Copy data from in to out using a temporary buffer of workbuffsize bytes.
88 *
89 * @param in stream to read from
90 * @param out stream to write to
91 * @param workbuffsize how large the work buffer should be
92 * @return number of bytes copied
93 * @throws IOException if an I/O error occurs
94 */
95 public static int copy(InputStream in, OutputStream out, int workbuffsize)
96 throws IOException
97 {
98 return IOUtil.copy(in, out, new byte[workbuffsize]);
99 }
100
101 /***
102 * Copy data from in to out using a default temporary buffer.
103 *
104 * @param in stream to read from
105 * @param out stream to write to
106 * @return number of bytes copied
107 * @throws IOException if an I/O error occurs
108 */
109 public static int copy(InputStream in, OutputStream out)
110 throws IOException
111 {
112 return IOUtil.copy(in, out, new byte[DEFAULT_COPY_SIZE]);
113 }
114 }