001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.common.util;
023
024 import java.io.ByteArrayOutputStream;
025 import java.io.IOException;
026 import java.io.InputStream;
027 import java.io.InputStreamReader;
028 import java.io.OutputStream;
029 import java.io.Reader;
030 import java.io.Writer;
031
032 /**
033 * @author Randall Hauch
034 */
035 public class IoUtil {
036
037 /**
038 * Read and return the entire contents of the supplied {@link InputStream stream}. This method always closes the stream when
039 * finished reading.
040 * @param stream the stream to the contents; may be null
041 * @return the contents, or an empty byte array if the supplied reader is null
042 * @throws IOException if there is an error reading the content
043 */
044 public static byte[] readBytes( InputStream stream ) throws IOException {
045 if (stream == null) return new byte[] {};
046 byte[] buffer = new byte[1024];
047 ByteArrayOutputStream output = new ByteArrayOutputStream();
048 try {
049 int numRead = 0;
050 while ((numRead = stream.read(buffer)) > -1) {
051 output.write(buffer, 0, numRead);
052 }
053 } finally {
054 stream.close();
055 }
056 output.flush();
057 return output.toByteArray();
058 }
059
060 /**
061 * Read and return the entire contents of the supplied {@link Reader}. This method always closes the reader when finished
062 * reading.
063 * @param reader the reader of the contents; may be null
064 * @return the contents, or an empty string if the supplied reader is null
065 * @throws IOException if there is an error reading the content
066 */
067 public static String read( Reader reader ) throws IOException {
068 if (reader == null) return "";
069 char[] buffer = new char[1024];
070 StringBuffer sb = new StringBuffer();
071 try {
072 int numRead = 0;
073 while ((numRead = reader.read(buffer)) > -1) {
074 sb.append(buffer, 0, numRead);
075 }
076 } finally {
077 reader.close();
078 }
079 return sb.toString();
080 }
081
082 /**
083 * Read and return the entire contents of the supplied {@link InputStream}. This method always closes the stream when
084 * finished reading.
085 * @param stream the streamed contents; may be null
086 * @return the contents, or an empty string if the supplied stream is null
087 * @throws IOException if there is an error reading the content
088 */
089 public static String read( InputStream stream ) throws IOException {
090 return stream == null ? "" : read(new InputStreamReader(stream));
091 }
092
093 /**
094 * Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when
095 * finished.
096 * @param content the content to write to the stream; may be null
097 * @param stream the stream to which the content is to be written
098 * @throws IOException
099 * @throws IllegalArgumentException if the stream is null
100 */
101 public static void write( String content, OutputStream stream ) throws IOException {
102 CheckArg.isNotNull(stream, "destination stream");
103 try {
104 if (content != null) {
105 byte[] bytes = content.getBytes();
106 stream.write(bytes, 0, bytes.length);
107 }
108 } finally {
109 try {
110 stream.flush();
111 } finally {
112 stream.close();
113 }
114 }
115 }
116
117 /**
118 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
119 * finished.
120 * @param content the content to write to the writer; may be null
121 * @param writer the writer to which the content is to be written
122 * @throws IOException
123 * @throws IllegalArgumentException if the writer is null
124 */
125 public static void write( String content, Writer writer ) throws IOException {
126 CheckArg.isNotNull(writer, "destination writer");
127 try {
128 if (content != null) {
129 writer.write(content);
130 }
131 } finally {
132 try {
133 writer.flush();
134 } finally {
135 writer.close();
136 }
137 }
138 }
139
140 /**
141 * Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when
142 * finished.
143 * @param input the content to write to the stream; may be null
144 * @param stream the stream to which the content is to be written
145 * @throws IOException
146 * @throws IllegalArgumentException if the stream is null
147 */
148 public static void write( InputStream input, OutputStream stream ) throws IOException {
149 CheckArg.isNotNull(stream, "destination stream");
150 try {
151 if (input != null) {
152 byte[] buffer = new byte[1024];
153 try {
154 int numRead = 0;
155 while ((numRead = input.read(buffer)) > -1) {
156 stream.write(buffer, 0, numRead);
157 }
158 } finally {
159 input.close();
160 }
161 }
162 } finally {
163 try {
164 stream.flush();
165 } finally {
166 stream.close();
167 }
168 }
169 }
170
171 /**
172 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
173 * finished.
174 * @param input the content to write to the writer; may be null
175 * @param writer the writer to which the content is to be written
176 * @throws IOException
177 * @throws IllegalArgumentException if the writer is null
178 */
179 public static void write( Reader input, Writer writer ) throws IOException {
180 CheckArg.isNotNull(writer, "destination writer");
181 try {
182 if (input != null) {
183 char[] buffer = new char[1024];
184 try {
185 int numRead = 0;
186 while ((numRead = input.read(buffer)) > -1) {
187 writer.write(buffer, 0, numRead);
188 }
189 } finally {
190 input.close();
191 }
192 }
193 } finally {
194 try {
195 writer.flush();
196 } finally {
197 writer.close();
198 }
199 }
200 }
201
202 private IoUtil() {
203 // Prevent construction
204 }
205 }