1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileOutputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
10 import java
.util
.ArrayList
;
11 import java
.util
.List
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
14 import java
.util
.zip
.ZipOutputStream
;
17 * This class offer some utilities based around Streams and Files.
21 public class IOUtils
{
23 * Write the data to the given {@link File}.
28 * the target {@link File}
31 * in case of I/O error
33 public static void write(InputStream in
, File target
) throws IOException
{
34 OutputStream out
= new FileOutputStream(target
);
43 * Write the data to the given {@link OutputStream}.
48 * the target {@link OutputStream}
51 * in case of I/O error
53 public static void write(InputStream in
, OutputStream out
)
55 byte buffer
[] = new byte[4096];
56 int len
= in
.read(buffer
);
58 out
.write(buffer
, 0, len
);
59 len
= in
.read(buffer
);
64 * Recursively Add a {@link File} (which can thus be a directory, too) to a
65 * {@link ZipOutputStream}.
70 * the path to prepend to the ZIP info before the actual
73 * the source {@link File} (which can be a directory)
75 * FALSE if we need to add a {@link ZipEntry} for base/target,
76 * TRUE to add it at the root of the ZIP
79 * in case of I/O error
81 public static void zip(ZipOutputStream zip
, String base
, File target
,
82 boolean targetIsRoot
) throws IOException
{
83 if (target
.isDirectory()) {
85 if (base
== null || base
.isEmpty()) {
86 base
= target
.getName();
88 base
+= "/" + target
.getName();
90 zip
.putNextEntry(new ZipEntry(base
+ "/"));
93 File
[] files
= target
.listFiles();
95 for (File file
: files
) {
96 zip(zip
, base
, file
, false);
100 if (base
== null || base
.isEmpty()) {
101 base
= target
.getName();
103 base
+= "/" + target
.getName();
105 zip
.putNextEntry(new ZipEntry(base
));
106 FileInputStream in
= new FileInputStream(target
);
108 IOUtils
.write(in
, zip
);
116 * Zip the given source into dest.
119 * the source {@link File} (which can be a directory)
121 * the destination <tt>.zip</tt> file
123 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
124 * add it at the root of the ZIP
126 * @throws IOException
127 * in case of I/O error
129 public static void zip(File src
, File dest
, boolean srcIsRoot
)
131 OutputStream out
= new FileOutputStream(dest
);
133 ZipOutputStream zip
= new ZipOutputStream(out
);
135 IOUtils
.zip(zip
, "", src
, srcIsRoot
);
145 * Unzip the given ZIP file into the target directory.
149 * @param targetDirectory
150 * the target directory
152 * @return the number of extracted files (not directories)
154 * @throws IOException
155 * in case of I/O errors
157 public static long unzip(File zipFile
, File targetDirectory
)
161 if (targetDirectory
.exists() && targetDirectory
.isFile()) {
162 throw new IOException("Cannot unzip " + zipFile
+ " into "
163 + targetDirectory
+ ": it is not a directory");
166 targetDirectory
.mkdir();
167 if (!targetDirectory
.exists()) {
168 throw new IOException("Cannot create target directory "
172 FileInputStream in
= new FileInputStream(zipFile
);
174 ZipInputStream zipStream
= new ZipInputStream(in
);
176 for (ZipEntry entry
= zipStream
.getNextEntry(); entry
!= null; entry
= zipStream
178 File file
= new File(targetDirectory
, entry
.getName());
179 if (entry
.isDirectory()) {
182 IOUtils
.write(zipStream
, file
);
197 * Write the {@link String} content to {@link File}.
200 * the directory where to write the {@link File}
202 * the {@link File} name
206 * @throws IOException
207 * in case of I/O error
209 public static void writeSmallFile(File dir
, String filename
, String content
)
215 writeSmallFile(new File(dir
, filename
), content
);
219 * Write the {@link String} content to {@link File}.
222 * the {@link File} to write
226 * @throws IOException
227 * in case of I/O error
229 public static void writeSmallFile(File file
, String content
)
231 FileOutputStream out
= new FileOutputStream(file
);
233 out
.write(content
.getBytes("UTF-8"));
240 * Read the whole {@link File} content into a {@link String}.
245 * @return the content
247 * @throws IOException
248 * in case of I/O error
250 public static String
readSmallFile(File file
) throws IOException
{
251 InputStream stream
= new FileInputStream(file
);
253 return readSmallStream(stream
);
260 * Read the whole {@link InputStream} content into a {@link String}.
263 * the {@link InputStream}
265 * @return the content
267 * @throws IOException
268 * in case of I/O error
270 public static String
readSmallStream(InputStream stream
) throws IOException
{
271 ByteArrayOutputStream out
= new ByteArrayOutputStream();
274 return out
.toString("UTF-8");
276 // do NOT close, or the related stream will be closed, too
282 * Recursively delete the given {@link File}, which may of course also be a
285 * Will either silently continue or throw an exception in case of error,
286 * depending upon the parameters.
289 * the target to delete
291 * TRUE to throw an {@link IOException} in case of error, FALSE
292 * to silently continue
294 * @return TRUE if all files were deleted, FALSE if an error occurred
296 * @throws IOException
297 * if an error occurred and the parameters allow an exception to
300 public static boolean deltree(File target
, boolean exception
)
302 List
<File
> list
= deltree(target
, null);
303 if (exception
&& !list
.isEmpty()) {
304 StringBuilder slist
= new StringBuilder();
305 for (File file
: list
) {
306 slist
.append("\n").append(file
.getPath());
309 throw new IOException("Cannot delete all the files from: <" //
310 + target
+ ">:" + slist
.toString());
313 return list
.isEmpty();
317 * Recursively delete the given {@link File}, which may of course also be a
320 * Will silently continue in case of error.
323 * the target to delete
325 * @return TRUE if all files were deleted, FALSE if an error occurred
327 public static boolean deltree(File target
) {
328 return deltree(target
, null).isEmpty();
332 * Recursively delete the given {@link File}, which may of course also be a
335 * Will collect all {@link File} that cannot be deleted in the given
339 * the target to delete
341 * the accumulator to use for errors, or NULL to create a new one
343 * @return the errors accumulator
345 public static List
<File
> deltree(File target
, List
<File
> errorAcc
) {
346 if (errorAcc
== null) {
347 errorAcc
= new ArrayList
<File
>();
350 File
[] files
= target
.listFiles();
352 for (File file
: files
) {
353 errorAcc
= deltree(file
, errorAcc
);
357 if (!target
.delete()) {
358 errorAcc
.add(target
);
365 * Open the given /-separated resource (from the binary root).
370 * @return the opened resource if found, NLL if not
372 public static InputStream
openResource(String name
) {
373 ClassLoader loader
= IOUtils
.class.getClassLoader();
374 if (loader
== null) {
375 loader
= ClassLoader
.getSystemClassLoader();
378 return loader
.getResourceAsStream(name
);
382 * Return a resetable {@link InputStream} from this stream, and reset it.
386 * @return the resetable stream, which <b>may</b> be the same
388 * @throws IOException
389 * in case of I/O error
391 public static InputStream
forceResetableStream(InputStream in
)
393 boolean resetable
= in
.markSupported();
397 } catch (IOException e
) {
406 final File tmp
= File
.createTempFile(".tmp-stream.", ".tmp");
411 final FileInputStream fis
= new FileInputStream(tmp
);
412 return new MarkableFileInputStream(fis
) {
414 public void close() throws IOException
{
421 } catch (IOException e
) {
429 } catch (IOException e
) {
436 * Convert the {@link InputStream} into a byte array.
443 * @throws IOException
444 * in case of I/O error
446 public static byte[] toByteArray(InputStream in
) throws IOException
{
447 ByteArrayOutputStream out
= new ByteArrayOutputStream();
450 return out
.toByteArray();
457 * Convert the {@link File} into a byte array.
460 * the input {@link File}
464 * @throws IOException
465 * in case of I/O error
467 public static byte[] toByteArray(File file
) throws IOException
{
468 FileInputStream fis
= new FileInputStream(file
);
470 return toByteArray(fis
);