001: /**
002: * Title: Oyster Project
003: * Description: Creating S/MIME email transport capabilities.
004: * Copyright: Copyright (c) 2001
005: * @Author Vladimir Radisic
006: * @Version 2.1.6
007: */package org.enhydra.oyster.util;
008:
009: import org.enhydra.oyster.exception.SMIMEException;
010: import java.io.File;
011: import java.io.FileInputStream;
012: import java.io.InputStream;
013: import java.io.IOException;
014:
015: /**
016: * This class contains static methods which make easier work and conversion
017: * between different types under the large amount of data (arrays, streams,
018: * files ...).
019: */
020: public class ConvertAssist {
021:
022: /**
023: * Reads data from input stream into the String.
024: * @param in0 source of data for reading
025: * @return data extracted from input stream represented as String
026: * @exception SMIMEException caused by non SMIMEException which is IOException
027: */
028: public static String inStreamToString(InputStream in0)
029: throws SMIMEException {
030:
031: String sAtach = new String();
032: try {
033: byte[] b = new byte[100000];
034: int a = in0.read(b);
035: while (a == 100000) {
036: sAtach = sAtach.concat(new String(b, "ISO-8859-1"));
037: a = in0.read(b);
038: }
039: in0.close();
040: sAtach = sAtach.concat(new String(b, "ISO-8859-1")
041: .substring(0, a));
042: } catch (IOException e) {
043: throw new SMIMEException(e);
044: }
045: return sAtach;
046: }
047:
048: /**
049: * Reads data from input stream into the byte array.
050: * @param in0 source of data for reading
051: * @return data extracted from input stream represented as byte array
052: * @exception SMIMEException caused by non SMIMEException which is IOException
053: */
054: public static byte[] inStreamToByteArray(InputStream in0)
055: throws SMIMEException {
056:
057: byte[] returnArray = null;
058: try {
059: returnArray = ConvertAssist.inStreamToString(in0).getBytes(
060: "ISO-8859-1");
061: } catch (IOException e) {
062: throw new SMIMEException(e);
063: }
064:
065: return returnArray;
066: }
067:
068: /**
069: * Reads data from file into the String.
070: * @param file0 abstract path to file represented as File object
071: * @return data from file represented as String
072: * @exception SMIMEException caused by non SMIMEException which is IOException
073: */
074: public static String fileToString(File file0) throws SMIMEException {
075:
076: String s = null;
077: try {
078: FileInputStream temp = new FileInputStream(file0);
079: s = new String();
080: byte[] b = new byte[100000];
081: int a = temp.read(b);
082: while (a == 100000) {
083: s = s.concat(new String(b, "ISO-8859-1"));
084: a = temp.read(b);
085: }
086: temp.close();
087: s = s.concat(new String(b, "ISO-8859-1").substring(0, a));
088: } catch (Exception e) {
089: throw new SMIMEException(e);
090: }
091: return s;
092: }
093:
094: /**
095: * Reads data from file into the byte array.
096: * @param file0 abstract path to file represented as File object
097: * @return data from file represented as byte array
098: * @exception SMIMEException caused by non SMIMEException which is IOException
099: */
100: public static byte[] fileToByteArray(File file0)
101: throws SMIMEException {
102:
103: try {
104: return fileToString(file0).getBytes("ISO-8859-1");
105: } catch (Exception e) {
106: throw new SMIMEException(e);
107: }
108: }
109:
110: }
|