Version 2.0.0 (small API change)
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
1 package be.nikiroo.utils;
2
3 import java.io.BufferedReader;
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.OutputStream;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipOutputStream;
14
15 /**
16 * This class offer some utilities based around Streams.
17 *
18 * @author niki
19 */
20 public 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 BufferedReader reader = new BufferedReader(new FileReader(file));
177 try {
178 StringBuilder builder = new StringBuilder();
179 for (String line = reader.readLine(); line != null; line = reader
180 .readLine()) {
181 builder.append(line);
182 }
183 return builder.toString();
184 } finally {
185 reader.close();
186 }
187 }
188
189 /**
190 * Recursively delete the given {@link File}, which may of course also be a
191 * directory.
192 * <p>
193 * Will silently continue in case of error.
194 *
195 * @param target
196 * the target to delete
197 */
198 public static void deltree(File target) {
199 File[] files = target.listFiles();
200 if (files != null) {
201 for (File file : files) {
202 deltree(file);
203 }
204 }
205
206 if (!target.delete()) {
207 System.err.println("Cannot delete: " + target.getAbsolutePath());
208 }
209 }
210
211 /**
212 * Open the given /-separated resource (from the binary root).
213 *
214 * @param name
215 * the resource name
216 *
217 * @return the opened resource if found, NLL if not
218 */
219 public static InputStream openResource(String name) {
220 ClassLoader loader = IOUtils.class.getClassLoader();
221 if (loader == null) {
222 loader = ClassLoader.getSystemClassLoader();
223 }
224
225 return loader.getResourceAsStream(name);
226 }
227 }