View Javadoc

1   /***
2    * Copyright 2007 the original author or authors.
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   * For more information visit
11   *         http://72miles.com and
12   *         http://architecturerules.googlecode.com/svn/docs/index.html
13   */
14  
15  package org.seventytwomiles.springframework.io;
16  
17  
18  import java.io.*;
19  
20  
21  
22  /***
23   * <p>FileUtils utility class extracted from the Spring Framework in order to
24   * remove the dependency on Spring for this one class.  <a
25   * href="http://code.google.com/p/architecturerules/issues/detail?id=2&can=1">
26   * issue 2 (remove unnecessary dependencies)</a></p>
27   *
28   * @author <a href="mailto:burton@relativity.yi.org">Kevin A. Burton</A>
29   * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
30   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31   * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
32   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
33   * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
34   * @author Matthew Hawthorne
35   * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
36   * @author Stephen Colebourne
37   * @author Ian Springer
38   * @author Chris Eldredge
39   * @author Jim Harrington
40   * @author Niall Pemberton
41   * @author Sandy McArthur
42   */
43  public class FileUtils {
44  
45  
46      /***
47       * <p>The default buffer size to use.</p>
48       *
49       * @parameter DEFAULT_BUFFER_SIZE int
50       */
51      private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
52  
53  
54      /***
55       * <p> Reads the contents of a file into a String. There is no
56       * readFileToString method without encoding parameter because the default
57       * encoding can differ between platforms and therefore results in
58       * inconsistent results. </p>
59       *
60       * @param file the file to read
61       * @param encoding the encoding to use, null means platform default
62       * @return The file contents or null if read failed.
63       * @throws IOException in case of an I/O error
64       * @throws UnsupportedEncodingException if the encoding is not supported by
65       * the VM
66       */
67      public static String readFileToString(final File file,
68                                            final String encoding)
69              throws IOException {
70  
71          final InputStream inputStream = new FileInputStream(file);
72  
73          try {
74  
75              return toString(inputStream, encoding);
76  
77          } finally {
78  
79              closeQuietly(inputStream);
80          }
81      }
82  
83  
84      /***
85       * <p>Get the contents of an <code>InputStream</code> as a String using the
86       * specified character encoding. <p> Character encoding names can be found
87       * at <a href="http://www.iana.org/assignments/character-sets">IANA</a>. <p>
88       * This method buffers the input internally, so there is no need to use a
89       * <code>BufferedInputStream</code>.</p>
90       *
91       * @param input the <code>InputStream</code> to read from
92       * @param encoding the encoding to use, null means platform default
93       * @return the requested String
94       * @throws NullPointerException if the input is null
95       * @throws IOException if an I/O error occurs
96       */
97      public static String toString(final InputStream input,
98                                    final String encoding) throws IOException {
99  
100         final StringWriter stringWriter = new StringWriter();
101 
102         copy(input, stringWriter, encoding);
103 
104         return stringWriter.toString();
105     }
106 
107 
108     /***
109      * <p>Copy bytes from an <code>InputStream</code> to chars on a
110      * <code>Writer</code> using the default character encoding of the platform.
111      * <p> This method buffers the input internally, so there is no need to use
112      * a <code>BufferedInputStream</code>. <p> This method uses {@link
113      * InputStreamReader}.</p>
114      *
115      * @param input the <code>InputStream</code> to read from
116      * @param output the <code>Writer</code> to write to
117      * @throws NullPointerException if the input or output is null
118      * @throws IOException if an I/O error occurs
119      * @since Commons IO 1.1
120      */
121     public static void copy(final InputStream input, final Writer output)
122             throws IOException {
123 
124         final InputStreamReader inputStreamReader = new InputStreamReader(
125                 input);
126         copy(inputStreamReader, output);
127     }
128 
129 
130     /***
131      * <p>Copy bytes from an <code>InputStream</code> to chars on a
132      * <code>Writer</code> using the specified character encoding. <p> This
133      * method buffers the inputStream internally, so there is no need to use a
134      * <code>BufferedInputStream</code>. <p> Character encoding names can be
135      * found at <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
136      * <p> This method uses {@link InputStreamReader}.</p>
137      *
138      * @param inputStream the <code>InputStream</code> to read from
139      * @param outputStream the <code>Writer</code> to write to
140      * @param encoding the encoding to use, null means platform default
141      * @throws NullPointerException if the inputStream or outputStream is null
142      * @throws IOException if an I/O error occurs
143      * @since Commons IO 1.1
144      */
145     public static void copy(final InputStream inputStream,
146                             final Writer outputStream, final String encoding)
147             throws IOException {
148 
149         if (encoding == null) {
150 
151             copy(inputStream, outputStream);
152 
153         } else {
154 
155             final InputStreamReader inputStreamReader = new InputStreamReader(
156                     inputStream, encoding);
157             copy(inputStreamReader, outputStream);
158         }
159     }
160 
161 
162     /***
163      * <p>Copy chars from a <code>Reader</code> to a <code>Writer</code>. <p>
164      * This method buffers the input internally, so there is no need to use a
165      * <code>BufferedReader</code>.</p>
166      *
167      * @param input the <code>Reader</code> to read from
168      * @param output the <code>Writer</code> to write to
169      * @return the number of characters copied
170      * @throws NullPointerException if the input or output is null
171      * @throws IOException if an I/O error occurs
172      * @since Commons IO 1.1
173      */
174     public static int copy(final Reader input, final Writer output)
175             throws IOException {
176 
177         final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
178 
179         int count = 0;
180         int n;
181 
182         while (-1 != (n = input.read(buffer))) {
183 
184             output.write(buffer, 0, n);
185             count += n;
186         }
187 
188         return count;
189     }
190 
191 
192     /***
193      * <code>Copy chars from a <code>Reader</code> to bytes on an
194      * <code>OutputStream</code> using the default character encoding of the
195      * platform, and calling flush. <p> This method buffers the input
196      * internally, so there is no need to use a <code>BufferedReader</code>. <p>
197      * Due to the implementation of OutputStreamWriter, this method performs a
198      * flush. <p> This method uses {@link OutputStreamWriter}.</code>
199      *
200      * @param input the <code>Reader</code> to read from
201      * @param output the <code>OutputStream</code> to write to
202      * @throws NullPointerException if the input or output is null
203      * @throws IOException if an I/O error occurs
204      * @since Commons IO 1.1
205      */
206     public static void copy(final Reader input, final OutputStream output)
207             throws IOException {
208 
209         final OutputStreamWriter out = new OutputStreamWriter(output);
210 
211         copy(input, out);
212         out.flush();
213     }
214 
215 
216     /***
217      * <code>Unconditionally close an <code>InputStream</code>. <p> Equivalent
218      * to {@link InputStream#close()}, except any exceptions will be ignored.
219      * This is typically used in finally blocks.</code>
220      *
221      * @param input the InputStream to close, may be null or already closed
222      */
223     public static void closeQuietly(final InputStream input) {
224 
225         try {
226 
227             if (input != null)
228                 input.close();
229 
230         } catch (IOException ioe) {
231 
232             // ignore
233         }
234     }
235 }