1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.util;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.util.logging.Logger;
28
29 /***
30 * @author carrd
31 */
32 public class IOUtil {
33 private static final Logger log = Logger.getLogger(IOUtil.class.getName());
34 /***
35 * The data transfer block size.
36 */
37 private static final int BLOCK_SIZE = 4096;
38
39 /***
40 * Closes the specified stream, ignoring any exceptions thrown
41 *
42 * @param is the stream to close
43 */
44 public static void closeStream(InputStream is) {
45 if (is != null) {
46 try {
47 is.close();
48 } catch (Exception e) {
49 log.finest("Error closing stream: " + e.getMessage());
50 }
51 }
52 }
53
54 /***
55 * Closes the specified stream, ignoring any exceptions thrown
56 *
57 * @param os the stream to close
58 */
59 public static void closeStream(OutputStream os) {
60 if (os != null) {
61 try {
62 os.close();
63 } catch (Exception e) {
64 log.finest("Error closing stream: " + e.getMessage());
65 }
66 }
67 }
68
69 /***
70 * Closes the specified reader, ignoring any exceptions thrown
71 *
72 * @param r the reader to close
73 */
74 public static void closeReader(Reader r) {
75 if (r != null) {
76 try {
77 r.close();
78 } catch (Exception e) {
79 log.finest("Error closing reader: " + e.getMessage());
80 }
81 }
82 }
83
84 /***
85 * Read all of the data from a stream, writing it to another stream. Reads
86 * data from the input stream and writes it to the output stream, until no
87 * more data is available.
88 *
89 * @param input The input stream.
90 * @param output The output stream.
91 * @return the output stream
92 *
93 * @exception IOException If an error occurred while reading from the stream.
94 */
95 static final OutputStream readStreamToStream(
96 InputStream input,
97 OutputStream output)
98 throws IOException {
99 byte buf[] = new byte[BLOCK_SIZE];
100 int b;
101 while ((b = input.read(buf)) > 0) {
102 output.write(buf, 0, b);
103 }
104 return output;
105 }
106
107 /***
108 * Read all of the data from a stream, returning the contents as a
109 * <code>String</code>. Note that this method is not unicode-aware.
110 *
111 * @param input The stream to read from.
112 * @return The contents of the stream, as a <code>String</code>.
113 * @throws IOException If an error occurred while reading from the stream.
114 */
115 public static final String readStreamToString(InputStream input)
116 throws IOException {
117 return readStream(input).toString();
118 }
119
120 /***
121 * Write a string to a stream. Note that this method is not unicode-aware.
122 *
123 * @param s The string to write.
124 * @param output The stream to write it to.
125 * @throws IOException If an error occurred while writing to the stream.
126 */
127 static final void writeStringToStream(String s, OutputStream output)
128 throws IOException {
129 InputStream input = new ByteArrayInputStream(s.getBytes());
130 readStreamToStream(input, output);
131 }
132
133 /***
134 * Read all of the data from a stream, returning the contents as a
135 * <code>byte</code> array.
136 *
137 * @param input The stream to read from.
138 * @return The contents of the stream, as a <code>byte</code> array.
139 * @throws IOException If an error occurred while reading from the stream.
140 */
141 public static final byte[] readStreamToByteArray(InputStream input)
142 throws IOException {
143 return readStream(input).toByteArray();
144 }
145
146 /***
147 * Fully read an {@link InputStream}, writing the data read to a
148 * {@link ByteArrayOutputStream}.
149 *
150 * @param input the stream to read in
151 * @return a reference to the resulting stream
152 * @throws IOException if there is an error reading from the stream
153 */
154 static final ByteArrayOutputStream readStream(InputStream input)
155 throws IOException {
156 ByteArrayOutputStream output = new ByteArrayOutputStream(BLOCK_SIZE);
157 readStreamToStream(input, output);
158 return output;
159 }
160 }
This page was automatically generated by Maven