KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > 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.axis.attachments;
17
18 import org.apache.axis.components.image.ImageIOFactory;
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.utils.Messages;
21 import org.apache.commons.logging.Log;
22
23 import javax.activation.DataSource JavaDoc;
24 import java.awt.*;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30
31 public class ImageDataSource implements DataSource JavaDoc {
32     protected static Log log =
33         LogFactory.getLog(ImageDataSource.class.getName());
34     
35     public static final String JavaDoc CONTENT_TYPE = "image/jpeg";
36
37     private final String JavaDoc name;
38     private final String JavaDoc contentType;
39     private byte[] data;
40     private ByteArrayOutputStream JavaDoc os;
41
42     public ImageDataSource(String JavaDoc name, Image data) {
43         this(name, CONTENT_TYPE, data);
44     } // ctor
45

46     public ImageDataSource(String JavaDoc name, String JavaDoc contentType, Image data) {
47         this.name = name;
48         this.contentType = contentType == null ? CONTENT_TYPE : contentType;
49         os = new ByteArrayOutputStream JavaDoc();
50         try {
51             if (data != null) {
52                 ImageIOFactory.getImageIO().saveImage(this.contentType, data, os);
53             }
54         }
55         catch (Exception JavaDoc e) {
56             log.error(Messages.getMessage("exception00"), e);
57         }
58     } // ctor
59

60     public String JavaDoc getName() {
61         return name;
62     } // getName
63

64     public String JavaDoc getContentType() {
65         return contentType;
66     } // getContentType
67

68     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
69         if (os.size() != 0) {
70             data = os.toByteArray();
71             os.reset();
72         }
73         return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
74     } // getInputStream
75

76     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
77         if (os.size() != 0) {
78             data = os.toByteArray();
79             os.reset();
80         }
81         return os;
82     } // getOutputStream
83
} // class ImageDataSource
84
Popular Tags