KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > attachments > ImageDataSource


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.attachments;
17
18 import javax.activation.DataSource JavaDoc;
19 import java.awt.*;
20 import java.io.*;
21
22 public class ImageDataSource implements DataSource JavaDoc {
23     
24     public static final String JavaDoc CONTENT_TYPE = "image/jpeg";
25     
26     private final String JavaDoc name;
27     
28     private final String JavaDoc contentType;
29     
30     private byte[] data;
31     
32     private ByteArrayOutputStream os;
33     
34     public ImageDataSource(String JavaDoc name, Image data) {
35         this(name, CONTENT_TYPE, data);
36     } // ctor
37

38     public ImageDataSource(String JavaDoc name, String JavaDoc contentType, Image data) {
39         this.name = name;
40         this.contentType = contentType == null ? CONTENT_TYPE : contentType;
41         os = new ByteArrayOutputStream();
42         try {
43             if (data != null) {
44                 new JDK13IO().saveImage(this.contentType, data, os);
45             }
46         } catch (Exception JavaDoc e) {
47             // log.error(Messages.getMessage("exception00"), e);
48
}
49     }
50     
51     public String JavaDoc getName() {
52         return name;
53     } // getName
54

55     public String JavaDoc getContentType() {
56         return contentType;
57     } // getContentType
58

59     public InputStream getInputStream() throws IOException {
60         if (os.size() != 0) {
61             data = os.toByteArray();
62             os.reset();
63         }
64         return new ByteArrayInputStream(data == null ? new byte[0] : data);
65     } // getInputStream
66

67     public OutputStream getOutputStream() throws IOException {
68         if (os.size() != 0) {
69             data = os.toByteArray();
70             os.reset();
71         }
72         return os;
73     } // getOutputStream
74
} // class ImageDataSource
75
Popular Tags