83dcd502b242fb26d88b611b1d7ff4e7d1a30c17
1 package be
.nikiroo
.utils
;
3 import java
.io
.BufferedReader
;
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
;
16 * This class offer some utilities based around Streams.
20 public class IOUtils
{
22 * Write the data to the given {@link File}.
27 * the target {@link File}
30 * in case of I/O error
32 public static void write(InputStream in
, File target
) throws IOException
{
33 OutputStream out
= new FileOutputStream(target
);
42 * Write the data to the given {@link OutputStream}.
47 * the target {@link OutputStream}
50 * in case of I/O error
52 public static void write(InputStream in
, OutputStream out
)
54 byte buffer
[] = new byte[4069];
55 for (int len
= 0; (len
= in
.read(buffer
)) > 0;) {
56 out
.write(buffer
, 0, len
);
61 * Recursively Add a {@link File} (which can thus be a directory, too) to a
62 * {@link ZipOutputStream}.
67 * the path to prepend to the ZIP info before the actual
70 * the source {@link File} (which can be a directory)
72 * FALSE if we need to add a {@link ZipEntry} for base/target,
73 * TRUE to add it at the root of the ZIP
76 * in case of I/O error
78 public static void zip(ZipOutputStream zip
, String base
, File target
,
79 boolean targetIsRoot
) throws IOException
{
80 if (target
.isDirectory()) {
82 if (base
== null || base
.isEmpty()) {
83 base
= target
.getName();
85 base
+= "/" + target
.getName();
87 zip
.putNextEntry(new ZipEntry(base
+ "/"));
89 for (File file
: target
.listFiles()) {
90 zip(zip
, base
, file
, false);
93 if (base
== null || base
.isEmpty()) {
94 base
= target
.getName();
96 base
+= "/" + target
.getName();
98 zip
.putNextEntry(new ZipEntry(base
));
99 FileInputStream in
= new FileInputStream(target
);
101 IOUtils
.write(in
, zip
);
109 * Zip the given source into dest.
112 * the source {@link File} (which can be a directory)
114 * the destination <tt>.zip</tt> file
116 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
117 * add it at the root of the ZIP
119 * @throws IOException
120 * in case of I/O error
122 public static void zip(File src
, File dest
, boolean srcIsRoot
)
124 OutputStream out
= new FileOutputStream(dest
);
126 ZipOutputStream zip
= new ZipOutputStream(out
);
128 IOUtils
.zip(zip
, "", src
, srcIsRoot
);
138 * Write the {@link String} content to {@link File}.
141 * the directory where to write the {@link File}
143 * the {@link File} name
147 * @throws IOException
148 * in case of I/O error
150 public static void writeSmallFile(File dir
, String filename
, String content
)
156 FileWriter writerVersion
= new FileWriter(new File(dir
, filename
));
158 writerVersion
.write(content
);
160 writerVersion
.close();
165 * Read the whole {@link File} content into a {@link String}.
170 * @return the content
172 * @throws IOException
173 * in case of I/O error
175 public static String
readSmallFile(File file
) throws IOException
{
176 BufferedReader reader
= new BufferedReader(new FileReader(file
));
178 StringBuilder builder
= new StringBuilder();
179 for (String line
= reader
.readLine(); line
!= null; line
= reader
181 builder
.append(line
);
183 return builder
.toString();
190 * Recursively delete the given {@link File}, which may of course also be a
193 * Will silently continue in case of error.
196 * the target to delete
198 public static void deltree(File target
) {
199 File
[] files
= target
.listFiles();
201 for (File file
: files
) {
206 if (!target
.delete()) {
207 System
.err
.println("Cannot delete: " + target
.getAbsolutePath());
212 * Open the given /-separated resource (from the binary root).
217 * @return the opened resource if found, NLL if not
219 public static InputStream
openResource(String name
) {
220 ClassLoader loader
= IOUtils
.class.getClassLoader();
221 if (loader
== null) {
222 loader
= ClassLoader
.getSystemClassLoader();
225 return loader
.getResourceAsStream(name
);