View Javadoc
1   package auxtestlib;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.FileOutputStream;
7   import java.io.FileReader;
8   import java.io.FileWriter;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.io.InputStreamReader;
12  
13  /**
14   * Utility class with methods to help dealing with text files.
15   */
16  public final class FileContentWorker {
17  	/**
18  	 * Utility class: no constructor.
19  	 */
20  	private FileContentWorker() {
21  		/*
22  		 * Nothing to do.
23  		 */
24  	}
25  	
26  	/**
27  	 * Reads a file's contents.
28  	 * @param file the file
29  	 * @return a string with the whole file's contents
30  	 * @throws IOException failed to read the file
31  	 */
32  	public static String read_contents(File file) throws IOException {
33  		if (file == null) {
34  			throw new IllegalArgumentException("file == null");
35  		}
36  
37  		StringBuffer sb = new StringBuffer();
38  		try (FileReader fr = new FileReader(file)) {
39  			int ch;
40  			while ((ch = fr.read()) != -1) {
41  				sb.append((char) ch);
42  			}
43  		}
44  
45  		return sb.toString();
46  	}
47  
48  	/**
49  	 * Obtains the contents of a file as a binary array.
50  	 * @param binFile the file
51  	 * @return an array with the file's contents
52  	 * @throws IOException failed to read the file
53  	 */
54  	public static byte[] read_contents_bin(File binFile) throws IOException {
55  		if (binFile == null) {
56  			throw new IllegalArgumentException("binFile == null");
57  		}
58  
59  		ByteArrayOutputStream os = new ByteArrayOutputStream();
60  		try (FileInputStream fis = new FileInputStream(binFile)) {
61  			int ch;
62  			while ((ch = fis.read()) != -1) {
63  				os.write(ch);
64  			}
65  		}
66  
67  		return os.toByteArray();
68  	}
69  	
70  	/**
71  	 * Changes the contents of a file (or creates a new file if it doesn't
72  	 * exist).
73  	 * @param f the file to change
74  	 * @param s the file's new contents
75  	 * @throws IOException failed to write the file
76  	 */
77  	public static void set_contents(File f, String s) throws IOException {
78  		if (f == null) {
79  			throw new IllegalArgumentException("f == null");
80  		}
81  
82  		if (s == null) {
83  			throw new IllegalArgumentException("s == null");
84  		}
85  
86  		try (FileWriter fw = new FileWriter(f)) {
87  			fw.write(s);
88  		}
89  	}
90  	
91  	/**
92  	 * Changes the contents of a file (or creates a new file if it doesn't
93  	 * exist).
94  	 * @param f the file to change
95  	 * @param data the new file's contents
96  	 * @throws IOException failed to write the file
97  	 */
98  	public static void set_contents_bin(File f, byte data[])
99  			throws IOException {
100 		if (f == null) {
101 			throw new IllegalArgumentException("f == null");
102 		}
103 
104 		if (data == null) {
105 			throw new IllegalArgumentException("data == null");
106 		}
107 
108 		try (FileOutputStream fo = new FileOutputStream(f)) {
109 			fo.write(data);
110 		}
111 	}
112 	
113 	/**
114 	 * Reads a file's contents (the file is expected to be a resource).
115 	 * @param name the resource name
116 	 * @return the resource's contents as a string
117 	 * @throws IOException failed to read the resource
118 	 */
119 	public static String read_resource(String name) throws IOException {
120 		if (name == null) {
121 			throw new IllegalArgumentException("name == null");
122 		}
123 
124 		StringBuffer sb = new StringBuffer();
125 		try (InputStream is =
126 				FileContentWorker.class.getResourceAsStream(name)) {
127 			if (is == null) {
128 				throw new IllegalArgumentException("Resource '" + name + "' "
129 						+ "is not available.");
130 			}
131 	
132 			try (InputStreamReader isr = new InputStreamReader(is)) {
133 				int ch;
134 				while ((ch = isr.read()) != -1) {
135 					sb.append((char) ch);
136 				}
137 			}
138 		}
139 		
140 		return sb.toString();
141 	}
142 
143 	/**
144 	 * Reads a file's contents (the file is expected to be a resource).
145 	 * @param cls a class defining the package where the resource is located
146 	 * @param name the resource name
147 	 * @return the resource's contents as a string
148 	 * @throws IOException failed to read the resource
149 	 */
150 	public static String read_resource(Class<?> cls, String name)
151 			throws IOException {
152 		if (cls == null) {
153 			throw new IllegalArgumentException("cls == null");
154 		}
155 		
156 		if (name == null) {
157 			throw new IllegalArgumentException("name == null");
158 		}
159 
160 		StringBuffer sb = new StringBuffer();
161 		try (InputStream is = cls.getResourceAsStream(name)) {
162 			if (is == null) {
163 				throw new IllegalArgumentException("Resource '" + name + "' "
164 						+ "is not available.");
165 			}
166 	
167 			try (InputStreamReader isr = new InputStreamReader(is)) {
168 				int ch;
169 				while ((ch = isr.read()) != -1) {
170 					sb.append((char) ch);
171 				}
172 			}
173 		}
174 		
175 		return sb.toString();
176 	}
177 	
178 	/**
179 	 * Reads a file's contents as binary data (the file is expected to be a
180 	 * resource).
181 	 * @param name the resource name
182 	 * @return the resource's contents
183 	 * @throws IOException failed to read the resource
184 	 */
185 	public static byte[] read_resource_bin(String name)
186 			throws IOException {
187 		if (name == null) {
188 			throw new IllegalArgumentException("name == null");
189 		}
190 
191 		ByteArrayOutputStream os = new ByteArrayOutputStream();
192 		try (InputStream is =
193 				FileContentWorker.class.getResourceAsStream(name)) {
194 			if (is == null) {
195 				throw new IllegalArgumentException("Resource '" + name + "' is "
196 						+ "not available.");
197 			}
198 	
199 			int ch;
200 			while ((ch = is.read()) != -1) {
201 				os.write(ch);
202 			}
203 		}
204 		
205 		return os.toByteArray();
206 	}
207 	
208 	/**
209 	 * Checks that two files are equal.
210 	 * @param f1 first file
211 	 * @param f2 second file
212 	 * @return are the files equal?
213 	 * @throws IOException failed to read the files
214 	 */
215 	public static boolean files_equal(File f1, File f2) throws IOException {
216 		if (f1 == null) {
217 			throw new IllegalArgumentException("f1 == null");
218 		}
219 
220 		if (f2 == null) {
221 			throw new IllegalArgumentException("f2 == null");
222 		}
223 
224 		byte dt1[] = FileContentWorker.read_contents_bin(f1);
225 		byte dt2[] = FileContentWorker.read_contents_bin(f2);
226 		return ComparisonUtils.array_equals(dt1, dt2);
227 	}
228 }