KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > attachments > ImageDataSource


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 package org.jboss.axis.attachments;
9
10 import org.jboss.axis.components.image.ImageIOFactory;
11 import org.jboss.axis.utils.Messages;
12 import org.jboss.logging.Logger;
13
14 import javax.activation.DataSource JavaDoc;
15 import java.awt.*;
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 public class ImageDataSource implements DataSource JavaDoc
23 {
24    private static Logger log = Logger.getLogger(ImageDataSource.class.getName());
25
26    public static final String JavaDoc CONTENT_TYPE = "image/jpeg";
27
28    private final String JavaDoc name;
29    private final String JavaDoc contentType;
30    private byte[] data;
31    private ByteArrayInputStream JavaDoc is;
32    private ByteArrayOutputStream JavaDoc os;
33
34    public ImageDataSource(String JavaDoc name, Image data)
35    {
36       this(name, CONTENT_TYPE, data);
37    } // ctor
38

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

57    public String JavaDoc getName()
58    {
59       return name;
60    } // getName
61

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

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

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