source: devel/testlink/automation2.0/src/test/java/org/expressolivre/cte/common/IOUtil.java @ 4780

Revision 4780, 4.2 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1771 - Refactoring, criadas novas classes para Pasta, Filtro, Pesquisa

Line 
1package org.expressolivre.cte.common;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.io.Reader;
11import java.net.URL;
12
13/**
14 * @author L.F.Estivalet (Serpro)
15 *
16 *         Created on May 9, 2011 at 2:43:28 PM
17 *
18 */
19public class IOUtil {
20
21        /**
22         * Open and write contents to a file.
23         *
24         * @param fileName
25         *            the name of the file.
26         * @param contents
27         *            contents to be written.
28         * @param append
29         *            if <code>true</code> contents will be appended to previous
30         *            contents of the file. If <code>false</code> a brand new file
31         *            is created to write the contents.
32         * @throws IOException
33         *             Thrown whenever a problem in the underlying file system
34         *             happens.
35         */
36        public static void writeFile(String fileName, String contents,
37                        Boolean append) throws IOException {
38                BufferedWriter out = new BufferedWriter(
39                                new FileWriter(fileName, append));
40                out.write(contents);
41                out.close();
42        }
43
44        /**
45         * Read the entire contents of the specified file and return a single String
46         * object containing the contents of the file.
47         *
48         * @param fileName
49         *            the name of the file from which to read
50         *
51         * @return a String containing the contents of the input file
52         *
53         * @throws IOException
54         *             if the specified file cannot be opened, or if an I/O error
55         *             occurs while reading the file
56         */
57        public static String readFully(String fileName) throws IOException {
58                return readFully(new File(fileName));
59        }
60
61        /**
62         * Read the entire contents of the specified file and return a single String
63         * object containing the contents of the file. This method does not return
64         * until the end of the input file is reached.
65         *
66         * @param file
67         *            a File from which to read
68         *
69         * @return a String containing the contents of the input file
70         *
71         * @throws IOException
72         *             if the specified file cannot be opened, or if an I/O error
73         *             occurs while reading the file
74         */
75        public static String readFully(File file) throws IOException {
76                return readFully(new FileReader(file));
77        }
78
79        /**
80         * Read the entire contents of the specified URL and return a single String
81         * object containing the contents of the URL. This method does not return
82         * until an end of stream is reached for the URL.
83         *
84         * @param url
85         *            a URL from which to read
86         *
87         * @return a String containing the contents of the input URL
88         *
89         * @throws IOException
90         *             if the specified URL cannot be opened, or if an I/O error
91         *             occurs while reading the URL
92         */
93        public static String readFully(URL url) throws IOException {
94                return readFully(url.openStream());
95        }
96
97        /**
98         * Read the entire contents of the specified InputStream and return a single
99         * String object containing the contents of the InputStream. This method
100         * does not return until the end of the input stream is reached.
101         *
102         * @param stream
103         *            an InputStream from which to read
104         *
105         * @return a String containing the contents of the input stream
106         *
107         * @throws IOException
108         *             if an I/O error occurs while reading the input stream
109         */
110        public static String readFully(InputStream stream) throws IOException {
111                return readFully(new InputStreamReader(stream));
112        }
113
114        /**
115         * Read the entire contents of the specified Reader and return a single
116         * String object containing the contents of the InputStream. This method
117         * does not return until the end of the input file or stream is reached.
118         *
119         * @param reader
120         *            a Reader from which to read
121         *
122         * @return a String containing the contents of the stream
123         *
124         * @throws IOException
125         *             if an I/O error occurs while reading the input stream
126         */
127        public static String readFully(Reader reader) throws IOException {
128                char[] arr = new char[8 * 1024]; // 8K at a time
129                StringBuffer buf = new StringBuffer();
130                int numChars;
131
132                while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
133                        buf.append(arr, 0, numChars);
134                }
135
136                return buf.toString();
137        }
138
139        /**
140         * Do not construct an instance of this class.
141         */
142        private IOUtil() {
143        }
144}
Note: See TracBrowser for help on using the repository browser.