1 2 24 25 26 27 28 package com.lutris.mime; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 32 import com.lutris.util.BMByteSearch; 33 import com.lutris.util.BMByteSearchStream; 34 35 39 public class MultipartMimeInput 40 { 41 46 protected MultipartMimeInputStream currentStream; 47 48 52 protected BMByteSearchStream inputSource; 53 54 59 protected String newlineString="\r\n"; 60 61 65 protected String inputSeparator; 66 67 71 protected BMByteSearch inputPattern; 72 73 77 protected boolean atEOF = false; 78 79 99 public 100 MultipartMimeInput(InputStream source, ContentHeader contentHeader) 101 throws MimeException 102 { 103 atEOF = false; 104 String value = contentHeader.getValue(); 105 if (value == null) { 106 throw new MimeException("Missing content header value.", 107 MimeException.INVALID_HEADER); 108 } 109 if (!value.toLowerCase().trim().startsWith("multipart/")) { 110 throw new MimeException("Illegal mime type.", 111 MimeException.INVALID_MIME_TYPE); 112 } 113 String boundary = contentHeader.getParameter("boundary"); 114 if (boundary == null) { 115 throw new MimeException("Missing boundary parameter.", 116 MimeException.INVALID_HEADER); 117 } 118 119 int skipped=0; 123 try { 124 inputSource = new BMByteSearchStream(source, boundary, 2000); 125 skipped = inputSource.skipPattern(); 126 } catch (IOException e) { 127 throw new MimeException("Error while reading to first boundary: "+ 128 e.toString(), MimeException.GENERIC); 129 } 130 if (skipped < 1) { 131 throw new MimeException("Boundary pattern missing in input " + 132 "stream.", MimeException.GENERIC); 133 } 134 135 try { 142 switch (inputSource.read()) { 143 case '\r': 144 if (inputSource.read() != '\n') { 145 newlineString = "\r"; 147 } else { 148 newlineString="\r\n"; 150 } 151 break; 152 case '\n': 153 newlineString="\n"; break; 160 default: 161 atEOF = true; throw new MimeException("Missing newline after boundary.", 166 MimeException.INVALID_HEADER); 167 } 168 } catch (IOException e) { 169 throw new MimeException("Missing newline after boundary:" + 170 e.toString(), 171 MimeException.INVALID_HEADER); 172 } 173 inputSeparator = newlineString + "--" + boundary; 174 inputPattern = new BMByteSearch(inputSeparator); 175 inputSource.setPattern(inputPattern); 176 } 177 178 193 public 194 MultipartMimeInputStream nextPart() 195 throws MimeException 196 { 197 try { 198 if (currentStream != null) { 199 currentStream.close(); 200 if (currentStream.lastPart) atEOF = true; 201 } 202 if (atEOF) return null; 203 currentStream = new MultipartMimeInputStream(inputSource, 204 inputPattern); 205 } catch (MimeEOFException e) { 206 return null; 207 } catch (IOException ioe) { 208 throw new MimeException("IO Error between parts: " + 209 ioe.toString(), MimeException.GENERIC); 210 } 211 return currentStream; 212 } 213 214 222 public 223 void close() 224 throws MimeException 225 { 226 try { 227 currentStream.close(); 228 currentStream = null; 229 inputSource.close(); 230 inputSource = null; 231 atEOF = true; 232 } catch (IOException ioe) { 233 throw new MimeException("MultipartMimeInput: " + 234 "IO Error during close: " + ioe.toString(), 235 MimeException.GENERIC); 236 } 237 } 238 } 239 | Popular Tags |