1 17 package org.apache.axis2.attachments; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.PushbackInputStream ; 22 23 27 28 public class MimeBodyPartInputStream extends InputStream { 29 PushbackInputStream inStream; 30 31 boolean boundaryFound = false; 32 33 byte[] boundary; 34 35 public MimeBodyPartInputStream(PushbackInputStream inStream, byte[] boundary) { 36 super(); 37 this.inStream = inStream; 38 this.boundary = boundary; 39 } 40 41 public int read() throws IOException { 42 if (boundaryFound) { 43 return -1; 44 } 45 int value = inStream.read(); 47 48 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 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) { boundaryFound = true; 79 if (inStream.read() == 45) { 81 if (!((value = inStream.read()) == 45)) { 84 inStream.unread(value); 85 } 86 87 } 88 89 return -1; 90 } 91 92 95 if (value != -1) { inStream.unread(value); 97 } 98 inStream.unread(boundary, 1, boundaryIndex - 1); 99 return boundary[0]; 100 } 101 } | Popular Tags |