Source Code Cross Referenced for ByteArrayDataSource.java in  » Development » jodd » jodd » mail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jodd » jodd.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003:        package jodd.mail;
004:
005:        import java.io.ByteArrayInputStream;
006:        import java.io.ByteArrayOutputStream;
007:        import java.io.File;
008:        import java.io.FileInputStream;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.OutputStream;
012:        import java.io.UnsupportedEncodingException;
013:
014:        import javax.activation.DataSource;
015:        import javax.activation.FileTypeMap;
016:
017:        /**
018:         * Implements a DataSource from an InputStream, a byte array, a String, a File.
019:         *
020:         * This class has been build upon the similar one in javamail demos, but it
021:         * is enhanced.
022:         */
023:        public class ByteArrayDataSource implements  DataSource {
024:
025:            private byte[] data; // data
026:            private String type; // content-type
027:
028:            /**
029:             * Create a datasource from a File. If the Content-Type parameter is null,
030:             * the type will be derived from the filename extension.
031:             *
032:             * @param f File object
033:             * @param type Content-Type
034:             */
035:            public ByteArrayDataSource(File f, String type) throws IOException {
036:                this (new FileInputStream(f), type);
037:                if (this .type == null) {
038:                    this .type = FileTypeMap.getDefaultFileTypeMap()
039:                            .getContentType(f);
040:                }
041:            }
042:
043:            /**
044:             * Create a datasource from an input stream.
045:             *
046:             * @param is InputStream
047:             * @param type Content-Type
048:             */
049:            public ByteArrayDataSource(InputStream is, String type)
050:                    throws IOException {
051:                this .type = type;
052:
053:                ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
054:
055:                byte buf[] = new byte[4096];
056:                int len;
057:                while (true) {
058:                    len = is.read(buf);
059:                    if (len < 0) {
060:                        break;
061:                    }
062:                    os.write(buf, 0, len);
063:                }
064:                data = os.toByteArray();
065:            }
066:
067:            /**
068:             * Create a datasource from a byte array.
069:             *
070:             * @param data byte array
071:             * @param type Content-Type
072:             */
073:            public ByteArrayDataSource(byte[] data, String type) {
074:                this .type = type;
075:                this .data = data;
076:            }
077:
078:            /**
079:             * Create a datasource from a String. This method defaults to
080:             * a String encoding of iso-8859-1. For a different encoding,
081:             * specify a Mime "charset" in the Content-Type parameter.
082:             *
083:             * @param data byte array
084:             * @param type Content-Type
085:             */
086:            public ByteArrayDataSource(String data, String type) {
087:                this .type = type;
088:                try {
089:                    this .data = data.getBytes("ISO-8859-1");
090:                } catch (UnsupportedEncodingException uex) {
091:                    // ignore
092:                }
093:            }
094:
095:            /**
096:             * Return an InputStream to read the content.
097:             *
098:             * @return an InputStream with the content
099:             */
100:            public InputStream getInputStream() throws IOException {
101:                if (data == null) {
102:                    throw new IOException("No data.");
103:                }
104:                return new ByteArrayInputStream(data);
105:            }
106:
107:            /**
108:             * This DataSource cannot return an OutputStream, so this method is not
109:             * implemented.
110:             */
111:            public OutputStream getOutputStream() throws IOException {
112:                throw new IOException("getOutputStream() not supported.");
113:            }
114:
115:            /**
116:             * Get the content type.
117:             *
118:             * @return Content-Type string
119:             */
120:            public String getContentType() {
121:                return type;
122:            }
123:
124:            /**
125:             * Set the content type.
126:             *
127:             * @param type	Content-Type string
128:             */
129:            public void setContentType(String type) {
130:                this .type = type;
131:            }
132:
133:            /**
134:             * getName() is not implemented.
135:             */
136:            public String getName() {
137:                return "";
138:            }
139:
140:            /**
141:             * Write the content to an OutputStream.
142:             *
143:             * @param os OutputStream to write the entire content to
144:             */
145:            public void writeTo(OutputStream os) throws IOException {
146:                os.write(data);
147:            }
148:
149:            /**
150:             * Return the content as a byte array.
151:             *
152:             * @return byte array with the content
153:             */
154:            public byte[] toByteArray() {
155:                return data;
156:            }
157:
158:            /**
159:             * Return the number of bytes in the content.
160:             *
161:             * @return size of the byte array, or -1 if not set.
162:             */
163:            public int getSize() {
164:                if (data == null) {
165:                    return -1;
166:                } else {
167:                    return data.length;
168:                }
169:            }
170:
171:            /**
172:             * Return the content as a String. The Content-Type "charset" parameter
173:             * will be used to determine the encoding, and if that's not available or
174:             * invalid, "iso-8859-1".
175:             *
176:             * @return a String with the content
177:             */
178:            public String getText() {
179:                try {
180:                    return new String(data, type);
181:                } catch (UnsupportedEncodingException uex) {
182:                    try {
183:                        return new String(data, "ISO-8859-1");
184:                    } catch (UnsupportedEncodingException uex1) {
185:                        return null;
186:                    }
187:                }
188:            }
189:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.