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
15
16 public final class FileContentWorker {
17
18
19
20 private FileContentWorker() {
21
22
23
24 }
25
26
27
28
29
30
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
50
51
52
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
72
73
74
75
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
93
94
95
96
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
115
116
117
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
145
146
147
148
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
180
181
182
183
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
210
211
212
213
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 }