Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/util/ByteArrayDataSource.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: ByteArrayDataSource.java,v $
5    * $Date: 2003/02/26 03:38:45 $
6    * $Revision: 1.3 $
7    */
8   
9   package com.clra.util;
10  
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.InputStream;
14  import java.io.IOException;
15  import java.io.OutputStream;
16  import java.io.UnsupportedEncodingException;
17  import java.util.Date;
18  
19  import javax.activation.DataSource;
20  
21  
22  /**
23   * Used to create a DataSource for the mail message.
24   * @see MailHelper
25   */
26  class ByteArrayDataSource implements DataSource {
27      private byte[] data; // data for mail message
28      private String type; // content type/mime type
29  
30     /**
31      * Create a DataSource from a String
32      * @param data is the contents of the mail message
33      * @param type is the mime-type such as text/html
34      */
35      ByteArrayDataSource(String data, String type) {
36          try {
37             this.data = data.getBytes("UTF-8");
38          } catch (UnsupportedEncodingException uex) { }
39          this.type = type;
40      }
41  
42      //DataSource interface methods
43  
44      public InputStream getInputStream() throws IOException {
45          if (data == null)
46              throw new IOException("no data");
47          return new ByteArrayInputStream(data);
48      }
49  
50      public OutputStream getOutputStream() throws IOException {
51          throw new IOException("cannot do this");
52      }
53  
54      public String getContentType() {
55          return type;
56      }
57  
58      public String getName() {
59          return "dummy";
60      }
61  }
62  
63