Source Code Cross Referenced for FileBackedMimeBodyPart.java in  » Security » Bouncy-Castle » org » bouncycastle » mail » smime » util » 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 » Security » Bouncy Castle » org.bouncycastle.mail.smime.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.mail.smime.util;
002:
003:        import javax.mail.MessagingException;
004:        import javax.mail.internet.InternetHeaders;
005:        import javax.mail.internet.MimeBodyPart;
006:        import java.io.File;
007:        import java.io.FileOutputStream;
008:        import java.io.IOException;
009:        import java.io.InputStream;
010:        import java.io.OutputStream;
011:        import java.util.Enumeration;
012:
013:        public class FileBackedMimeBodyPart extends MimeBodyPart {
014:            private static final int BUF_SIZE = 32760;
015:
016:            private final File _file;
017:
018:            /**
019:             * Create a MimeBodyPart backed by the data in file.
020:             *
021:             * @param file file containing the body part.
022:             * @throws MessagingException an exception occurs parsing file.
023:             * @throws IOException an exception occurs accessing file.
024:             */
025:            public FileBackedMimeBodyPart(File file) throws MessagingException,
026:                    IOException {
027:                super (new SharedFileInputStream(file));
028:
029:                _file = file;
030:            }
031:
032:            /**
033:             * Create a MimeBodyPart backed by file based on the headers and
034:             * content data in content.
035:             *
036:             * @param content an inputstream containing the body part.
037:             * @param file a handle to the backing file to use for storage.
038:             * @throws MessagingException an exception occurs parsing the resulting body part in file.
039:             * @throws IOException an exception occurs accessing file or content.
040:             */
041:            public FileBackedMimeBodyPart(InputStream content, File file)
042:                    throws MessagingException, IOException {
043:                this (saveStreamToFile(content, file));
044:            }
045:
046:            /**
047:             * Create a MimeBodyPart backed by file, with the headers
048:             * given in headers and body content taken from the stream body.
049:             *
050:             * @param headers headers for the body part.
051:             * @param body internal content for the body part.
052:             * @param file backing file to use.
053:             *
054:             * @throws MessagingException if the body part can't be produced.
055:             * @throws IOException if there is an issue reading stream or writing to file.
056:             */
057:            public FileBackedMimeBodyPart(InternetHeaders headers,
058:                    InputStream body, File file) throws MessagingException,
059:                    IOException {
060:                this (saveStreamToFile(headers, body, file));
061:            }
062:
063:            public void writeTo(OutputStream out) throws IOException,
064:                    MessagingException {
065:                if (!_file.exists()) {
066:                    throw new IOException("file " + _file.getCanonicalPath()
067:                            + " no longer exists.");
068:                }
069:
070:                super .writeTo(out);
071:            }
072:
073:            /**
074:             * Close off the underlying shared streams and remove the backing file.
075:             *
076:             * @throws IOException if streams cannot be closed or the file cannot be deleted.
077:             */
078:            public void dispose() throws IOException {
079:                ((SharedFileInputStream) contentStream).getRoot().dispose();
080:
081:                if (!_file.delete()) {
082:                    throw new IOException("deletion of underlying file <"
083:                            + _file.getCanonicalPath() + "> failed.");
084:                }
085:            }
086:
087:            private static File saveStreamToFile(InputStream content,
088:                    File tempFile) throws IOException {
089:                saveContentToStream(new FileOutputStream(tempFile), content);
090:
091:                return tempFile;
092:            }
093:
094:            private static File saveStreamToFile(InternetHeaders headers,
095:                    InputStream content, File tempFile) throws IOException {
096:                OutputStream out = new FileOutputStream(tempFile);
097:                Enumeration en = headers.getAllHeaderLines();
098:
099:                while (en.hasMoreElements()) {
100:                    writeHeader(out, (String) en.nextElement());
101:                }
102:
103:                writeSeperator(out);
104:
105:                saveContentToStream(out, content);
106:
107:                return tempFile;
108:            }
109:
110:            private static void writeHeader(OutputStream out, String header)
111:                    throws IOException {
112:                for (int i = 0; i != header.length(); i++) {
113:                    out.write(header.charAt(i));
114:                }
115:
116:                writeSeperator(out);
117:            }
118:
119:            private static void writeSeperator(OutputStream out)
120:                    throws IOException {
121:                out.write('\r');
122:                out.write('\n');
123:            }
124:
125:            private static void saveContentToStream(OutputStream out,
126:                    InputStream content) throws IOException {
127:                byte[] buf = new byte[BUF_SIZE];
128:                int len;
129:
130:                while ((len = content.read(buf, 0, buf.length)) > 0) {
131:                    out.write(buf, 0, len);
132:                }
133:
134:                out.close();
135:                content.close();
136:            }
137:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.