~testplan-team/testplan/source-collection

« back to all changes in this revision

Viewing changes to javamail-1.4.3/mbox/src/main/java/com/sun/mail/mbox/SunV3Multipart.java

  • Committer: edA-qa mort-ora-y
  • Date: 2010-11-26 18:56:28 UTC
  • Revision ID: eda-qa@disemia.com-20101126185628-elxvrs14srop28r2
adding javamail 1.4.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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.
 
13
 *
 
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]"
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
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
 
34
 * holder.
 
35
 */
 
36
 
 
37
package com.sun.mail.mbox;
 
38
 
 
39
import javax.mail.*;
 
40
import javax.mail.internet.*;
 
41
import javax.activation.*;
 
42
import java.util.*;
 
43
import java.io.*;
 
44
import com.sun.mail.util.LineInputStream;
 
45
 
 
46
/**
 
47
 * The SunV3Multipart class is an implementation of the abstract Multipart
 
48
 * class that uses SunV3 conventions for the multipart data. <p>
 
49
 *
 
50
 * @author  Bill Shannon
 
51
 */
 
52
 
 
53
public class SunV3Multipart extends MimeMultipart {
 
54
    /**
 
55
     * Constructs a SunV3Multipart object and its bodyparts from the 
 
56
     * given DataSource. <p>
 
57
     *
 
58
     * @param   ds      DataSource, can be a MultipartDataSource
 
59
     */
 
60
    public SunV3Multipart(DataSource ds) throws MessagingException {
 
61
        super(ds);
 
62
    }
 
63
 
 
64
    /**
 
65
     * Set the subtype.  Throws MethodNotSupportedException.
 
66
     *
 
67
     * @param   subtype         Subtype
 
68
     */
 
69
    public void setSubType(String subtype) throws MessagingException {
 
70
        throw new MethodNotSupportedException(
 
71
                "can't change SunV3Multipart subtype");
 
72
    }
 
73
 
 
74
    /**
 
75
     * Get the BodyPart referred to by the given ContentID (CID). 
 
76
     * Throws MethodNotSupportException.
 
77
     */
 
78
    public synchronized BodyPart getBodyPart(String CID) 
 
79
                        throws MessagingException {
 
80
        throw new MethodNotSupportedException(
 
81
                "SunV3Multipart doesn't support Content-ID");
 
82
    }
 
83
 
 
84
    /**
 
85
     * Update headers.  Throws MethodNotSupportException.
 
86
     */
 
87
    protected void updateHeaders() throws MessagingException {
 
88
        throw new MethodNotSupportedException("SunV3Multipart not writable");
 
89
    }
 
90
 
 
91
    /**
 
92
     * Iterates through all the parts and outputs each SunV3 part
 
93
     * separated by a boundary.
 
94
     */
 
95
    public void writeTo(OutputStream os)
 
96
                                throws IOException, MessagingException {
 
97
        throw new MethodNotSupportedException(
 
98
                "SunV3Multipart writeTo not supported");
 
99
    }
 
100
 
 
101
    private static final String boundary = "----------";
 
102
 
 
103
    /*
 
104
     * Parse the contents of this multipart message and create the
 
105
     * child body parts.
 
106
     */
 
107
    protected synchronized void parse() throws MessagingException {
 
108
        if (parsed)
 
109
            return;
 
110
 
 
111
        InputStream in = null;
 
112
 
 
113
        try {
 
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");
 
120
        }
 
121
 
 
122
        String line;
 
123
        int bl = boundary.length();
 
124
        byte[] bndbytes = new byte[bl];
 
125
        boundary.getBytes(0, bl, bndbytes, 0);
 
126
 
 
127
        try {
 
128
            /*
 
129
             * Skip any kind of junk until we get to the first
 
130
             * boundary line.
 
131
             */
 
132
            LineInputStream lin = new LineInputStream(in);
 
133
            while ((line = lin.readLine()) != null) {
 
134
                if (line.trim().equals(boundary))
 
135
                    break;
 
136
            }
 
137
            if (line == null)
 
138
                throw new MessagingException("Missing start boundary");
 
139
 
 
140
            /*
 
141
             * Read and process body parts until we see the
 
142
             * terminating boundary line (or EOF).
 
143
             */
 
144
            for (;;) {
 
145
                /*
 
146
                 * Collect the headers for this body part.
 
147
                 */
 
148
                InternetHeaders headers = new InternetHeaders(in);
 
149
 
 
150
                if (!in.markSupported())
 
151
                    throw new MessagingException("Stream doesn't support mark");
 
152
 
 
153
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
 
154
                int b;
 
155
 
 
156
                /*
 
157
                 * Read and save the content bytes in buf.
 
158
                 */
 
159
                while ((b = in.read()) >= 0) {
 
160
                    if (b == '\r' || b == '\n') {
 
161
                        /*
 
162
                         * Found the end of a line, check whether the
 
163
                         * next line is a boundary.
 
164
                         */
 
165
                        int i;
 
166
                        in.mark(bl + 4 + 1);    // "4" for possible "--\r\n"
 
167
                        if (b == '\r' && in.read() != '\n') {
 
168
                            in.reset();
 
169
                            in.mark(bl + 4);
 
170
                        }
 
171
                        // read bytes, matching against the boundary
 
172
                        for (i = 0; i < bl; i++)
 
173
                            if (in.read() != bndbytes[i])
 
174
                                break;
 
175
                        if (i == bl) {
 
176
                            int b2 = in.read();
 
177
                            // check for end of line
 
178
                            if (b2 == '\n')
 
179
                                break;  // got it!  break out of the while loop
 
180
                            if (b2 == '\r') {
 
181
                                in.mark(1);
 
182
                                if (in.read() != '\n')
 
183
                                    in.reset();
 
184
                                break;  // got it!  break out of the while loop
 
185
                            }
 
186
                        }
 
187
                        // failed to match, reset and proceed normally
 
188
                        in.reset();
 
189
                    }
 
190
                    buf.write(b);
 
191
                }
 
192
 
 
193
                /*
 
194
                 * Create a SunV3BodyPart to represent this body part.
 
195
                 */
 
196
                SunV3BodyPart body =
 
197
                        new SunV3BodyPart(headers, buf.toByteArray());
 
198
                addBodyPart(body);
 
199
                if (b < 0)
 
200
                    break;
 
201
            }
 
202
        } catch (IOException e) {
 
203
            throw new MessagingException("IO Error");   // XXX
 
204
        }
 
205
 
 
206
        parsed = true;
 
207
    }
 
208
}