1 | package org.expressolivre.cte.common; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileReader; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStream; |
---|
7 | import java.io.InputStreamReader; |
---|
8 | import java.io.Reader; |
---|
9 | import java.net.URL; |
---|
10 | |
---|
11 | /** |
---|
12 | * @author L.F.Estivalet (Serpro) |
---|
13 | * |
---|
14 | * Created on May 9, 2011 at 2:43:28 PM |
---|
15 | * |
---|
16 | */ |
---|
17 | public class IOUtil { |
---|
18 | |
---|
19 | /** |
---|
20 | * Read the entire contents of the specified file and return a single String |
---|
21 | * object containing the contents of the file. |
---|
22 | * |
---|
23 | * @param fileName |
---|
24 | * the name of the file from which to read |
---|
25 | * |
---|
26 | * @return a String containing the contents of the input file |
---|
27 | * |
---|
28 | * @throws IOException |
---|
29 | * if the specified file cannot be opened, or if an I/O error |
---|
30 | * occurs while reading the file |
---|
31 | */ |
---|
32 | public static String readFully(String fileName) throws IOException { |
---|
33 | return readFully(new File(fileName)); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Read the entire contents of the specified file and return a single String |
---|
38 | * object containing the contents of the file. This method does not return |
---|
39 | * until the end of the input file is reached. |
---|
40 | * |
---|
41 | * @param file |
---|
42 | * a File from which to read |
---|
43 | * |
---|
44 | * @return a String containing the contents of the input file |
---|
45 | * |
---|
46 | * @throws IOException |
---|
47 | * if the specified file cannot be opened, or if an I/O error |
---|
48 | * occurs while reading the file |
---|
49 | */ |
---|
50 | public static String readFully(File file) throws IOException { |
---|
51 | return readFully(new FileReader(file)); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Read the entire contents of the specified URL and return a single String |
---|
56 | * object containing the contents of the URL. This method does not return |
---|
57 | * until an end of stream is reached for the URL. |
---|
58 | * |
---|
59 | * @param url |
---|
60 | * a URL from which to read |
---|
61 | * |
---|
62 | * @return a String containing the contents of the input URL |
---|
63 | * |
---|
64 | * @throws IOException |
---|
65 | * if the specified URL cannot be opened, or if an I/O error |
---|
66 | * occurs while reading the URL |
---|
67 | */ |
---|
68 | public static String readFully(URL url) throws IOException { |
---|
69 | return readFully(url.openStream()); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Read the entire contents of the specified InputStream and return a single |
---|
74 | * String object containing the contents of the InputStream. This method |
---|
75 | * does not return until the end of the input stream is reached. |
---|
76 | * |
---|
77 | * @param stream |
---|
78 | * an InputStream from which to read |
---|
79 | * |
---|
80 | * @return a String containing the contents of the input stream |
---|
81 | * |
---|
82 | * @throws IOException |
---|
83 | * if an I/O error occurs while reading the input stream |
---|
84 | */ |
---|
85 | public static String readFully(InputStream stream) throws IOException { |
---|
86 | return readFully(new InputStreamReader(stream)); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Read the entire contents of the specified Reader and return a single |
---|
91 | * String object containing the contents of the InputStream. This method |
---|
92 | * does not return until the end of the input file or stream is reached. |
---|
93 | * |
---|
94 | * @param reader |
---|
95 | * a Reader from which to read |
---|
96 | * |
---|
97 | * @return a String containing the contents of the stream |
---|
98 | * |
---|
99 | * @throws IOException |
---|
100 | * if an I/O error occurs while reading the input stream |
---|
101 | */ |
---|
102 | public static String readFully(Reader reader) throws IOException { |
---|
103 | char[] arr = new char[8 * 1024]; // 8K at a time |
---|
104 | StringBuffer buf = new StringBuffer(); |
---|
105 | int numChars; |
---|
106 | |
---|
107 | while ((numChars = reader.read(arr, 0, arr.length)) > 0) { |
---|
108 | buf.append(arr, 0, numChars); |
---|
109 | } |
---|
110 | |
---|
111 | return buf.toString(); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Do not construct an instance of this class. |
---|
116 | */ |
---|
117 | private IOUtil() { |
---|
118 | } |
---|
119 | } |
---|