1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io;
  17. import java.io.BufferedInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.io.Reader;
  22. import java.io.StringWriter;
  23. import java.io.Writer;
  24. import org.apache.commons.io.output.ByteArrayOutputStream;
  25. /**
  26. * General IO Stream manipulation.
  27. * <p>
  28. * This class provides static utility methods for input/output operations.
  29. * </p>
  30. * <p>The closeQuietly methods are expected to be used when an IOException
  31. * would be meaningless. This is usually when in a catch block for an
  32. * IOException. </p>
  33. * <p>The toString and toByteArray methods all rely on CopyUtils.copy
  34. * methods in the current implementation. </p>
  35. *
  36. * <p>Origin of code: Apache Avalon (Excalibur)</p>
  37. *
  38. * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  39. * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
  40. * @version CVS $Revision: 1.14 $ $Date: 2004/04/24 23:49:25 $
  41. */
  42. public final class IOUtils
  43. {
  44. private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
  45. /**
  46. * Instances should NOT be constructed in standard programming.
  47. */
  48. public IOUtils() {}
  49. /**
  50. * Unconditionally close an <code>Reader</code>.
  51. * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
  52. *
  53. * @param input A (possibly null) Reader
  54. */
  55. public static void closeQuietly( Reader input )
  56. {
  57. if( input == null )
  58. {
  59. return;
  60. }
  61. try
  62. {
  63. input.close();
  64. }
  65. catch( IOException ioe )
  66. {
  67. }
  68. }
  69. /**
  70. * Unconditionally close an <code>Writer</code>.
  71. * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
  72. *
  73. * @param output A (possibly null) Writer
  74. */
  75. public static void closeQuietly( Writer output )
  76. {
  77. if( output == null )
  78. {
  79. return;
  80. }
  81. try
  82. {
  83. output.close();
  84. }
  85. catch( IOException ioe )
  86. {
  87. }
  88. }
  89. /**
  90. * Unconditionally close an <code>OutputStream</code>.
  91. * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
  92. * @param output A (possibly null) OutputStream
  93. */
  94. public static void closeQuietly( OutputStream output )
  95. {
  96. if( output == null )
  97. {
  98. return;
  99. }
  100. try
  101. {
  102. output.close();
  103. }
  104. catch( IOException ioe )
  105. {
  106. }
  107. }
  108. /**
  109. * Unconditionally close an <code>InputStream</code>.
  110. * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
  111. * @param input A (possibly null) InputStream
  112. */
  113. public static void closeQuietly( InputStream input )
  114. {
  115. if( input == null )
  116. {
  117. return;
  118. }
  119. try
  120. {
  121. input.close();
  122. }
  123. catch( IOException ioe )
  124. {
  125. }
  126. }
  127. /**
  128. * Get the contents of an <code>InputStream</code> as a String.
  129. * The platform's default encoding is used for the byte-to-char conversion.
  130. * @param input the <code>InputStream</code> to read from
  131. * @return the requested <code>String</code>
  132. * @throws IOException In case of an I/O problem
  133. */
  134. public static String toString( InputStream input )
  135. throws IOException
  136. {
  137. StringWriter sw = new StringWriter();
  138. CopyUtils.copy( input, sw );
  139. return sw.toString();
  140. }
  141. /**
  142. * Get the contents of an <code>InputStream</code> as a String.
  143. * @param input the <code>InputStream</code> to read from
  144. * @param encoding The name of a supported character encoding. See the
  145. * <a href="http://www.iana.org/assignments/character-sets">IANA
  146. * Charset Registry</a> for a list of valid encoding types.
  147. * @return the requested <code>String</code>
  148. * @throws IOException In case of an I/O problem
  149. */
  150. public static String toString( InputStream input,
  151. String encoding )
  152. throws IOException
  153. {
  154. StringWriter sw = new StringWriter();
  155. CopyUtils.copy( input, sw, encoding );
  156. return sw.toString();
  157. }
  158. ///////////////////////////////////////////////////////////////
  159. // InputStream -> byte[]
  160. /**
  161. * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
  162. * @param input the <code>InputStream</code> to read from
  163. * @return the requested byte array
  164. * @throws IOException In case of an I/O problem
  165. */
  166. public static byte[] toByteArray( InputStream input )
  167. throws IOException
  168. {
  169. ByteArrayOutputStream output = new ByteArrayOutputStream();
  170. CopyUtils.copy( input, output );
  171. return output.toByteArray();
  172. }
  173. ///////////////////////////////////////////////////////////////
  174. // Derived copy methods
  175. // Reader -> *
  176. ///////////////////////////////////////////////////////////////
  177. ///////////////////////////////////////////////////////////////
  178. // Reader -> String
  179. /**
  180. * Get the contents of a <code>Reader</code> as a String.
  181. * @param input the <code>Reader</code> to read from
  182. * @return the requested <code>String</code>
  183. * @throws IOException In case of an I/O problem
  184. */
  185. public static String toString( Reader input )
  186. throws IOException
  187. {
  188. StringWriter sw = new StringWriter();
  189. CopyUtils.copy( input, sw );
  190. return sw.toString();
  191. }
  192. ///////////////////////////////////////////////////////////////
  193. // Reader -> byte[]
  194. /**
  195. * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
  196. * @param input the <code>Reader</code> to read from
  197. * @return the requested byte array
  198. * @throws IOException In case of an I/O problem
  199. */
  200. public static byte[] toByteArray( Reader input )
  201. throws IOException
  202. {
  203. ByteArrayOutputStream output = new ByteArrayOutputStream();
  204. CopyUtils.copy( input, output );
  205. return output.toByteArray();
  206. }
  207. ///////////////////////////////////////////////////////////////
  208. // Derived copy methods
  209. // String -> *
  210. ///////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////
  212. // String -> byte[]
  213. /**
  214. * Get the contents of a <code>String</code> as a <code>byte[]</code>.
  215. * @param input the <code>String</code> to convert
  216. * @return the requested byte array
  217. * @throws IOException In case of an I/O problem
  218. */
  219. public static byte[] toByteArray( String input )
  220. throws IOException
  221. {
  222. ByteArrayOutputStream output = new ByteArrayOutputStream();
  223. CopyUtils.copy( input, output );
  224. return output.toByteArray();
  225. }
  226. ///////////////////////////////////////////////////////////////
  227. // Derived copy methods
  228. // byte[] -> *
  229. ///////////////////////////////////////////////////////////////
  230. ///////////////////////////////////////////////////////////////
  231. // byte[] -> String
  232. /**
  233. * Get the contents of a <code>byte[]</code> as a String.
  234. * The platform's default encoding is used for the byte-to-char conversion.
  235. * @param input the byte array to read from
  236. * @return the requested <code>String</code>
  237. * @throws IOException In case of an I/O problem
  238. */
  239. public static String toString( byte[] input )
  240. throws IOException
  241. {
  242. StringWriter sw = new StringWriter();
  243. CopyUtils.copy( input, sw );
  244. return sw.toString();
  245. }
  246. /**
  247. * Get the contents of a <code>byte[]</code> as a String.
  248. * @param input the byte array to read from
  249. * @param encoding The name of a supported character encoding. See the
  250. * <a href="http://www.iana.org/assignments/character-sets">IANA
  251. * Charset Registry</a> for a list of valid encoding types.
  252. * @return the requested <code>String</code>
  253. * @throws IOException In case of an I/O problem
  254. */
  255. public static String toString( byte[] input,
  256. String encoding )
  257. throws IOException
  258. {
  259. StringWriter sw = new StringWriter();
  260. CopyUtils.copy( input, sw, encoding );
  261. return sw.toString();
  262. }
  263. /**
  264. * Compare the contents of two Streams to determine if they are equal or not.
  265. *
  266. * @param input1 the first stream
  267. * @param input2 the second stream
  268. * @return true if the content of the streams are equal or they both don't exist, false otherwise
  269. * @throws IOException In case of an I/O problem
  270. */
  271. public static boolean contentEquals( InputStream input1,
  272. InputStream input2 )
  273. throws IOException
  274. {
  275. InputStream bufferedInput1 = new BufferedInputStream( input1 );
  276. InputStream bufferedInput2 = new BufferedInputStream( input2 );
  277. int ch = bufferedInput1.read();
  278. while( -1 != ch )
  279. {
  280. int ch2 = bufferedInput2.read();
  281. if( ch != ch2 )
  282. {
  283. return false;
  284. }
  285. ch = bufferedInput1.read();
  286. }
  287. int ch2 = bufferedInput2.read();
  288. if( -1 != ch2 )
  289. {
  290. return false;
  291. }
  292. else
  293. {
  294. return true;
  295. }
  296. }
  297. }