fix changelog headers
[fanfix.git] / IOUtils.java
... / ...
CommitLineData
1package be.nikiroo.utils;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileOutputStream;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.OutputStream;
12import java.util.zip.ZipEntry;
13import java.util.zip.ZipOutputStream;
14
15/**
16 * This class offer some utilities based around Streams.
17 *
18 * @author niki
19 */
20public class IOUtils {
21 /**
22 * Write the data to the given {@link File}.
23 *
24 * @param in
25 * the data source
26 * @param target
27 * the target {@link File}
28 *
29 * @throws IOException
30 * in case of I/O error
31 */
32 public static void write(InputStream in, File target) throws IOException {
33 OutputStream out = new FileOutputStream(target);
34 try {
35 write(in, out);
36 } finally {
37 out.close();
38 }
39 }
40
41 /**
42 * Write the data to the given {@link OutputStream}.
43 *
44 * @param in
45 * the data source
46 * @param out
47 * the target {@link OutputStream}
48 *
49 * @throws IOException
50 * in case of I/O error
51 */
52 public static void write(InputStream in, OutputStream out)
53 throws IOException {
54 byte buffer[] = new byte[4069];
55 for (int len = 0; (len = in.read(buffer)) > 0;) {
56 out.write(buffer, 0, len);
57 }
58 }
59
60 /**
61 * Recursively Add a {@link File} (which can thus be a directory, too) to a
62 * {@link ZipOutputStream}.
63 *
64 * @param zip
65 * the stream
66 * @param base
67 * the path to prepend to the ZIP info before the actual
68 * {@link File} path
69 * @param target
70 * the source {@link File} (which can be a directory)
71 * @param targetIsRoot
72 * FALSE if we need to add a {@link ZipEntry} for base/target,
73 * TRUE to add it at the root of the ZIP
74 *
75 * @throws IOException
76 * in case of I/O error
77 */
78 public static void zip(ZipOutputStream zip, String base, File target,
79 boolean targetIsRoot) throws IOException {
80 if (target.isDirectory()) {
81 if (!targetIsRoot) {
82 if (base == null || base.isEmpty()) {
83 base = target.getName();
84 } else {
85 base += "/" + target.getName();
86 }
87 zip.putNextEntry(new ZipEntry(base + "/"));
88 }
89 for (File file : target.listFiles()) {
90 zip(zip, base, file, false);
91 }
92 } else {
93 if (base == null || base.isEmpty()) {
94 base = target.getName();
95 } else {
96 base += "/" + target.getName();
97 }
98 zip.putNextEntry(new ZipEntry(base));
99 FileInputStream in = new FileInputStream(target);
100 try {
101 IOUtils.write(in, zip);
102 } finally {
103 in.close();
104 }
105 }
106 }
107
108 /**
109 * Zip the given source into dest.
110 *
111 * @param src
112 * the source {@link File} (which can be a directory)
113 * @param dest
114 * the destination <tt>.zip</tt> file
115 * @param srcIsRoot
116 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
117 * add it at the root of the ZIP
118 *
119 * @throws IOException
120 * in case of I/O error
121 */
122 public static void zip(File src, File dest, boolean srcIsRoot)
123 throws IOException {
124 OutputStream out = new FileOutputStream(dest);
125 try {
126 ZipOutputStream zip = new ZipOutputStream(out);
127 try {
128 IOUtils.zip(zip, "", src, srcIsRoot);
129 } finally {
130 zip.close();
131 }
132 } finally {
133 out.close();
134 }
135 }
136
137 /**
138 * Write the {@link String} content to {@link File}.
139 *
140 * @param dir
141 * the directory where to write the {@link File}
142 * @param filename
143 * the {@link File} name
144 * @param content
145 * the content
146 *
147 * @throws IOException
148 * in case of I/O error
149 */
150 public static void writeSmallFile(File dir, String filename, String content)
151 throws IOException {
152 if (!dir.exists()) {
153 dir.mkdirs();
154 }
155
156 FileWriter writerVersion = new FileWriter(new File(dir, filename));
157 try {
158 writerVersion.write(content);
159 } finally {
160 writerVersion.close();
161 }
162 }
163
164 /**
165 * Read the whole {@link File} content into a {@link String}.
166 *
167 * @param file
168 * the {@link File}
169 *
170 * @return the content
171 *
172 * @throws IOException
173 * in case of I/O error
174 */
175 public static String readSmallFile(File file) throws IOException {
176 InputStream stream = new FileInputStream(file);
177 try {
178 return readSmallStream(stream);
179 } finally {
180 stream.close();
181 }
182 }
183
184 /**
185 * Read the whole {@link InputStream} content into a {@link String}.
186 *
187 * @param stream
188 * the {@link InputStream}
189 *
190 * @return the content
191 *
192 * @throws IOException
193 * in case of I/O error
194 */
195 public static String readSmallStream(InputStream stream) throws IOException {
196 // do NOT close the reader, or the related stream will be closed, too
197 // reader.close();
198 BufferedReader reader = new BufferedReader(
199 new InputStreamReader(stream));
200
201 StringBuilder builder = new StringBuilder();
202 for (String line = reader.readLine(); line != null; line = reader
203 .readLine()) {
204 builder.append(line);
205 builder.append("\n");
206 }
207
208 return builder.toString();
209 }
210
211 /**
212 * Recursively delete the given {@link File}, which may of course also be a
213 * directory.
214 * <p>
215 * Will silently continue in case of error.
216 *
217 * @param target
218 * the target to delete
219 */
220 public static void deltree(File target) {
221 File[] files = target.listFiles();
222 if (files != null) {
223 for (File file : files) {
224 deltree(file);
225 }
226 }
227
228 if (!target.delete()) {
229 System.err.println("Cannot delete: " + target.getAbsolutePath());
230 }
231 }
232
233 /**
234 * Open the given /-separated resource (from the binary root).
235 *
236 * @param name
237 * the resource name
238 *
239 * @return the opened resource if found, NLL if not
240 */
241 public static InputStream openResource(String name) {
242 ClassLoader loader = IOUtils.class.getClassLoader();
243 if (loader == null) {
244 loader = ClassLoader.getSystemClassLoader();
245 }
246
247 return loader.getResourceAsStream(name);
248 }
249}