2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common Development
8
* and Distribution License("CDDL") (collectively, the "License"). You
9
* may not use this file except in compliance with the License. You can obtain
10
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12
* language governing permissions and limitations under the License.
14
* When distributing the software, include this License Header Notice in each
15
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16
* Sun designates this particular file as subject to the "Classpath" exception
17
* as provided by Sun in the GPL Version 2 section of the License file that
18
* accompanied this code. If applicable, add the following below the License
19
* Header, with the fields enclosed by brackets [] replaced by your own
20
* identifying information: "Portions Copyrighted [year]
21
* [name of copyright owner]"
25
* If you wish your version of this file to be governed by only the CDDL or
26
* only the GPL Version 2, indicate your decision by adding "[Contributor]
27
* elects to include this software in this distribution under the [CDDL or GPL
28
* Version 2] license." If you don't indicate a single choice of license, a
29
* recipient has the option to distribute your version of this file under
30
* either the CDDL, the GPL Version 2 or to extend the choice of license to
31
* its licensees as provided above. However, if you add GPL Version 2 code
32
* and therefore, elected the GPL Version 2 license, then the option applies
33
* only if the new code is made subject to such option by the copyright
37
package com.sun.mail.mbox;
40
import javax.mail.internet.*;
41
import javax.activation.*;
44
import com.sun.mail.util.LineInputStream;
47
* The SunV3Multipart class is an implementation of the abstract Multipart
48
* class that uses SunV3 conventions for the multipart data. <p>
50
* @author Bill Shannon
53
public class SunV3Multipart extends MimeMultipart {
55
* Constructs a SunV3Multipart object and its bodyparts from the
56
* given DataSource. <p>
58
* @param ds DataSource, can be a MultipartDataSource
60
public SunV3Multipart(DataSource ds) throws MessagingException {
65
* Set the subtype. Throws MethodNotSupportedException.
67
* @param subtype Subtype
69
public void setSubType(String subtype) throws MessagingException {
70
throw new MethodNotSupportedException(
71
"can't change SunV3Multipart subtype");
75
* Get the BodyPart referred to by the given ContentID (CID).
76
* Throws MethodNotSupportException.
78
public synchronized BodyPart getBodyPart(String CID)
79
throws MessagingException {
80
throw new MethodNotSupportedException(
81
"SunV3Multipart doesn't support Content-ID");
85
* Update headers. Throws MethodNotSupportException.
87
protected void updateHeaders() throws MessagingException {
88
throw new MethodNotSupportedException("SunV3Multipart not writable");
92
* Iterates through all the parts and outputs each SunV3 part
93
* separated by a boundary.
95
public void writeTo(OutputStream os)
96
throws IOException, MessagingException {
97
throw new MethodNotSupportedException(
98
"SunV3Multipart writeTo not supported");
101
private static final String boundary = "----------";
104
* Parse the contents of this multipart message and create the
107
protected synchronized void parse() throws MessagingException {
111
InputStream in = null;
114
in = ds.getInputStream();
115
if (!(in instanceof ByteArrayInputStream) &&
116
!(in instanceof BufferedInputStream))
117
in = new BufferedInputStream(in);
118
} catch (Exception ex) {
119
throw new MessagingException("No inputstream from datasource");
123
int bl = boundary.length();
124
byte[] bndbytes = new byte[bl];
125
boundary.getBytes(0, bl, bndbytes, 0);
129
* Skip any kind of junk until we get to the first
132
LineInputStream lin = new LineInputStream(in);
133
while ((line = lin.readLine()) != null) {
134
if (line.trim().equals(boundary))
138
throw new MessagingException("Missing start boundary");
141
* Read and process body parts until we see the
142
* terminating boundary line (or EOF).
146
* Collect the headers for this body part.
148
InternetHeaders headers = new InternetHeaders(in);
150
if (!in.markSupported())
151
throw new MessagingException("Stream doesn't support mark");
153
ByteArrayOutputStream buf = new ByteArrayOutputStream();
157
* Read and save the content bytes in buf.
159
while ((b = in.read()) >= 0) {
160
if (b == '\r' || b == '\n') {
162
* Found the end of a line, check whether the
163
* next line is a boundary.
166
in.mark(bl + 4 + 1); // "4" for possible "--\r\n"
167
if (b == '\r' && in.read() != '\n') {
171
// read bytes, matching against the boundary
172
for (i = 0; i < bl; i++)
173
if (in.read() != bndbytes[i])
177
// check for end of line
179
break; // got it! break out of the while loop
182
if (in.read() != '\n')
184
break; // got it! break out of the while loop
187
// failed to match, reset and proceed normally
194
* Create a SunV3BodyPart to represent this body part.
197
new SunV3BodyPart(headers, buf.toByteArray());
202
} catch (IOException e) {
203
throw new MessagingException("IO Error"); // XXX