1 18 19 package org.apache.struts.upload; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 import javax.servlet.ServletException ; 25 26 40 41 class MultipartValueStream extends InputStream { 42 43 public static final String HEADER_ENCODING = "iso-8859-1"; 44 45 46 private InputStream in; 47 48 49 private byte boundaryBytes[]; 50 51 52 private int matchedBoundaryBytes; 53 54 55 private byte readAheadBytes[]; 56 57 58 private int readAheadBufferStartI; 59 60 61 private int readAheadBufferEndI; 62 63 64 private boolean boundaryReached = false; 65 66 67 private boolean finalBoundaryReached = false; 68 69 70 75 public MultipartValueStream(InputStream in, String boundary) 76 throws IOException 77 { 78 this.in = in; 79 this.boundaryBytes = ("\r\n" + boundary).getBytes(HEADER_ENCODING); 80 this.matchedBoundaryBytes = 0; 81 this.readAheadBytes = new byte[this.boundaryBytes.length]; 82 83 84 if (in.read(readAheadBytes, 0, readAheadBytes.length) != readAheadBytes.length) { 85 throw new IOException ("end of stream before boundary found!"); 86 } 87 88 89 for (int i = 0; i < readAheadBytes.length; i++) { 90 if (readAheadBytes[i] == boundaryBytes[matchedBoundaryBytes]) { 91 matchedBoundaryBytes++; 92 } else { 93 matchedBoundaryBytes = 0; 94 if (readAheadBytes[i] == boundaryBytes[0]) { 95 matchedBoundaryBytes = 1; 96 } 97 } 98 } 99 100 readAheadBufferStartI = 0; 101 readAheadBufferEndI = readAheadBytes.length - 1; 102 } 103 104 105 112 113 public int read() throws IOException { 114 if (boundaryReached) { 115 return -1; 116 } 117 if (matchedBoundaryBytes == boundaryBytes.length) { 118 119 boundaryReached = true; 120 121 129 130 byte buf[] = new byte[2]; 131 if (in.read(buf) != 2) { 132 throw new IOException ("end of stream before boundary found!"); 133 } 134 135 String readStr = new String (buf, HEADER_ENCODING); 136 if (readStr.equals("--")) { 137 138 if (in.read(buf) != 2) { 139 throw new IOException ("invalid end of final boundary found!"); 140 } 141 readStr = new String (buf, HEADER_ENCODING); 142 if (!readStr.equals("\r\n")) { 143 throw new IOException ("invalid end of final boundary found!"); 144 } 145 finalBoundaryReached = true; 146 147 } else if (readStr.equals("\r\n")) { 148 finalBoundaryReached = false; 149 } else { 150 throw new IOException ("invalid end of boundary found!"); 151 } 152 153 return -1; 154 } 155 156 162 int returnByte = (int)(char) readAheadBytes[readAheadBufferStartI]; 163 164 165 readAheadBufferStartI++; 166 if (readAheadBufferStartI == readAheadBytes.length) { 167 readAheadBufferStartI = 0; 168 } 169 170 171 int underlyingRead = in.read(); 172 if (underlyingRead == -1) { 173 throw new IOException ("end of stream before boundary found!"); 174 } 175 176 177 readAheadBufferEndI++; 178 if (readAheadBufferEndI == readAheadBytes.length) { 179 readAheadBufferEndI = 0; 180 } 181 readAheadBytes[readAheadBufferEndI] = (byte) underlyingRead; 182 183 if (readAheadBytes[readAheadBufferEndI] == boundaryBytes[matchedBoundaryBytes]) { 184 matchedBoundaryBytes++; 185 } else { 186 matchedBoundaryBytes = 0; 187 if (readAheadBytes[readAheadBufferEndI] == boundaryBytes[0]) { 188 matchedBoundaryBytes = 1; 189 } 190 } 191 return returnByte; 192 } 193 194 200 201 public boolean encounteredFinalBoundary() 202 throws ServletException 203 { 204 if (!boundaryReached) { 205 throw new ServletException ("have not reached boundary yet!"); 206 } 207 return finalBoundaryReached; 208 } 209 } 210 | Popular Tags |