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
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipOutputStream
;
17 * This class offer some utilities based around Streams.
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[4069];
56 for (int len
= 0; (len
= in
.read(buffer
)) > 0;) {
57 out
.write(buffer
, 0, len
);
62 * Recursively Add a {@link File} (which can thus be a directory, too) to a
63 * {@link ZipOutputStream}.
68 * the path to prepend to the ZIP info before the actual
71 * the source {@link File} (which can be a directory)
73 * FALSE if we need to add a {@link ZipEntry} for base/target,
74 * TRUE to add it at the root of the ZIP
77 * in case of I/O error
79 public static void zip(ZipOutputStream zip
, String base
, File target
,
80 boolean targetIsRoot
) throws IOException
{
81 if (target
.isDirectory()) {
83 if (base
== null || base
.isEmpty()) {
84 base
= target
.getName();
86 base
+= "/" + target
.getName();
88 zip
.putNextEntry(new ZipEntry(base
+ "/"));
90 for (File file
: target
.listFiles()) {
91 zip(zip
, base
, file
, false);
94 if (base
== null || base
.isEmpty()) {
95 base
= target
.getName();
97 base
+= "/" + target
.getName();
99 zip
.putNextEntry(new ZipEntry(base
));
100 FileInputStream in
= new FileInputStream(target
);
102 IOUtils
.write(in
, zip
);
110 * Zip the given source into dest.
113 * the source {@link File} (which can be a directory)
115 * the destination <tt>.zip</tt> file
117 * FALSE if we need to add a {@link ZipEntry} for src, TRUE to
118 * add it at the root of the ZIP
120 * @throws IOException
121 * in case of I/O error
123 public static void zip(File src
, File dest
, boolean srcIsRoot
)
125 OutputStream out
= new FileOutputStream(dest
);
127 ZipOutputStream zip
= new ZipOutputStream(out
);
129 IOUtils
.zip(zip
, "", src
, srcIsRoot
);
139 * Write the {@link String} content to {@link File}.
142 * the directory where to write the {@link File}
144 * the {@link File} name
148 * @throws IOException
149 * in case of I/O error
151 public static void writeSmallFile(File dir
, String filename
, String content
)
157 FileWriter writerVersion
= new FileWriter(new File(dir
, filename
));
159 writerVersion
.write(content
);
161 writerVersion
.close();
166 * Read the whole {@link File} content into a {@link String}.
171 * @return the content
173 * @throws IOException
174 * in case of I/O error
176 public static String
readSmallFile(File file
) throws IOException
{
177 InputStream stream
= new FileInputStream(file
);
179 return readSmallStream(stream
);
186 * Read the whole {@link InputStream} content into a {@link String}.
189 * the {@link InputStream}
191 * @return the content
193 * @throws IOException
194 * in case of I/O error
196 public static String
readSmallStream(InputStream stream
) throws IOException
{
197 // do NOT close the reader, or the related stream will be closed, too
199 BufferedReader reader
= new BufferedReader(
200 new InputStreamReader(stream
));
202 StringBuilder builder
= new StringBuilder();
203 for (String line
= reader
.readLine(); line
!= null; line
= reader
205 builder
.append(line
);
206 builder
.append("\n");
209 return builder
.toString();
213 * Recursively delete the given {@link File}, which may of course also be a
216 * Will silently continue in case of error.
219 * the target to delete
221 public static void deltree(File target
) {
222 File
[] files
= target
.listFiles();
224 for (File file
: files
) {
229 if (!target
.delete()) {
230 System
.err
.println("Cannot delete: " + target
.getAbsolutePath());
235 * Open the given /-separated resource (from the binary root).
240 * @return the opened resource if found, NLL if not
242 public static InputStream
openResource(String name
) {
243 ClassLoader loader
= IOUtils
.class.getClassLoader();
244 if (loader
== null) {
245 loader
= ClassLoader
.getSystemClassLoader();
248 return loader
.getResourceAsStream(name
);
252 * Return a resetable {@link InputStream} from this stream, and reset it.
256 * @return the resetable stream, which <b>may</b> be the same
258 * @throws IOException
259 * in case of I/O error
261 public static InputStream
forceResetableStream(InputStream in
)
263 MarkableFileInputStream tmpIn
= null;
266 boolean resetable
= in
.markSupported();
270 } catch (IOException e
) {
279 tmp
= File
.createTempFile(".tmp-stream", ".tmp");
282 tmpIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
296 * Convert the {@link InputStream} into a byte array.
303 * @throws IOException
304 * in case of I/O error
306 public static byte[] toByteArray(InputStream in
) throws IOException
{
307 ByteArrayOutputStream out
= new ByteArrayOutputStream();
310 byte[] array
= out
.toByteArray();