KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > Utils


1 package org.sapia.clazzy;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 /**
8  * Holds misc. utilities.
9  *
10  * @author Yanick Duchesne
11  *
12  * <dl>
13  * <dt><b>Copyright: </b>
14  * <dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open
15  * Source Software </a>. All Rights Reserved.</dd>
16  * </dt>
17  * <dt><b>License: </b>
18  * <dd>Read the license.txt file of the jar or visit the <a
19  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
20  * OSS web site</dd>
21  * </dt>
22  * </dl>
23  */

24 public class Utils {
25
26   static final int BUFSZ = 1096;
27
28   /**
29    * @param is
30    * an <code>InputStream</code>
31    * @return the <code>byte</code> s of data contained in the stream.
32    * @throws IOException
33    * if an IO problem occurs.
34    */

35   public static byte[] streamToBytes(InputStream JavaDoc is) throws IOException JavaDoc {
36     int read = 0;
37     byte[] buf = new byte[BUFSZ];
38     ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(BUFSZ);
39     while((read = is.read(buf)) > 0) {
40       bos.write(buf, 0, read);
41     }
42     return bos.toByteArray();
43   }
44   
45   /**
46    * @param className
47    * @return the name of package for the given class name, or null if
48    * no package exists (class is part of default package).
49    */

50   public static String JavaDoc getPackageNameFor(String JavaDoc className){
51     int i = className.lastIndexOf('.');
52     if(i < 0){
53       return null;
54     }
55     else{
56       return className.substring(0, i);
57     }
58   }
59
60 }
61
Popular Tags