KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > util > IO


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.util;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.ObjectOutputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.io.Serializable JavaDoc;
12
13 /**
14  * IO Utilities.
15  */

16 public class IO {
17
18     
19     /**
20      * Copy from one stream to another.
21      *
22      * @param in InputStream.
23      * @param out OutputStream.
24      *
25      * @throws IOException If copy fails.
26      */

27     public static void copy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc{
28         byte b[] = new byte[8192];
29         int i;
30         while ((i = in.read(b)) != -1) {
31             out.write(b, 0, i);
32         }
33     }
34
35     /**
36      * Can the object be serialized.
37      *
38      * @param o The object.
39      * @return true if it can, false if it can't.
40      */

41     public static boolean canSerialize(Object JavaDoc o) {
42         if (o == null) {
43             return true;
44         }
45         if (!(o instanceof Serializable JavaDoc)) {
46             return false;
47         }
48         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
49         try {
50             ObjectOutputStream JavaDoc s = new ObjectOutputStream JavaDoc(os);
51             s.writeObject(o);
52         } catch (IOException JavaDoc e) {
53             return false;
54         }
55         return true;
56     }
57
58 }
59
Popular Tags