Source Code Cross Referenced for ByteArrayDataSource.java in  » Net » apache-common-Email » org » apache » commons » 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 » Net » apache common Email » org.apache.commons.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.mail;
017:
018:        import java.io.BufferedInputStream;
019:        import java.io.BufferedOutputStream;
020:        import java.io.ByteArrayInputStream;
021:        import java.io.ByteArrayOutputStream;
022:        import java.io.IOException;
023:        import java.io.InputStream;
024:        import java.io.OutputStream;
025:        import java.io.UnsupportedEncodingException;
026:
027:        import javax.activation.DataSource;
028:
029:        /**
030:         * This class implements a typed DataSource from:<br>
031:         *
032:         * - an InputStream<br>
033:         * - a byte array<br>
034:         * - a String<br>
035:         *
036:         * @since 1.0
037:         * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
038:         * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
039:         * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
040:         * @version $Id: ByteArrayDataSource.java 225600 2005-07-27 20:16:23Z rdonkin $
041:         */
042:        public class ByteArrayDataSource implements  DataSource {
043:            /** define the buffer size */
044:            public static final int BUFFER_SIZE = 512;
045:
046:            /** Stream containg the Data */
047:            private ByteArrayOutputStream baos;
048:
049:            /** Content-type. */
050:            private String type = "application/octet-stream";
051:
052:            /**
053:             * Create a datasource from a byte array.
054:             *
055:             * @param data A byte[].
056:             * @param aType A String.
057:             * @throws IOException IOException
058:             * @since 1.0
059:             */
060:            public ByteArrayDataSource(byte[] data, String aType)
061:                    throws IOException {
062:                ByteArrayInputStream bis = null;
063:
064:                try {
065:                    bis = new ByteArrayInputStream(data);
066:                    this .byteArrayDataSource(bis, aType);
067:                } catch (IOException ioex) {
068:                    throw ioex;
069:                } finally {
070:                    if (bis != null) {
071:                        bis.close();
072:                    }
073:                }
074:            }
075:
076:            /**
077:             * Create a datasource from an input stream.
078:             *
079:             * @param aIs An InputStream.
080:             * @param aType A String.
081:             * @throws IOException IOException
082:             * @since 1.0
083:             */
084:            public ByteArrayDataSource(InputStream aIs, String aType)
085:                    throws IOException {
086:                this .byteArrayDataSource(aIs, aType);
087:            }
088:
089:            /**
090:             * Create a datasource from a String.
091:             *
092:             * @param data A String.
093:             * @param aType A String.
094:             * @throws IOException IOException
095:             * @since 1.0
096:             */
097:            public ByteArrayDataSource(String data, String aType)
098:                    throws IOException {
099:                this .type = aType;
100:
101:                try {
102:                    baos = new ByteArrayOutputStream();
103:
104:                    // Assumption that the string contains only ASCII
105:                    // characters!  Else just pass in a charset into this
106:                    // constructor and use it in getBytes().
107:                    baos.write(data.getBytes("iso-8859-1"));
108:                    baos.flush();
109:                    baos.close();
110:                } catch (UnsupportedEncodingException uex) {
111:                    throw new IOException(
112:                            "The Character Encoding is not supported.");
113:                } finally {
114:                    if (baos != null) {
115:                        baos.close();
116:                    }
117:                }
118:            }
119:
120:            /**
121:             * Create a datasource from an input stream.
122:             *
123:             * @param aIs An InputStream.
124:             * @param aType A String.
125:             * @throws IOException IOException
126:             */
127:            private void byteArrayDataSource(InputStream aIs, String aType)
128:                    throws IOException {
129:                this .type = aType;
130:
131:                BufferedInputStream bis = null;
132:                BufferedOutputStream osWriter = null;
133:
134:                try {
135:                    int length = 0;
136:                    byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
137:
138:                    bis = new BufferedInputStream(aIs);
139:                    baos = new ByteArrayOutputStream();
140:                    osWriter = new BufferedOutputStream(baos);
141:
142:                    //Write the InputData to OutputStream
143:                    while ((length = bis.read(buffer)) != -1) {
144:                        osWriter.write(buffer, 0, length);
145:                    }
146:                    osWriter.flush();
147:                    osWriter.close();
148:
149:                } catch (IOException ioex) {
150:                    throw ioex;
151:                } finally {
152:                    if (bis != null) {
153:                        bis.close();
154:                    }
155:                    if (baos != null) {
156:                        baos.close();
157:                    }
158:                    if (osWriter != null) {
159:                        osWriter.close();
160:                    }
161:                }
162:            }
163:
164:            /**
165:             * Get the content type.
166:             *
167:             * @return A String.
168:             * @since 1.0
169:             */
170:            public String getContentType() {
171:                return type == null ? "application/octet-stream" : type;
172:            }
173:
174:            /**
175:             * Get the input stream.
176:             *
177:             * @return An InputStream.
178:             * @throws IOException IOException
179:             * @since 1.0
180:             */
181:            public InputStream getInputStream() throws IOException {
182:                if (baos == null) {
183:                    throw new IOException("no data");
184:                }
185:                return new ByteArrayInputStream(baos.toByteArray());
186:            }
187:
188:            /**
189:             * Get the name.
190:             *
191:             * @return A String.
192:             * @since 1.0
193:             */
194:            public String getName() {
195:                return "ByteArrayDataSource";
196:            }
197:
198:            /**
199:             * Get the OutputStream to write to
200:             *
201:             * @return  An OutputStream
202:             * @since 1.0
203:             */
204:            public OutputStream getOutputStream() {
205:                baos = new ByteArrayOutputStream();
206:                return baos;
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.