KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > IOUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 import java.io.* ;
8 import java.net.URL JavaDoc;
9
10 /**
11  * @author: Tuan Nguyen
12  * @version: $Id: IOUtil.java,v 1.4 2004/09/14 02:41:19 tuan08 Exp $
13  * @since: 0.0
14  * @email: tuan08@yahoo.com
15  */

16 public class IOUtil {
17   static public String JavaDoc getFileContenntAsString(File file, String JavaDoc encoding) throws Exception JavaDoc {
18     FileInputStream is = new FileInputStream(file) ;
19     return new String JavaDoc(getStreamContentAsBytes(is), encoding) ;
20   }
21   
22   static public String JavaDoc getFileContenntAsString(File file) throws Exception JavaDoc {
23     FileInputStream is = new FileInputStream(file) ;
24     return new String JavaDoc(getStreamContentAsBytes(is)) ;
25   }
26   
27   static public String JavaDoc getFileContenntAsString(String JavaDoc fileName, String JavaDoc encoding) throws Exception JavaDoc {
28     FileInputStream is = new FileInputStream(fileName) ;
29     return new String JavaDoc(getStreamContentAsBytes(is), encoding) ;
30   }
31   
32     static public String JavaDoc getFileContenntAsString(String JavaDoc fileName) throws Exception JavaDoc {
33         FileInputStream is = new FileInputStream(fileName) ;
34         return new String JavaDoc(getStreamContentAsBytes(is)) ;
35     }
36     
37     static public byte[] getFileContentAsBytes(String JavaDoc fileName) throws Exception JavaDoc {
38         FileInputStream is = new FileInputStream(fileName) ;
39         return getStreamContentAsBytes(is) ;
40     }
41     
42     static public String JavaDoc getStreamContentAsString(InputStream is) throws Exception JavaDoc {
43     char buf[] = new char[is.available()];
44     BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
45     in.read(buf);
46       return new String JavaDoc(buf) ;
47     }
48     
49     static public byte[] getStreamContentAsBytes(InputStream is) throws Exception JavaDoc {
50         byte buf[] = new byte[is.available()];
51       for(int start = 0; start < buf.length; start += is.read(buf, start, buf.length - start));
52       return buf ;
53     }
54   
55   static public String JavaDoc getResourceAsString(String JavaDoc resource) throws Exception JavaDoc {
56     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
57     URL JavaDoc url = cl.getResource(resource);
58     InputStream is = url.openStream();
59     return getStreamContentAsString(is) ;
60   }
61   
62   static public byte[] getResourceAsBytes(String JavaDoc resource) throws Exception JavaDoc {
63     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
64     URL JavaDoc url = cl.getResource(resource);
65     InputStream is = url.openStream();
66     return getStreamContentAsBytes(is) ;
67   }
68
69     static public byte[] serialize(Object JavaDoc obj) throws Exception JavaDoc {
70       //long start = System.currentTimeMillis() ;
71
ByteArrayOutputStream bytes = new ByteArrayOutputStream() ;
72       ObjectOutputStream out = new ObjectOutputStream(bytes);
73       out.writeObject(obj);
74       out.close();
75       byte[] ret = bytes.toByteArray() ;
76       //long end = System.currentTimeMillis() ;
77
return ret ;
78     }
79
80     static public Object JavaDoc deserialize(byte[] bytes) throws Exception JavaDoc {
81       if (bytes == null) return null ;
82       //long start = System.currentTimeMillis() ;
83
ByteArrayInputStream is = new ByteArrayInputStream(bytes) ;
84       ObjectInputStream in = new ObjectInputStream(is);
85       Object JavaDoc obj = in.readObject() ;
86       in.close();
87       return obj ;
88     }
89 }
Popular Tags