KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > util > ConvertAssist


1 /**
2  * Title: Oyster Project
3  * Description: Creating S/MIME email transport capabilities.
4  * Copyright: Copyright (c) 2001
5  * @Author Vladimir Radisic
6  * @Version 2.1.5
7  */

8
9 package org.enhydra.oyster.util;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * This class contains static methods which make easier work and conversion
19  * between different types under the large amount of data (arrays, streams,
20  * files ...).
21  */

22 public class ConvertAssist {
23
24   /**
25    * Reads data from input stream into the String.
26    * @param in0 source of data for reading
27    * @return data extracted from input stream represented as String
28    * @exception SMIMEException caused by non SMIMEException which is IOException
29    */

30    public static String JavaDoc inStreamToString(InputStream JavaDoc in0) throws SMIMEException {
31
32       String JavaDoc sAtach = new String JavaDoc();
33       try {
34         byte[] b = new byte[100000];
35         int a = in0.read(b);
36         while (a == 100000) {
37           sAtach = sAtach.concat(new String JavaDoc(b, "ISO-8859-1"));
38           a = in0.read(b);
39         }
40         in0.close();
41         sAtach = sAtach.concat(new String JavaDoc(b, "ISO-8859-1").substring(0, a));
42      }
43       catch(IOException JavaDoc e) {
44         throw SMIMEException.getInstance("org.enhydra.oyster.util.ConvertAssist",
45                                           e, "inStreamToString");
46       }
47       return sAtach;
48     }
49
50
51   /**
52    * Reads data from input stream into the byte array.
53    * @param in0 source of data for reading
54    * @return data extracted from input stream represented as byte array
55    * @exception SMIMEException caused by non SMIMEException which is IOException
56    */

57    public static byte[] inStreamToByteArray(InputStream JavaDoc in0) throws SMIMEException {
58
59       byte[] returnArray = null;
60       try {
61         returnArray = ConvertAssist.inStreamToString(in0).getBytes("ISO-8859-1");
62       }
63       catch(IOException JavaDoc e) {
64         throw SMIMEException.getInstance("org.enhydra.oyster.util.ConvertAssist",
65                                           e, "inStreamToByteArray");
66       }
67
68       return returnArray;
69     }
70
71 /**
72  * Reads data from file into the String.
73  * @param file0 abstract path to file represented as File object
74  * @return data from file represented as String
75  * @exception SMIMEException caused by non SMIMEException which is IOException
76  */

77   public static String JavaDoc fileToString(File JavaDoc file0) throws SMIMEException{
78
79     String JavaDoc s = null;
80     try {
81       FileInputStream JavaDoc temp = new FileInputStream JavaDoc(file0);
82       s = new String JavaDoc();
83       byte[] b = new byte[100000];
84       int a = temp.read(b);
85       while (a == 100000) {
86         s = s.concat(new String JavaDoc(b, "ISO-8859-1"));
87         a = temp.read(b);
88       }
89       temp.close();
90       s = s.concat(new String JavaDoc(b, "ISO-8859-1").substring(0, a));
91     }
92     catch(Exception JavaDoc e) {
93       throw SMIMEException.getInstance("org.enhydra.oyster.util.FileAssist",
94                                         e, "fileToString");
95     }
96     return s;
97   }
98
99
100 /**
101  * Reads data from file into the byte array.
102  * @param file0 abstract path to file represented as File object
103  * @return data from file represented as byte array
104  * @exception SMIMEException caused by non SMIMEException which is IOException
105  */

106   public static byte[] fileToByteArray(File JavaDoc file0) throws SMIMEException {
107
108     try {
109       return fileToString(file0).getBytes("ISO-8859-1");
110     }
111     catch(Exception JavaDoc e) {
112       throw SMIMEException.getInstance("org.enhydra.oyster.util.FileAssist",
113                                         e, "fileToByteArray");
114     }
115   }
116
117 }
Popular Tags