1 package be
.nikiroo
.utils
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayOutputStream
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.FileWriter
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStream
;
11 import java
.io
.InputStreamReader
;
12 import java
.io
.OutputStream
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.zip
.ZipEntry
;
16 import java
.util
.zip
.ZipOutputStream
;
19 * This class offer some utilities based around Streams.
23 public class IOUtils
{
25 * Write the data to the given {@link File}.
30 * the target {@link File}
33 * in case of I/O error
35 public static void write(InputStream in
, File target
) throws IOException
{
36 OutputStream out
= new FileOutputStream(target
);
45 * Write the data to the given {@link OutputStream}.
50 * the target {@link OutputStream}
53 * in case of I/O error
55 public static void write(InputStream in
, OutputStream out
)
57 byte buffer
[] = new byte[4069];
58 for (int len
= 0; (len
= in
.read(buffer
)) > 0;) {
59 out
.write(buffer
, 0, len
);
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 * Write the {@link String} content to {@link File}.
148 * the directory where to write the {@link File}
150 * the {@link File} name
154 * @throws IOException
155 * in case of I/O error
157 public static void writeSmallFile(File dir
, String filename
, String content
)
163 FileWriter writerVersion
= new FileWriter(new File(dir
, filename
));
165 writerVersion
.write(content
);
167 writerVersion
.close();
172 * Read the whole {@link File} content into a {@link String}.
177 * @return the content
179 * @throws IOException
180 * in case of I/O error
182 public static String
readSmallFile(File file
) throws IOException
{
183 InputStream stream
= new FileInputStream(file
);
185 return readSmallStream(stream
);
192 * Read the whole {@link InputStream} content into a {@link String}.
195 * the {@link InputStream}
197 * @return the content
199 * @throws IOException
200 * in case of I/O error
202 public static String
readSmallStream(InputStream stream
) throws IOException
{
203 // do NOT close the reader, or the related stream will be closed, too
205 BufferedReader reader
= new BufferedReader(
206 new InputStreamReader(stream
));
208 StringBuilder builder
= new StringBuilder();
209 for (String line
= reader
.readLine(); line
!= null; line
= reader
211 builder
.append(line
);
212 builder
.append("\n");
215 return builder
.toString();
219 * Recursively delete the given {@link File}, which may of course also be a
222 * Will either silently continue or throw an exception in case of error,
223 * depending upon the parameters.
226 * the target to delete
228 * TRUE to throw an {@link IOException} in case of error, FALSE
229 * to silently continue
231 * @return TRUE if all files were deleted, FALSE if an error occurred
233 * @throws IOException
234 * if an error occurred and the parameters allow an exception to
237 public static boolean deltree(File target
, boolean exception
)
239 List
<File
> list
= deltree(target
, null);
240 if (exception
&& !list
.isEmpty()) {
241 StringBuilder slist
= new StringBuilder();
242 for (File file
: list
) {
243 slist
.append("\n").append(file
.getPath());
246 throw new IOException("Cannot delete all the files from: <" //
247 + target
+ ">:" + slist
.toString());
250 return list
.isEmpty();
254 * Recursively delete the given {@link File}, which may of course also be a
257 * Will silently continue in case of error.
260 * the target to delete
262 * @return TRUE if all files were deleted, FALSE if an error occurred
264 public static boolean deltree(File target
) {
265 return deltree(target
, null).isEmpty();
269 * Recursively delete the given {@link File}, which may of course also be a
272 * Will collect all {@link File} that cannot be deleted in the given
276 * the target to delete
278 * the accumulator to use for errors, or NULL to create a new one
280 * @return the errors accumulator
282 public static List
<File
> deltree(File target
, List
<File
> errorAcc
) {
283 if (errorAcc
== null) {
284 errorAcc
= new ArrayList
<File
>();
287 File
[] files
= target
.listFiles();
289 for (File file
: files
) {
290 errorAcc
= deltree(file
, errorAcc
);
294 if (!target
.delete()) {
295 errorAcc
.add(target
);
302 * Open the given /-separated resource (from the binary root).
307 * @return the opened resource if found, NLL if not
309 public static InputStream
openResource(String name
) {
310 ClassLoader loader
= IOUtils
.class.getClassLoader();
311 if (loader
== null) {
312 loader
= ClassLoader
.getSystemClassLoader();
315 return loader
.getResourceAsStream(name
);
319 * Return a resetable {@link InputStream} from this stream, and reset it.
323 * @return the resetable stream, which <b>may</b> be the same
325 * @throws IOException
326 * in case of I/O error
328 public static InputStream
forceResetableStream(InputStream in
)
330 MarkableFileInputStream tmpIn
= null;
333 boolean resetable
= in
.markSupported();
337 } catch (IOException e
) {
346 tmp
= File
.createTempFile(".tmp-stream", ".tmp");
349 tmpIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
363 * Convert the {@link InputStream} into a byte array.
370 * @throws IOException
371 * in case of I/O error
373 public static byte[] toByteArray(InputStream in
) throws IOException
{
374 ByteArrayOutputStream out
= new ByteArrayOutputStream();
377 byte[] array
= out
.toByteArray();