3 import java.io.Closeable;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.ObjectInputStream;
11 import java.io.ObjectOutputStream;
12 import java.io.OutputStream;
14 import org.apache.log4j.Logger;
18 static private Logger _logger = Logger.getLogger(IOUtils.class);
20 static final private int BUFFER_SIZE = 1024 * 8;
22 static private final int COPY_SIZE = 1024 * 8;
24 static public void copy(InputStream in, OutputStream out) throws IOException
26 byte[] data = new byte[BUFFER_SIZE];
29 while ((read = in.read(data)) > 0)
30 out.write(data, 0, read);
33 static public void close(Closeable closeable)
35 if (closeable != null) {
38 } catch (Throwable cause) {
39 _logger.error("Error trying to close closeable item.", cause);
44 static public void recursiveDelete(File target)
49 if (target.isDirectory())
50 for (File newTarget : target.listFiles())
51 recursiveDelete(newTarget);
56 static public void copy(File source, File target) throws IOException
58 InputStream in = null;
59 OutputStream out = null;
62 in = new FileInputStream(source);
63 out = new FileOutputStream(target);
71 static public void serialize(String filePath, Object obj) throws IOException
73 serialize(new File(filePath), obj);
76 static public void serialize(File target, Object obj) throws IOException
78 FileOutputStream fos = null;
81 fos = new FileOutputStream(target);
82 ObjectOutputStream oos = new ObjectOutputStream(fos);
90 static public <Type> Type deserialize(Class<Type> cl, String sourcePath) throws FileNotFoundException, IOException
92 return deserialize(cl, new File(sourcePath));
95 static public <Type> Type deserialize(Class<Type> cl, File source) throws FileNotFoundException, IOException
97 FileInputStream fin = null;
100 fin = new FileInputStream(source);
101 ObjectInputStream ois = new ObjectInputStream(fin);
102 return cl.cast(ois.readObject());
103 } catch (ClassNotFoundException e) {
104 throw new IOException("Unable to deserialize from file.", e);