KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > attachments > MimeBodyPartInputStream


1 /**
2  * Copyright 2001-2004 The Apache Software Foundation.
3  * <p/>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p/>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p/>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * <p/>
16  */

17 package org.apache.axis2.attachments;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.PushbackInputStream JavaDoc;
22
23 /**
24  * @author <a HREF="mailto:thilina@opensource.lk">Thilina Gunarathne </a>
25  * This class provides input streams delimited by the mime boundaries.
26  */

27
28 public class MimeBodyPartInputStream extends InputStream JavaDoc {
29     PushbackInputStream JavaDoc inStream;
30
31     boolean boundaryFound = false;
32
33     byte[] boundary;
34
35     public MimeBodyPartInputStream(PushbackInputStream JavaDoc inStream, byte[] boundary) {
36         super();
37         this.inStream = inStream;
38         this.boundary = boundary;
39     }
40
41     public int read() throws IOException JavaDoc {
42         if (boundaryFound) {
43             return -1;
44         }
45         // read the next value from stream
46
int value = inStream.read();
47
48         // A problem occured because all the mime parts tends to have a /r/n at the end. Making it hard to transform them to correct DataSources.
49
// This logic introduced to handle it
50
//TODO look more in to this && for a better way to do this
51
if (value == 13) {
52             value = inStream.read();
53             if (value != 10) {
54                 inStream.unread(value);
55                 return 13;
56             } else {
57                 value = inStream.read();
58                 if ((byte) value != boundary[0]) {
59                     inStream.unread(value);
60                     inStream.unread(10);
61                     return 13;
62                 }
63             }
64         } else if ((byte) value != boundary[0]) {
65             return value;
66         }
67
68         // read value is the first byte of the boundary. Start matching the
69
// next characters to find a boundary
70
int boundaryIndex = 0;
71         while ((boundaryIndex < boundary.length)
72                 && ((byte) value == boundary[boundaryIndex])) {
73             value = inStream.read();
74             boundaryIndex++;
75         }
76
77         if (boundaryIndex == boundary.length) { // boundary found
78
boundaryFound = true;
79             // read the end of line character
80
if (inStream.read() == 45) {
81                 //check whether end of stream
82
//Last mime boundary should have a succeeding "--"
83
if (!((value = inStream.read()) == 45)) {
84                     inStream.unread(value);
85                 }
86
87             }
88
89             return -1;
90         }
91
92         // Boundary not found. Restoring bytes skipped.
93
// write first skipped byte, push back the rest
94

95         if (value != -1) { // Stream might have ended
96
inStream.unread(value);
97         }
98         inStream.unread(boundary, 1, boundaryIndex - 1);
99         return boundary[0];
100     }
101 }
Popular Tags