feisty meow concerns codebase 2.140
IOUtils.java
Go to the documentation of this file.
1package org.gffs.io;
2
3import java.io.Closeable;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.ObjectInputStream;
11import java.io.ObjectOutputStream;
12import java.io.OutputStream;
13
14import org.apache.log4j.Logger;
15
16public class IOUtils
17{
18 static private Logger _logger = Logger.getLogger(IOUtils.class);
19
20 static final private int BUFFER_SIZE = 1024 * 8;
21
22// static private final int COPY_SIZE = 1024 * 8;
23
24 static public void copy(InputStream in, OutputStream out) throws IOException
25 {
26 byte[] data = new byte[BUFFER_SIZE];
27 int read;
28
29 while ((read = in.read(data)) > 0)
30 out.write(data, 0, read);
31 }
32
33 static public void close(Closeable closeable)
34 {
35 if (closeable != null) {
36 try {
37 closeable.close();
38 } catch (Throwable cause) {
39 _logger.error("Error trying to close closeable item.", cause);
40 }
41 }
42 }
43
44 static public void recursiveDelete(File target)
45 {
46 if (!target.exists())
47 return;
48
49 if (target.isDirectory())
50 for (File newTarget : target.listFiles())
51 recursiveDelete(newTarget);
52
53 target.delete();
54 }
55
56 static public void copy(File source, File target) throws IOException
57 {
58 InputStream in = null;
59 OutputStream out = null;
60
61 try {
62 in = new FileInputStream(source);
63 out = new FileOutputStream(target);
64 copy(in, out);
65 } finally {
66 close(in);
67 close(out);
68 }
69 }
70
71 static public void serialize(String filePath, Object obj) throws IOException
72 {
73 serialize(new File(filePath), obj);
74 }
75
76 static public void serialize(File target, Object obj) throws IOException
77 {
78 FileOutputStream fos = null;
79
80 try {
81 fos = new FileOutputStream(target);
82 ObjectOutputStream oos = new ObjectOutputStream(fos);
83 oos.writeObject(obj);
84 oos.close();
85 } finally {
86 close(fos);
87 }
88 }
89
90 static public <Type> Type deserialize(Class<Type> cl, String sourcePath) throws FileNotFoundException, IOException
91 {
92 return deserialize(cl, new File(sourcePath));
93 }
94
95 static public <Type> Type deserialize(Class<Type> cl, File source) throws FileNotFoundException, IOException
96 {
97 FileInputStream fin = null;
98
99 try {
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);
105 } finally {
106 close(fin);
107 }
108 }
109}
#define read
Definition Xos2defs.h:38
#define close
Definition Xos2defs.h:13
static void recursiveDelete(File target)
Definition IOUtils.java:44
static void serialize(String filePath, Object obj)
Definition IOUtils.java:71
static void copy(File source, File target)
Definition IOUtils.java:56
static void serialize(File target, Object obj)
Definition IOUtils.java:76
static void close(Closeable closeable)
Definition IOUtils.java:33
static void copy(InputStream in, OutputStream out)
Definition IOUtils.java:24